Module: VariaModel
- Defined in:
- lib/varia_model.rb,
lib/varia_model/version.rb,
lib/varia_model/attributes.rb
Defined Under Namespace
Modules: ClassMethods
Classes: Attributes
Constant Summary
collapse
- VERSION =
"0.6.0"
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(base) ⇒ Object
158
159
160
|
# File 'lib/varia_model.rb', line 158
def included(base)
base.extend(ClassMethods)
end
|
Instance Method Details
#_attributes_ ⇒ Hashie::Mash
Also known as:
to_hash
The storage hash containing all of the key/values for this object’s attributes
247
248
249
|
# File 'lib/varia_model.rb', line 247
def _attributes_
@_attributes_ ||= self.class.attributes.dup
end
|
#as_json ⇒ Hash
261
262
263
264
265
|
# File 'lib/varia_model.rb', line 261
def as_json(*)
opts = {}
opts[JSON.create_id] = self.class.name if JSON.create_id
to_hash.merge(opts)
end
|
#errors ⇒ Hashie::Mash
190
191
192
|
# File 'lib/varia_model.rb', line 190
def errors
@errors ||= Hashie::Mash.new
end
|
#from_hash(hash) ⇒ self
231
232
233
234
|
# File 'lib/varia_model.rb', line 231
def from_hash(hash)
mass_assign(hash.to_hash)
self
end
|
#from_json(data) ⇒ self
239
240
241
242
|
# File 'lib/varia_model.rb', line 239
def from_json(data)
mass_assign(JSON.parse(data))
self
end
|
#get_attribute(key) ⇒ Object
Also known as:
[]
216
217
218
|
# File 'lib/varia_model.rb', line 216
def get_attribute(key)
eval_as_proc(_attributes_.berks_dig(key.to_s))
end
|
#mass_assign(new_attrs = {}) ⇒ Object
Assigns the attributes of a model from a given hash of attributes.
If the assignment mode is set to ‘:whitelist`, then only the values of keys which have a corresponding attribute definition on the model will be set. All other keys will have their values ignored.
If the assignment mode is set to ‘:carefree`, then the attributes hash will be populated with any key/values that are provided.
204
205
206
207
208
209
210
211
|
# File 'lib/varia_model.rb', line 204
def mass_assign(new_attrs = {})
case self.class.assignment_mode
when :whitelist
whitelist_assign(new_attrs)
when :carefree
carefree_assign(new_attrs)
end
end
|
#set_attribute(key, value) ⇒ Object
Also known as:
[]=
223
224
225
|
# File 'lib/varia_model.rb', line 223
def set_attribute(key, value)
_attributes_.deep_merge!(Attributes.from_dotted_path(key.to_s, value))
end
|
#to_json(*options) ⇒ String
256
257
258
|
# File 'lib/varia_model.rb', line 256
def to_json(*options)
as_json.to_json(*options)
end
|
#valid? ⇒ Buff::Boolean
185
186
187
|
# File 'lib/varia_model.rb', line 185
def valid?
validate.empty?
end
|
#validate ⇒ Hashie::Mash
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
# File 'lib/varia_model.rb', line 164
def validate
self.class.validations.each do |attr_path, validations|
validations.each do |validation|
status, messages = validation.call(self, attr_path)
if status == :error
if messages.is_a?(Array)
messages.each do |message|
self.add_error(attr_path, message)
end
else
self.add_error(attr_path, messages)
end
end
end
end
self.errors
end
|