Class: TopModel::Base
- Inherits:
-
Object
show all
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, Association::Model, Callbacks, Dirty, Validations
- Defined in:
- lib/topmodel/base.rb,
lib/topmodel/base.rb
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.
151
152
153
154
155
156
157
|
# File 'lib/topmodel/base.rb', line 151
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
# File 'lib/topmodel/base.rb', line 307
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/topmodel/base.rb', line 7
def primary_key
@primary_key
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
144
145
146
|
# File 'lib/topmodel/base.rb', line 144
def attributes
@attributes
end
|
#new_record=(value) ⇒ Object
Sets the attribute new_record
145
146
147
|
# File 'lib/topmodel/base.rb', line 145
def new_record=(value)
@new_record = value
end
|
Class Method Details
.all ⇒ Object
87
88
89
|
# File 'lib/topmodel/base.rb', line 87
def all
collection.new(records.values.deep_dup)
end
|
.attributes(*attributes) ⇒ Object
27
28
29
|
# File 'lib/topmodel/base.rb', line 27
def attributes(*attributes)
self.known_attributes |= attributes.map(&:to_s)
end
|
.belongs_assoc ⇒ Object
9
10
11
|
# File 'lib/topmodel/base.rb', line 9
def belongs_assoc
@belongs_assoc ||= {}
end
|
.collection(&block) ⇒ Object
21
22
23
24
25
|
# File 'lib/topmodel/base.rb', line 21
def collection(&block)
@collection ||= Class.new(Array)
@collection.class_eval(&block) if block_given?
@collection
end
|
.count ⇒ Object
83
84
85
|
# File 'lib/topmodel/base.rb', line 83
def count
records.length
end
|
.create(atts = {}) ⇒ Object
Create a new record. Example:
create(:name => "foo", :id => 1)
118
119
120
121
|
# File 'lib/topmodel/base.rb', line 118
def create(atts = {})
rec = self.new(atts)
rec.save && rec
end
|
.create!(*args) ⇒ Object
123
124
125
|
# File 'lib/topmodel/base.rb', line 123
def create!(*args)
create(*args) || raise(InvalidRecord)
end
|
.delete_all ⇒ Object
Removes all records without executing destroy callbacks.
111
112
113
|
# File 'lib/topmodel/base.rb', line 111
def delete_all
records.clear
end
|
.destroy(id) ⇒ Object
99
100
101
|
# File 'lib/topmodel/base.rb', line 99
def destroy(id)
find(id).destroy
end
|
.destroy_all ⇒ Object
Removes all records and executes destroy callbacks.
105
106
107
|
# File 'lib/topmodel/base.rb', line 105
def destroy_all
all.each {|r| r.destroy }
end
|
.exists?(id) ⇒ Boolean
79
80
81
|
# File 'lib/topmodel/base.rb', line 79
def exists?(id)
records.has_key?(id)
end
|
.find(id) ⇒ Object
Also known as:
[]
Find record by ID, or raise.
50
51
52
53
|
# File 'lib/topmodel/base.rb', line 50
def find(id)
item = raw_find(id)
item && item.dup
end
|
.find_all_by_attribute(name, value) ⇒ Object
40
41
42
43
|
# File 'lib/topmodel/base.rb', line 40
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
35
36
37
38
|
# File 'lib/topmodel/base.rb', line 35
def find_by_attribute(name, value) item = records.values.find {|r| r.send(name) == value }
item && item.dup
end
|
.first ⇒ Object
56
57
58
59
|
# File 'lib/topmodel/base.rb', line 56
def first
item = records.values[0]
item && item.dup
end
|
.has_many_assoc ⇒ Object
13
14
15
|
# File 'lib/topmodel/base.rb', line 13
def has_many_assoc
@has_many_assoc ||= {}
end
|
.last ⇒ Object
61
62
63
64
|
# File 'lib/topmodel/base.rb', line 61
def last
item = records.values[-1]
item && item.dup
end
|
.method_missing(method_symbol, *args) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/topmodel/base.rb', line 127
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
45
46
47
|
# File 'lib/topmodel/base.rb', line 45
def raw_find(id) records[id] || raise(UnknownRecord, "Couldn't find #{self.name} with ID=#{id}")
end
|
.records ⇒ Object
31
32
33
|
# File 'lib/topmodel/base.rb', line 31
def records
@records ||= {}
end
|
.select(&block) ⇒ Object
91
92
93
|
# File 'lib/topmodel/base.rb', line 91
def select(&block)
collection.new(records.values.select(&block).deep_dup)
end
|
.update(id, atts) ⇒ Object
95
96
97
|
# File 'lib/topmodel/base.rb', line 95
def update(id, atts)
find(id).update_attributes(atts)
end
|
.where(options) ⇒ Object
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/topmodel/base.rb', line 66
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
183
184
185
|
# File 'lib/topmodel/base.rb', line 183
def ==(other)
other.equal?(self) || (other.instance_of?(self.class) && other.id == id)
end
|
#clone ⇒ Object
159
160
161
162
163
164
165
166
|
# File 'lib/topmodel/base.rb', line 159
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
262
263
264
265
|
# File 'lib/topmodel/base.rb', line 262
def destroy
raw_destroy
self
end
|
#dup ⇒ Object
196
197
198
199
200
201
|
# File 'lib/topmodel/base.rb', line 196
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 ==).
188
189
190
|
# File 'lib/topmodel/base.rb', line 188
def eql?(other)
self == other
end
|
#exists? ⇒ Boolean
Also known as:
persisted?
211
212
213
|
# File 'lib/topmodel/base.rb', line 211
def exists?
!new?
end
|
#has_attribute?(name) ⇒ Boolean
243
244
245
|
# File 'lib/topmodel/base.rb', line 243
def has_attribute?(name)
@attributes.has_key?(name)
end
|
#hash ⇒ Object
192
193
194
|
# File 'lib/topmodel/base.rb', line 192
def hash
id.hash
end
|
#id ⇒ Object
Gets the \id
attribute of the item.
174
175
176
|
# File 'lib/topmodel/base.rb', line 174
def id
attributes[self.class.primary_key]
end
|
#id=(id) ⇒ Object
Sets the \id
attribute of the item.
179
180
181
|
# File 'lib/topmodel/base.rb', line 179
def id=(id)
attributes[self.class.primary_key] = id
end
|
#known_attributes ⇒ Object
147
148
149
|
# File 'lib/topmodel/base.rb', line 147
def known_attributes
self.class.known_attributes | self.attributes.keys.map(&:to_s)
end
|
#load(attributes) ⇒ Object
216
217
218
219
220
221
|
# File 'lib/topmodel/base.rb', line 216
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?
168
169
170
|
# File 'lib/topmodel/base.rb', line 168
def new?
@new_record || false
end
|
#reload ⇒ Object
223
224
225
226
227
228
|
# File 'lib/topmodel/base.rb', line 223
def reload
return self if new?
item = self.class.find(id)
load(item.attributes)
return self
end
|
#respond_to?(method, include_priv = false) ⇒ Boolean
249
250
251
252
253
254
255
256
257
258
259
260
|
# File 'lib/topmodel/base.rb', line 249
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
247
|
# File 'lib/topmodel/base.rb', line 247
alias_method :respond_to_without_attributes?, :respond_to?
|
#save ⇒ Object
203
204
205
|
# File 'lib/topmodel/base.rb', line 203
def save
new? ? create : update
end
|
#save! ⇒ Object
207
208
209
|
# File 'lib/topmodel/base.rb', line 207
def save!
save || raise(InvalidRecord)
end
|
#update_attribute(name, value) ⇒ Object
230
231
232
233
|
# File 'lib/topmodel/base.rb', line 230
def update_attribute(name, value)
self.send("#{name}=".to_sym, value)
self.save
end
|
#update_attributes(attributes) ⇒ Object
235
236
237
|
# File 'lib/topmodel/base.rb', line 235
def update_attributes(attributes)
load(attributes) && save
end
|
#update_attributes!(attributes) ⇒ Object
239
240
241
|
# File 'lib/topmodel/base.rb', line 239
def update_attributes!(attributes)
update_attributes(attributes) || raise(InvalidRecord)
end
|