Module: MiniModel
- Defined in:
- lib/mini_model.rb
Defined Under Namespace
Modules: ClassMethods
Classes: Error, MissingId
Constant Summary
collapse
- VERSION =
'0.0.2'
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(model) ⇒ Object
8
9
10
|
# File 'lib/mini_model.rb', line 8
def self.included(model)
model.extend(ClassMethods)
end
|
Instance Method Details
#==(other) ⇒ Object
Strap in, the is probably the most complicated method in the entire library.
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/mini_model.rb', line 169
def ==(other)
if self.class != other.class
return false
end
if persisted? != other.persisted?
return false
end
if persisted? && id != other.id
return false
end
attributes == other.attributes
end
|
#attributes ⇒ Object
148
149
150
|
# File 'lib/mini_model.rb', line 148
def attributes
@attributes
end
|
#attributes=(attributes) ⇒ Object
#attributes= is vulnerable to mass assignment attacks if used directly with user input. Some sort of filter must be in place before setting attributes or initializing a new model.
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/mini_model.rb', line 155
def attributes=(attributes)
@attributes = {}
attributes.each do |key, value|
writer = :"#{key}="
if respond_to?(writer)
send(writer, value)
end
end
end
|
#create ⇒ Object
#create (as well as #update, and #delete) return self on success and nil on failure. This lets us use these actions as if conditions which is convenience though dangerous.
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/mini_model.rb', line 209
def create
id = dataset.insert(attributes)
if id
self.id = id
self
else
nil
end
end
|
#dataset ⇒ Object
129
130
131
|
# File 'lib/mini_model.rb', line 129
def dataset
self.class.dataset
end
|
#delete ⇒ Object
231
232
233
234
235
236
237
238
239
240
241
|
# File 'lib/mini_model.rb', line 231
def delete
count = dataset.where(id: id).delete
if count.to_i > 0
self.id = nil
self
else
nil
end
end
|
#id ⇒ Object
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/mini_model.rb', line 133
def id
if @id
@id
else
raise MissingId
end
end
|
#id=(id) ⇒ Object
144
145
146
|
# File 'lib/mini_model.rb', line 144
def id=(id)
@id = id
end
|
#initialize(attributes = {}) ⇒ Object
125
126
127
|
# File 'lib/mini_model.rb', line 125
def initialize(attributes = {})
self.attributes = attributes
end
|
#persisted? ⇒ Boolean
191
192
193
|
# File 'lib/mini_model.rb', line 191
def persisted?
!!@id
end
|
#save ⇒ Object
Use #save to write generic persistence code in things like form objects so you don’t have to reach inside the model to determine the proper method to call.
198
199
200
201
202
203
204
|
# File 'lib/mini_model.rb', line 198
def save
if persisted?
update
else
create
end
end
|
#update ⇒ Object
221
222
223
224
225
226
227
228
229
|
# File 'lib/mini_model.rb', line 221
def update
count = dataset.where(id: id).update(attributes)
if count.to_i > 0
self
else
nil
end
end
|