56. class MyLib
def self.accessor(method)
class_eval <<-RUBY
def self.#{method}(*args)
if args.size.zero?
@#{method}
else
@#{method} = args.last
end
end
RUBY
end
accessor :name
end
64. class MyLib
NAMES = { :name => "mylib’s instance" }
def method_missing(method, *args)
if NAMES.key?(method.to_sym)
NAMES[method.to_sym]
else
super
end
end
end
65. class MyLib
#...
def respond_to?(method, include_private = false)
if NAMES.key?(method.to_sym)
true
else
super
end
end
end
68. class MyLib
extend Accessor
accessor :name
end
class MyOtherLib
extend Accessor
accessor :name
end
69. module Accessor
def accessor(name)
class_eval <<-RUBY
def self.#{name}(*args)
if args.size.zero?
@#{name}
else
@#{name} = args.last
end
end
RUBY
end
end
73. class String
def to_permalink
self.downcase.gsub(/[^[a-z0-9]-]/, "-")
end
end
74. class Conference < ActiveRecord::Base
before_validation :generate_permalink
def generate_permalink
write_attribute :permalink, name.to_s.to_permalink
end
end
76. module Permalink
def self.included(base)
base.send :extend, ClassMethods
end
end
77. module Permalink
# ...
module ClassMethods
def has_permalink
class_eval do
before_validation :generate_permalink
include InstanceMethods
end
end
end
end
78. module Permalink
# ...
module InstanceMethods
def generate_permalink
write_attribute :permalink, name.to_s.to_permalink
end
end
end