Class: Xeroizer::Record::Base
- Inherits:
-
Object
- Object
- Xeroizer::Record::Base
- Includes:
- ClassLevelInheritableAttributes, ModelDefinitionHelper, RecordAssociationHelper, ValidationHelper, XmlHelper
- Defined in:
- lib/xeroizer/record/base.rb
Direct Known Subclasses
Account, Address, Allocation, BankAccount, BankTransaction, BrandingTheme, Contact, ContactGroup, CreditNote, Currency, Employee, ExternalLink, Invoice, Item, ItemPurchaseDetails, ItemSalesDetails, Journal, JournalLine, JournalLineTrackingCategory, LineItem, ManualJournal, ManualJournalLine, Option, Organisation, Payment, Phone, TaxRate, TrackingCategory, TrackingCategoryChild
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#complete_record_downloaded ⇒ Object
Returns the value of attribute complete_record_downloaded.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Class Method Summary collapse
-
.build(attributes, parent) ⇒ Object
Build a record with attributes set to the value of attributes.
Instance Method Summary collapse
- #[](attribute) ⇒ Object
- #[]=(attribute, value) ⇒ Object
-
#as_json(options = {}) ⇒ Object
Deprecated.
-
#complete_record_downloaded? ⇒ Boolean
Check to see if the complete record is downloaded.
-
#download_complete_record! ⇒ Object
Downloads the complete record if we only have a summary of the record.
-
#initialize(parent) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
- #new_model_class(model_name) ⇒ Object
- #new_record? ⇒ Boolean
- #non_calculated_attributes ⇒ Object
- #save ⇒ Object
- #saved! ⇒ Object
- #to_h ⇒ Object
- #to_json(*args) ⇒ Object
- #update_attributes(attributes) ⇒ Object
Methods included from XmlHelper
Methods included from ValidationHelper
Methods included from RecordAssociationHelper
Methods included from ModelDefinitionHelper
Methods included from ClassLevelInheritableAttributes
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
#attributes ⇒ Object
Returns the value of attribute attributes.
15 16 17 |
# File 'lib/xeroizer/record/base.rb', line 15 def attributes @attributes end |
#complete_record_downloaded ⇒ Object
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 |
#errors ⇒ Object
Returns the value of attribute errors.
18 19 20 |
# File 'lib/xeroizer/record/base.rb', line 18 def errors @errors end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
17 18 19 |
# File 'lib/xeroizer/record/base.rb', line 17 def model @model end |
#parent ⇒ Object (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( = {}) to_h.to_json end |
#complete_record_downloaded? ⇒ Boolean
Check to see if the complete record is downloaded.
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 |
#inspect ⇒ Object
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
81 82 83 |
# File 'lib/xeroizer/record/base.rb', line 81 def new_record? id.nil? end |
#non_calculated_attributes ⇒ Object
64 65 66 |
# File 'lib/xeroizer/record/base.rb', line 64 def non_calculated_attributes attributes.reject {|name| self.class.fields[name][:calculated] } end |
#save ⇒ Object
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_h ⇒ Object
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 |