@@ -15,9 +15,18 @@ def count
15
15
alias_method :size , :count
16
16
alias_method :length , :count
17
17
18
+ def last
19
+ all . last
20
+ end
21
+
22
+ def find ( id )
23
+ id = id . to_i
24
+ all . find { |record | record . id == id } || raise
25
+ end
26
+
18
27
def create! ( attributes = { } )
19
- with_id = { :id => next_id , :persisted => true }
20
- all << record = new ( with_id . merge ( attributes ) )
28
+ record = new ( attributes )
29
+ record . save
21
30
record
22
31
end
23
32
@@ -38,16 +47,50 @@ class Model
38
47
include ::ActiveModel ::Validations
39
48
40
49
def initialize ( attributes = { } )
41
- attributes . each do |name , value |
42
- send ( "#{ name } =" , value )
43
- end
50
+ assign_attributes ( attributes )
44
51
end
45
52
end
46
53
47
54
attr_accessor :id , :persisted
48
55
49
56
alias_method :persisted? , :persisted
50
57
58
+ def update ( attributes )
59
+ assign_attributes ( attributes )
60
+ save
61
+ end
62
+
63
+ alias_method :update_attributes , :update
64
+
65
+ def assign_attributes ( attributes )
66
+ attributes . each do |name , value |
67
+ __send__ ( "#{ name } =" , value )
68
+ end
69
+ end
70
+
71
+ def save ( *)
72
+ self . id = self . class . next_id
73
+ self . class . all << self
74
+ true
75
+ end
76
+
77
+ def destroy
78
+ self . class . all . delete ( self )
79
+ true
80
+ end
81
+
82
+ def reload ( *)
83
+ self
84
+ end
85
+
86
+ def ==( other )
87
+ other . is_a? ( self . class ) && id == other . id
88
+ end
89
+
90
+ def persisted?
91
+ !id . nil?
92
+ end
93
+
51
94
def new_record?
52
95
!persisted?
53
96
end
0 commit comments