Class: SuperModel::Base
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
included
Methods included from Dirty
#save_previous_changes
#save_with_validation
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
143
144
145
146
147
148
149
|
# File 'lib/supermodel/base.rb', line 143
def initialize(attributes = {})
@new_record = true
@attributes = {}.with_indifferent_access
@attributes.merge!(known_attributes.inject({}) {|h, n| h[n] = nil; h })
@changed_attributes = {}
load(attributes)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *arguments) ⇒ Object
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
# File 'lib/supermodel/base.rb', line 299
def method_missing(method_symbol, *arguments) method_name = method_symbol.to_s
if method_name =~ /(=|\?)$/
case $1
when "="
attribute_will_change!($`)
attributes[$`] = arguments.first
when "?"
attributes[$`]
end
else
return attributes[method_name] if attributes.include?(method_name)
return nil if known_attributes.include?(method_name)
super
end
end
|
Class Attribute Details
.primary_key ⇒ Object
7
8
9
|
# File 'lib/supermodel/base.rb', line 7
def primary_key
@primary_key
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
136
137
138
|
# File 'lib/supermodel/base.rb', line 136
def attributes
@attributes
end
|
#new_record=(value) ⇒ Object
Sets the attribute new_record
137
138
139
|
# File 'lib/supermodel/base.rb', line 137
def new_record=(value)
@new_record = value
end
|
Class Method Details
.all ⇒ Object
79
80
81
|
# File 'lib/supermodel/base.rb', line 79
def all
collection.new(records.values.deep_dup)
end
|
.attributes(*attributes) ⇒ Object
19
20
21
|
# File 'lib/supermodel/base.rb', line 19
def attributes(*attributes)
self.known_attributes |= attributes.map(&:to_s)
end
|
.collection(&block) ⇒ Object
13
14
15
16
17
|
# File 'lib/supermodel/base.rb', line 13
def collection(&block)
@collection ||= Class.new(Array)
@collection.class_eval(&block) if block_given?
@collection
end
|
.count ⇒ Object
75
76
77
|
# File 'lib/supermodel/base.rb', line 75
def count
records.length
end
|
.create(atts = {}) ⇒ Object
Create a new record. Example:
create(:name => "foo", :id => 1)
110
111
112
113
|
# File 'lib/supermodel/base.rb', line 110
def create(atts = {})
rec = self.new(atts)
rec.save && rec
end
|
.create!(*args) ⇒ Object
115
116
117
|
# File 'lib/supermodel/base.rb', line 115
def create!(*args)
create(*args) || raise(InvalidRecord)
end
|
.delete_all ⇒ Object
Removes all records without executing destroy callbacks.
103
104
105
|
# File 'lib/supermodel/base.rb', line 103
def delete_all
records.clear
end
|
.destroy(id) ⇒ Object
91
92
93
|
# File 'lib/supermodel/base.rb', line 91
def destroy(id)
find(id).destroy
end
|
.destroy_all ⇒ Object
Removes all records and executes destroy callbacks.
97
98
99
|
# File 'lib/supermodel/base.rb', line 97
def destroy_all
all.each {|r| r.destroy }
end
|
.exists?(id) ⇒ Boolean
71
72
73
|
# File 'lib/supermodel/base.rb', line 71
def exists?(id)
records.has_key?(id)
end
|
.find(id) ⇒ Object
Also known as:
[]
Find record by ID, or raise.
42
43
44
45
|
# File 'lib/supermodel/base.rb', line 42
def find(id)
item = raw_find(id)
item && item.dup
end
|
.find_all_by_attribute(name, value) ⇒ Object
32
33
34
35
|
# File 'lib/supermodel/base.rb', line 32
def find_all_by_attribute(name, value) items = records.values.select {|r| r.send(name) == value }
collection.new(items.deep_dup)
end
|
.find_by_attribute(name, value) ⇒ Object
27
28
29
30
|
# File 'lib/supermodel/base.rb', line 27
def find_by_attribute(name, value) item = records.values.find {|r| r.send(name) == value }
item && item.dup
end
|
.first ⇒ Object
48
49
50
51
|
# File 'lib/supermodel/base.rb', line 48
def first
item = records.values[0]
item && item.dup
end
|
.last ⇒ Object
53
54
55
56
|
# File 'lib/supermodel/base.rb', line 53
def last
item = records.values[-1]
item && item.dup
end
|
.method_missing(method_symbol, *args) ⇒ Object
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/supermodel/base.rb', line 119
def method_missing(method_symbol, *args) method_name = method_symbol.to_s
if method_name =~ /^find_by_(\w+)!/
send("find_by_#{$1}", *args) || raise(UnknownRecord)
elsif method_name =~ /^find_by_(\w+)/
find_by_attribute($1, args.first)
elsif method_name =~ /^find_or_create_by_(\w+)/
send("find_by_#{$1}", *args) || create($1 => args.first)
elsif method_name =~ /^find_all_by_(\w+)/
find_all_by_attribute($1, args.first)
else
super
end
end
|
.raw_find(id) ⇒ Object
37
38
39
|
# File 'lib/supermodel/base.rb', line 37
def raw_find(id) records[id] || raise(UnknownRecord, "Couldn't find #{self.name} with ID=#{id}")
end
|
.records ⇒ Object
23
24
25
|
# File 'lib/supermodel/base.rb', line 23
def records
@records ||= {}
end
|
.select(&block) ⇒ Object
83
84
85
|
# File 'lib/supermodel/base.rb', line 83
def select(&block)
collection.new(records.values.select(&block).deep_dup)
end
|
.update(id, atts) ⇒ Object
87
88
89
|
# File 'lib/supermodel/base.rb', line 87
def update(id, atts)
find(id).update_attributes(atts)
end
|
.where(options) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/supermodel/base.rb', line 58
def where(options)
items = records.values.select do |r|
options.all? do |k, v|
if v.is_a?(Enumerable)
v.include?(r.send(k))
else
r.send(k) == v
end
end
end
collection.new(items.deep_dup)
end
|
Instance Method Details
#==(other) ⇒ Object
175
176
177
|
# File 'lib/supermodel/base.rb', line 175
def ==(other)
other.equal?(self) || (other.instance_of?(self.class) && other.id == id)
end
|
#clone ⇒ Object
151
152
153
154
155
156
157
158
|
# File 'lib/supermodel/base.rb', line 151
def clone
cloned = attributes.reject {|k,v| k == self.class.primary_key }
cloned = cloned.inject({}) do |attrs, (k, v)|
attrs[k] = v.clone
attrs
end
self.class.new(cloned)
end
|
#destroy ⇒ Object
254
255
256
257
|
# File 'lib/supermodel/base.rb', line 254
def destroy
raw_destroy
self
end
|
#dup ⇒ Object
188
189
190
191
192
193
|
# File 'lib/supermodel/base.rb', line 188
def dup
self.class.new.tap do |base|
base.attributes = attributes
base.new_record = new_record?
end
end
|
#eql?(other) ⇒ Boolean
Tests for equality (delegates to ==).
180
181
182
|
# File 'lib/supermodel/base.rb', line 180
def eql?(other)
self == other
end
|
#exists? ⇒ Boolean
Also known as:
persisted?
203
204
205
|
# File 'lib/supermodel/base.rb', line 203
def exists?
!new?
end
|
#has_attribute?(name) ⇒ Boolean
235
236
237
|
# File 'lib/supermodel/base.rb', line 235
def has_attribute?(name)
@attributes.has_key?(name)
end
|
#hash ⇒ Object
184
185
186
|
# File 'lib/supermodel/base.rb', line 184
def hash
id.hash
end
|
#id ⇒ Object
Gets the \id
attribute of the item.
166
167
168
|
# File 'lib/supermodel/base.rb', line 166
def id
attributes[self.class.primary_key]
end
|
#id=(id) ⇒ Object
Sets the \id
attribute of the item.
171
172
173
|
# File 'lib/supermodel/base.rb', line 171
def id=(id)
attributes[self.class.primary_key] = id
end
|
#known_attributes ⇒ Object
139
140
141
|
# File 'lib/supermodel/base.rb', line 139
def known_attributes
self.class.known_attributes | self.attributes.keys.map(&:to_s)
end
|
#load(attributes) ⇒ Object
208
209
210
211
212
213
|
# File 'lib/supermodel/base.rb', line 208
def load(attributes) return unless attributes
attributes.each do |(name, value)|
self.send("#{name}=".to_sym, value)
end
end
|
#new? ⇒ Boolean
Also known as:
new_record?
160
161
162
|
# File 'lib/supermodel/base.rb', line 160
def new?
@new_record || false
end
|
#reload ⇒ Object
215
216
217
218
219
220
|
# File 'lib/supermodel/base.rb', line 215
def reload
return self if new?
item = self.class.find(id)
load(item.attributes)
return self
end
|
#respond_to?(method, include_priv = false) ⇒ Boolean
241
242
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/supermodel/base.rb', line 241
def respond_to?(method, include_priv = false)
method_name = method.to_s
if attributes.nil?
super
elsif known_attributes.include?(method_name)
true
elsif method_name =~ /(?:=|\?)$/ && attributes.include?($`)
true
else
super
end
end
|
#respond_to_without_attributes? ⇒ Object
239
|
# File 'lib/supermodel/base.rb', line 239
alias_method :respond_to_without_attributes?, :respond_to?
|
#save ⇒ Object
195
196
197
|
# File 'lib/supermodel/base.rb', line 195
def save
new? ? create : update
end
|
#save! ⇒ Object
199
200
201
|
# File 'lib/supermodel/base.rb', line 199
def save!
save || raise(InvalidRecord)
end
|
#update_attribute(name, value) ⇒ Object
222
223
224
225
|
# File 'lib/supermodel/base.rb', line 222
def update_attribute(name, value)
self.send("#{name}=".to_sym, value)
self.save
end
|
#update_attributes(attributes) ⇒ Object
227
228
229
|
# File 'lib/supermodel/base.rb', line 227
def update_attributes(attributes)
load(attributes) && save
end
|
#update_attributes!(attributes) ⇒ Object
231
232
233
|
# File 'lib/supermodel/base.rb', line 231
def update_attributes!(attributes)
update_attributes(attributes) || raise(InvalidRecord)
end
|