You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
698 B
35 lines
698 B
# A module which adds some generators for hash based accessors.
|
|
module HashAttrs
|
|
|
|
def hash_reader(hash_sym, syms)
|
|
syms.each do |id|
|
|
id = id.to_s.downcase
|
|
func = Proc.new do
|
|
hash = instance_variable_get(hash_sym)
|
|
hash[id.to_sym]
|
|
end
|
|
|
|
self.send(:define_method, id, func)
|
|
end
|
|
end
|
|
|
|
def hash_writer(hash_sym, syms)
|
|
syms.each do |id|
|
|
id = id.to_s.downcase
|
|
|
|
func = Proc.new do |val|
|
|
hash = instance_variable_get(hash_sym)
|
|
hash[id.to_sym] = val
|
|
end
|
|
|
|
self.send(:define_method, id+'=', func)
|
|
end
|
|
end
|
|
|
|
def hash_accessor(hash, *syms)
|
|
hash_reader(hash, syms)
|
|
hash_writer(hash, syms)
|
|
end
|
|
end
|
|
|