Class: SuperModel::Base
- Inherits:
-
Object
show all
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::Serializers::JSON, ActiveModel::Serializers::Xml, Callbacks, Observing, Validations
- Defined in:
- lib/supermodel/base.rb,
lib/supermodel/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#save_with_validation
Constructor Details
#initialize(attributes = {}) ⇒ Base
Returns a new instance of Base.
101
102
103
104
105
|
# File 'lib/supermodel/base.rb', line 101
def initialize(attributes = {})
@new_record = true
@attributes = {}.with_indifferent_access
load(attributes)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *arguments) ⇒ Object
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
# File 'lib/supermodel/base.rb', line 249
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
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
94
95
96
|
# File 'lib/supermodel/base.rb', line 94
def attributes
@attributes
end
|
#new_record=(value) ⇒ Object
Sets the attribute new_record
95
96
97
|
# File 'lib/supermodel/base.rb', line 95
def new_record=(value)
@new_record = value
end
|
Class Method Details
.all ⇒ Object
47
48
49
|
# File 'lib/supermodel/base.rb', line 47
def all
records.dup
end
|
.attributes(*attributes) ⇒ Object
10
11
12
|
# File 'lib/supermodel/base.rb', line 10
def attributes(*attributes)
self.known_attributes += attributes.map(&:to_s)
end
|
.count ⇒ Object
43
44
45
|
# File 'lib/supermodel/base.rb', line 43
def count
records.length
end
|
.create(atts = {}) ⇒ Object
Create a new record. Example:
create(:name => "foo", :id => 1)
74
75
76
77
|
# File 'lib/supermodel/base.rb', line 74
def create(atts = {})
rec = self.new(atts)
rec.save && rec
end
|
.delete_all ⇒ Object
Removes all records without executing destroy callbacks.
67
68
69
|
# File 'lib/supermodel/base.rb', line 67
def delete_all
records.clear
end
|
.destroy(id) ⇒ Object
55
56
57
|
# File 'lib/supermodel/base.rb', line 55
def destroy(id)
find(id).destroy
end
|
.destroy_all ⇒ Object
Removes all records and executes destory callbacks.
61
62
63
|
# File 'lib/supermodel/base.rb', line 61
def destroy_all
all.each {|r| r.destroy }
end
|
.find(id) ⇒ Object
Also known as:
[]
Find record by ID, or raise.
27
28
29
30
|
# File 'lib/supermodel/base.rb', line 27
def find(id)
item = raw_find(id)
item && item.dup
end
|
.find_by_attribute(name, value) ⇒ Object
18
19
20
|
# File 'lib/supermodel/base.rb', line 18
def find_by_attribute(name, value) records.find {|r| r.send(name) == value }
end
|
.first ⇒ Object
33
34
35
36
|
# File 'lib/supermodel/base.rb', line 33
def first
item = records[0]
item && item.dup
end
|
.last ⇒ Object
38
39
40
41
|
# File 'lib/supermodel/base.rb', line 38
def last
item = records[-1]
item && item.dup
end
|
.method_missing(method_symbol, *args) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/supermodel/base.rb', line 79
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)
else
super
end
end
|
.raw_find(id) ⇒ Object
22
23
24
|
# File 'lib/supermodel/base.rb', line 22
def raw_find(id) find_by_attribute(:id, id) || raise(UnknownRecord)
end
|
.records ⇒ Object
14
15
16
|
# File 'lib/supermodel/base.rb', line 14
def records
@records ||= []
end
|
.update(id, atts) ⇒ Object
51
52
53
|
# File 'lib/supermodel/base.rb', line 51
def update(id, atts)
find(id).update_attributes(atts)
end
|
Instance Method Details
#==(other) ⇒ Object
131
132
133
|
# File 'lib/supermodel/base.rb', line 131
def ==(other)
other.equal?(self) || (other.instance_of?(self.class) && other.id == id)
end
|
#clone ⇒ Object
107
108
109
110
111
112
113
114
|
# File 'lib/supermodel/base.rb', line 107
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
201
202
203
204
|
# File 'lib/supermodel/base.rb', line 201
def destroy
raw_destroy
self
end
|
#dup ⇒ Object
144
145
146
147
148
149
|
# File 'lib/supermodel/base.rb', line 144
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 ==).
136
137
138
|
# File 'lib/supermodel/base.rb', line 136
def eql?(other)
self == other
end
|
#exists? ⇒ Boolean
159
160
161
|
# File 'lib/supermodel/base.rb', line 159
def exists?
!new?
end
|
#has_attribute?(name) ⇒ Boolean
178
179
180
|
# File 'lib/supermodel/base.rb', line 178
def has_attribute?(name)
@attributes.has_key?(name)
end
|
#hash ⇒ Object
140
141
142
|
# File 'lib/supermodel/base.rb', line 140
def hash
id.hash
end
|
#id ⇒ Object
Gets the \id
attribute of the item.
122
123
124
|
# File 'lib/supermodel/base.rb', line 122
def id
attributes[self.class.primary_key]
end
|
#id=(id) ⇒ Object
Sets the \id
attribute of the item.
127
128
129
|
# File 'lib/supermodel/base.rb', line 127
def id=(id)
attributes[self.class.primary_key] = id
end
|
#known_attributes ⇒ Object
97
98
99
|
# File 'lib/supermodel/base.rb', line 97
def known_attributes
self.class.known_attributes + self.attributes.keys.map(&:to_s)
end
|
#load(attributes) ⇒ Object
163
164
165
166
167
|
# File 'lib/supermodel/base.rb', line 163
def load(attributes) attributes.each {|(name, value)|
self.send("#{name}=".to_sym, value)
}
end
|
#new? ⇒ Boolean
Also known as:
new_record?
116
117
118
|
# File 'lib/supermodel/base.rb', line 116
def new?
@new_record || false
end
|
#raw_destroy ⇒ Object
197
198
199
|
# File 'lib/supermodel/base.rb', line 197
def raw_destroy
self.class.records.delete(self)
end
|
#respond_to?(method, include_priv = false) ⇒ Boolean
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/supermodel/base.rb', line 184
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
182
|
# File 'lib/supermodel/base.rb', line 182
alias_method :respond_to_without_attributes?, :respond_to?
|
#save ⇒ Object
151
152
153
|
# File 'lib/supermodel/base.rb', line 151
def save
new? ? create : update
end
|
#save! ⇒ Object
155
156
157
|
# File 'lib/supermodel/base.rb', line 155
def save!
save || raise(InvalidRecord)
end
|
#update_attribute(name, value) ⇒ Object
169
170
171
172
|
# File 'lib/supermodel/base.rb', line 169
def update_attribute(name, value)
self.send("#{name}=".to_sym, value)
self.save
end
|
#update_attributes(attributes) ⇒ Object
174
175
176
|
# File 'lib/supermodel/base.rb', line 174
def update_attributes(attributes)
load(attributes) && save
end
|