Class: Xeroizer::Record::Base

Inherits:
Object
  • Object
show all
Includes:
ClassLevelInheritableAttributes, ModelDefinitionHelper, RecordAssociationHelper, ValidationHelper, XmlHelper
Defined in:
lib/xeroizer/record/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from XmlHelper

included

Methods included from ValidationHelper

included

Methods included from RecordAssociationHelper

included

Methods included from ModelDefinitionHelper

included

Methods included from ClassLevelInheritableAttributes

included

Constructor Details

#initialize(parent) ⇒ Base

Returns a new instance of Base.



41
42
43
44
45
# File 'lib/xeroizer/record/base.rb', line 41

def initialize(parent)
  @parent = parent
  @model = new_model_class(self.class.name.demodulize)
  @attributes = {}
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



15
16
17
# File 'lib/xeroizer/record/base.rb', line 15

def attributes
  @attributes
end

#complete_record_downloadedObject

Returns the value of attribute complete_record_downloaded.



19
20
21
# File 'lib/xeroizer/record/base.rb', line 19

def complete_record_downloaded
  @complete_record_downloaded
end

#errorsObject

Returns the value of attribute errors.



18
19
20
# File 'lib/xeroizer/record/base.rb', line 18

def errors
  @errors
end

#modelObject (readonly)

Returns the value of attribute model.



17
18
19
# File 'lib/xeroizer/record/base.rb', line 17

def model
  @model
end

#parentObject (readonly)

Returns the value of attribute parent.



16
17
18
# File 'lib/xeroizer/record/base.rb', line 16

def parent
  @parent
end

Class Method Details

.build(attributes, parent) ⇒ Object

Build a record with attributes set to the value of attributes.



29
30
31
32
33
34
35
# File 'lib/xeroizer/record/base.rb', line 29

def build(attributes, parent)
  record = new(parent)
  attributes.each do | key, value |
    record.send("#{key}=".to_sym, value)
  end
  record
end

Instance Method Details

#[](attribute) ⇒ Object



55
56
57
# File 'lib/xeroizer/record/base.rb', line 55

def [](attribute)
  self.send(attribute)
end

#[]=(attribute, value) ⇒ Object



59
60
61
62
# File 'lib/xeroizer/record/base.rb', line 59

def []=(attribute, value)
  parent.mark_dirty(self) if parent
  self.send("#{attribute}=".to_sym, value)
end

#as_json(options = {}) ⇒ Object

Deprecated



124
125
126
# File 'lib/xeroizer/record/base.rb', line 124

def as_json(options = {})
  to_h.to_json
end

#complete_record_downloaded?Boolean

Check to see if the complete record is downloaded.

Returns:

  • (Boolean)


86
87
88
89
90
91
92
# File 'lib/xeroizer/record/base.rb', line 86

def complete_record_downloaded?
  if !!self.class.list_contains_summary_only?
    !!complete_record_downloaded
  else
    true
  end
end

#download_complete_record!Object

Downloads the complete record if we only have a summary of the record.



95
96
97
98
99
100
101
# File 'lib/xeroizer/record/base.rb', line 95

def download_complete_record!
  record = self.parent.find(self.id)
  @attributes = record.attributes if record
  @complete_record_downloaded = true
  parent.mark_clean(self)
  self
end

#inspectObject



135
136
137
138
139
140
# File 'lib/xeroizer/record/base.rb', line 135

def inspect
  attribute_string = self.attributes.collect do |attr, value|
    "#{attr.inspect}: #{value.inspect}"
  end.join(", ")
  "#<#{self.class} #{attribute_string}>"
end

#new_model_class(model_name) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/xeroizer/record/base.rb', line 47

def new_model_class(model_name)
  if parent
    Xeroizer::Record.const_get("#{model_name}Model".to_sym).new(parent.application, model_name.to_s)
  else
    Xeroizer::Record.const_get("#{model_name}Model".to_sym).new(nil, model_name.to_s)
  end
end

#new_record?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/xeroizer/record/base.rb', line 81

def new_record?
  id.nil?
end

#non_calculated_attributesObject



64
65
66
# File 'lib/xeroizer/record/base.rb', line 64

def non_calculated_attributes
  attributes.reject {|name| self.class.fields[name][:calculated] }
end

#saveObject



103
104
105
106
107
108
109
110
111
# File 'lib/xeroizer/record/base.rb', line 103

def save
  return false unless valid?
  if new_record?
    create
  else
    update
  end
  saved!
end

#saved!Object



113
114
115
116
117
# File 'lib/xeroizer/record/base.rb', line 113

def saved!
  @complete_record_downloaded = true
  parent.mark_clean(self)
  true
end

#to_hObject



128
129
130
131
132
133
# File 'lib/xeroizer/record/base.rb', line 128

def to_h
  attrs = self.attributes.reject {|k, v| k == :parent }.map do |k, v|
    [k, v.kind_of?(Array) ? v.map(&:to_h) : (v.respond_to?(:to_h) ? v.to_h : v)]
  end
  Hash[attrs]
end

#to_json(*args) ⇒ Object



119
120
121
# File 'lib/xeroizer/record/base.rb', line 119

def to_json(*args)
  to_h.to_json(*args)
end

#update_attributes(attributes) ⇒ Object



76
77
78
79
# File 'lib/xeroizer/record/base.rb', line 76

def update_attributes(attributes)
  self.attributes = attributes
  save
end