3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/hashish/attribute_hash_accessors.rb', line 3
def hash_attr_accessors(*attrs)
attrs.each do |method|
method_name = method.to_s
define_method(method.to_sym) do
@attributes[method_name]
end
define_method("#{method_name}=".to_sym) do |value|
send("#{method_name}_will_change!") unless @attributes[method_name] == value
@attributes[method_name] = value
end
end
define_method(:attributes) do
ActiveSupport::HashWithIndifferentAccess.new.tap do |hash|
attrs.each do |attr|
attr_name = attr.to_s
hash[attr_name] = @attributes[attr_name]
end
end
end
self.class_eval do
define_attribute_methods attrs
end
end
|