-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirty.rb
64 lines (55 loc) · 886 Bytes
/
dirty.rb
1
2
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
require "observer"
module Dirty
include Observable
def initialize
add_observer(self)
end
def self.included(klass)
class << klass
def define_dirty_attributes(*args)
args.each do |param|
class_eval %{
def #{param}=(val)
@#{param}=val
end
def #{param}
@#{param}
end
}
end
end
end
end
def save
changes
puts self
notify_observers(age)
end
def changes
changed
end
def update(num)
puts num
end
end
class User
include Dirty
attr_accessor :name, :age, :email
define_dirty_attributes :name ,:age
end
u = User.new
u.name = 'Akhil'
u.email = '[email protected]'
u.age = 30
puts u.age
puts 1
puts u.changed?
puts 2
puts u.save
puts 3
puts u.changed?
puts 4
u.name = 'himanshu'
puts 5
puts u.changed?
puts 6