Class: NoyoFulfillment::BaseModel
- Inherits:
-
Object
- Object
- NoyoFulfillment::BaseModel
- Defined in:
- lib/noyo_fulfillment/models/base_model.rb
Direct Known Subclasses
ApiResource, ApiResourceCollection, DemographicChange, NewHire, OpenEnrollment, Person, QualifyingLifeEvent, Termination
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#attributes(options = {}) ⇒ Object
Returns a hash of settable attributes, including deep hashes of children.
-
#excluded_attributes ⇒ Object
Probably should be overriden in a subclass, if there’s any excluded attributes from API interaction.
-
#initialize(options = nil) ⇒ BaseModel
constructor
A new instance of BaseModel.
- #inspect ⇒ Object
-
#synced_attributes ⇒ Object
only those that should be imported to and exported from the API.
- #to_h ⇒ Object
-
#update_attrs(updated_resource) ⇒ Object
Updates this instance’s variables with cooresponding ones on the updated_resource object.
Constructor Details
#initialize(options = nil) ⇒ BaseModel
Returns a new instance of BaseModel.
7 8 9 10 11 12 13 14 15 |
# File 'lib/noyo_fulfillment/models/base_model.rb', line 7 def initialize( = nil) return if .nil? .keys.each do |key| if respond_to?("#{key}=") send("#{key}=", [key]) end end end |
Class Method Details
.class_name ⇒ Object
3 4 5 |
# File 'lib/noyo_fulfillment/models/base_model.rb', line 3 def self.class_name name.split('::')[-1] end |
Instance Method Details
#==(other) ⇒ Object
99 100 101 |
# File 'lib/noyo_fulfillment/models/base_model.rb', line 99 def ==(other) !other.nil? && self.class.name == other.class.name && id == other.id end |
#attributes(options = {}) ⇒ Object
Returns a hash of settable attributes, including deep hashes of children
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/noyo_fulfillment/models/base_model.rb', line 69 def attributes( = {}) attrs = {} synced_attributes.each do |var| attr_name_str = var.to_s.gsub(/^@/, '') next if !respond_to?(attr_name_str) attr_value = send(attr_name_str) # get deep attrs, whether or not it's a part of an array if attr_value.respond_to?(:attributes) attr_value = attr_value.attributes() end if attr_value.is_a?(Array) attr_value = map_nested_attr(attr_value, ) end if !attr_value.nil? attrs[attr_name_str.to_sym] = attr_value end end attrs end |
#excluded_attributes ⇒ Object
Probably should be overriden in a subclass, if there’s any excluded attributes from API interaction
33 34 35 |
# File 'lib/noyo_fulfillment/models/base_model.rb', line 33 def excluded_attributes [ ] end |
#inspect ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/noyo_fulfillment/models/base_model.rb', line 17 def inspect string = "#<#{self.class.name}:#{object_id} " fields = [] id_field = :@id if instance_variables.include?(id_field) fields << "#{id_field}: #{send(:id)}" end non_id_fields = instance_variables.reject{ |x| x == id_field } fields += non_id_fields.map{ |field| "#{field}: #{send(field.to_s.tr('@', ''))}" } string << fields.join(', ') << '>' end |
#synced_attributes ⇒ Object
only those that should be imported to and exported from the API
38 39 40 41 42 |
# File 'lib/noyo_fulfillment/models/base_model.rb', line 38 def synced_attributes exclusions = excluded_attributes.dup exclusions.map!{ |item| "@#{item}".to_sym } instance_variables - exclusions end |
#to_h ⇒ Object
95 96 97 |
# File 'lib/noyo_fulfillment/models/base_model.rb', line 95 def to_h Hash[instance_variables.map{ |var| [ var.to_s[1..-1], instance_variable_get(var) ] }] end |
#update_attrs(updated_resource) ⇒ Object
Updates this instance’s variables with cooresponding ones on the updated_resource object. Does so deeply.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/noyo_fulfillment/models/base_model.rb', line 46 def update_attrs(updated_resource) attributes_set = synced_attributes.to_set attributes_set.merge(updated_resource.synced_attributes) attributes_set.each do |var| str = var.to_s.gsub(/^@/, '') if !respond_to?("#{str}=") || !updated_resource.respond_to?(str) || updated_resource.send(str).nil? next end the_attr = send(str) # get deep attrs, whether or not it's a part of an array if the_attr.respond_to?(:update_attrs) the_attr.update_attrs(updated_resource.send(str)) else instance_variable_set(var, updated_resource.send(str)) end end end |