Class: RescueGroups::RemoteModel
- Inherits:
-
Object
- Object
- RescueGroups::RemoteModel
- Includes:
- ApiClient, Queryable, Relationable
- Defined in:
- lib/remote_model.rb
Instance Method Summary collapse
-
#attributes ⇒ Object
method: attributes purpose: Distill the included class’s attributes into a hash of keys and values If an attribute is nil, the key for that attribute is still included in the resulting hash and the value is nil param: none return: <Hash> of attributes from the included class.
- #cast_attribute_to_type(name, value) ⇒ Object
-
#initialize(attribute_hash = {}) ⇒ RemoteModel
constructor
method: initialize purpose: given a hash of attributes, assign the attributes that the included model has defined and discard the rest param: attribute_hash - <Hash> - hash of attributes to instantiate this model with return: nil.
Methods included from Relationable
Methods included from Queryable
Methods included from ApiClient
Constructor Details
#initialize(attribute_hash = {}) ⇒ RemoteModel
method: initialize purpose: given a hash of attributes, assign the attributes that the
included model has defined and discard the rest
param: attribute_hash - <Hash> - hash of attributes to instantiate
this model with
return: nil
18 19 20 21 22 23 24 25 |
# File 'lib/remote_model.rb', line 18 def initialize(attribute_hash = {}) (attribute_hash || {}).each do |key, value| mapped_key = self.class.object_fields::FIELDS.key(key.to_sym) mapped_method = "#{ mapped_key }=" next unless self.respond_to?(mapped_method) self.send(mapped_method, cast_attribute_to_type(mapped_key, value)) end end |
Instance Method Details
#attributes ⇒ Object
method: attributes purpose: Distill the included class’s attributes into a hash of keys and values
If an attribute is nil, the key for that attribute is still included
in the resulting hash and the value is nil
param: none return: <Hash> of attributes from the included class
39 40 41 42 43 44 45 |
# File 'lib/remote_model.rb', line 39 def attributes {}.tap do |hash| self.class.object_fields::FIELDS.keys.each do |attribute| hash[attribute] = instance_variable_get(:"@#{ attribute }") end end end |
#cast_attribute_to_type(name, value) ⇒ Object
27 28 29 30 31 |
# File 'lib/remote_model.rb', line 27 def cast_attribute_to_type(name, value) return value.to_i if name.to_s == 'id' || name =~ /_id/ value end |