Class: NoyoFulfillment::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/noyo_fulfillment/models/base_model.rb

Class Method Summary collapse

Instance Method Summary collapse

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(options = nil)
  return if options.nil?

  options.keys.each do |key|
    if respond_to?("#{key}=")
      send("#{key}=", options[key])
    end
  end
end

Class Method Details

.class_nameObject



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(options = {})
  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(options)
    end

    if attr_value.is_a?(Array)
      attr_value = map_nested_attr(attr_value, options)
    end

    if !attr_value.nil?
      attrs[attr_name_str.to_sym] = attr_value
    end
  end

  attrs
end

#excluded_attributesObject

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

#inspectObject



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_attributesObject

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_hObject



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