Class: Xero::Models::BaseModel

Inherits:
Object
  • Object
show all
Includes:
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.



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

def initialize(attributes = {})
  @new_record = true
  self.attributes = attributes
end

Class Attribute Details

.pathObject

Returns the value of attribute path.



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

def path
  @path
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



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

def client
  @client
end

#new_recordObject

Returns the value of attribute new_record.



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

def new_record
  @new_record
end

Instance Method Details

#attributes=(attrs) ⇒ Object



20
21
22
# File 'lib/xero/models/base_model.rb', line 20

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

#cleanup_hash(hash) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/xero/models/base_model.rb', line 41

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)


52
53
54
# File 'lib/xero/models/base_model.rb', line 52

def persisted?
  id && !@new_record
end

#saveObject

Raises:

  • (StandardError)


24
25
26
27
# File 'lib/xero/models/base_model.rb', line 24

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

#xero_attributes(attrs = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/xero/models/base_model.rb', line 29

def xero_attributes(attrs = nil)
  attrs ||= attributes.clone
  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
end