Class: Xero::Models::BaseModel

Inherits:
Object
  • Object
show all
Includes:
ActiveAttr::Attributes, ActiveAttr::Model, Associations
Defined in:
lib/xero/models/base_model.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ BaseModel

Returns a new instance of BaseModel.



16
17
18
19
# File 'lib/xero/models/base_model.rb', line 16

def initialize(attributes = {})
  super(cleanup_hash(attributes))
  @new_record = true
end

Class Attribute Details

.pathObject

Returns the value of attribute path.



9
10
11
# File 'lib/xero/models/base_model.rb', line 9

def path
  @path
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



14
15
16
# File 'lib/xero/models/base_model.rb', line 14

def client
  @client
end

#new_recordObject

Returns the value of attribute new_record.



14
15
16
# File 'lib/xero/models/base_model.rb', line 14

def new_record
  @new_record
end

Instance Method Details

#attributes=(attrs) ⇒ Object



21
22
23
24
25
# File 'lib/xero/models/base_model.rb', line 21

def attributes=(attrs)
  cleanup_hash(attrs).each do |key, value|
    send("#{key}=", value) unless value.blank?
  end
end

#cleanup_hash(hash) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/xero/models/base_model.rb', line 45

def cleanup_hash(hash)
  hash.keys.each do |key|
    value = hash.delete(key)
    value = cleanup_hash(value) if value.is_a?(Hash)
    key = key.to_s.underscore
    key = 'id' if key.match(/_id/)
    hash[key] = value
  end
  hash
end

#persisted?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/xero/models/base_model.rb', line 56

def persisted?
  id && !@new_record
end

#saveObject

Raises:

  • (StandardError)


27
28
29
30
# File 'lib/xero/models/base_model.rb', line 27

def save
  raise StandardError if self.client.blank?
  self.client.save(self)
end

#xero_attributes(attrs = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/xero/models/base_model.rb', line 32

def xero_attributes(attrs = nil)
  attrs ||= attributes.clone
  id = attrs.delete('id')
  attrs.delete('updated_date_utc')
  attrs.keys.each do |key|
    value = attrs.delete(key)
    value = xero_attributes(value) if value.is_a?(Hash)
    attrs[key.to_s.camelize] = value.to_s unless value.blank?
  end
  attrs.merge!("#{self.class.to_s.demodulize}ID" => id) unless id.blank?
  attrs
end