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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/soulless/dirty.rb', line 3
def self.included(base)
base.class_eval do
def initialize(params = {})
super
init_dirty(attribute_set.map { |a| a.name })
end
private
def init_dirty(attributes)
define_dirty_attributes(attributes)
define_dirty_methods(attributes)
@changed_attributes = {}
end
def changes_applied
@previously_changed = changed
@changed_attributes = {}
end
def define_dirty_attributes(attributes)
self.class.define_attribute_methods(attributes)
end
def define_dirty_methods(attributes)
attributes.each do |attribute|
define_dirty_reader(attribute)
define_dirty_writer(attribute)
end
end
def define_dirty_reader(name)
self.class.send(:define_method, "#{name}".to_sym) do
instance_variable_get("@#{name}")
end
end
def define_dirty_writer(name)
self.class.send(:define_method, "#{name}=".to_sym) do |value|
send("#{name}_will_change!") unless instance_variable_get("@#{name}") == value
super(value)
end
end
end
end
|