Class: Quickbooks::Model::BaseModel
- Inherits:
-
Object
- Object
- Quickbooks::Model::BaseModel
show all
- Includes:
- ActiveModel::AttributeMethods, ActiveModel::Validations, Definition, Validator, ROXML
- Defined in:
- lib/quickbooks/model/base_model.rb
Direct Known Subclasses
AccessTokenResponse, Account, AccountBasedExpenseLineDetail, Attachable, AttachableRef, BaseModelJSON, BaseReference, BatchRequest, BatchResponse, Bill, BillLineItem, BillPayment, BillPaymentCheck, BillPaymentCreditCard, BillPaymentLineItem, Budget, BudgetLineItem, ChangeDataCapture, ChangeModel, CheckPayment, Class, CompanyCurrency, CompanyInfo, CreditCardPayment, CreditMemo, CustomField, Customer, CustomerType, DeliveryInfo, Department, Deposit, DepositLineDetail, DepositLineItem, DescriptionLineDetail, DiscountLineDetail, DiscountOverride, EffectiveTaxRate, EmailAddress, Employee, Entity, EntityRef, Estimate, ExchangeRate, Fault, GroupLineDetail, Invoice, InvoiceLineItem, Item, ItemBasedExpenseLineDetail, ItemGroupDetail, ItemGroupLine, JournalEntry, JournalEntryLineDetail, Line, LineEx, LinkedTransaction, MarkupInfo, MetaData, NameValue, OtherContactInfo, Payment, PaymentLineDetail, PaymentMethod, PhysicalAddress, Preferences, Purchase, PurchaseLineItem, PurchaseOrder, PurchaseTaxRateList, RefundReceipt, Report, SalesItemLineDetail, SalesReceipt, SalesTaxRateList, SubTotalLineDetail, TaxAgency, TaxCode, TaxLine, TaxLineDetail, TaxRate, TaxRateDetail, TaxRateDetailLine, TelephoneNumber, Term, TimeActivity, TransactionTaxDetail, Transfer, Upload, Vendor, VendorCredit, WebSiteAddress
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Validator
#line_item_size
Methods included from Definition
included, #is_name_list_entity?, #is_transaction_entity?
Constructor Details
#initialize(attributes = {}) ⇒ BaseModel
Returns a new instance of BaseModel.
12
13
14
|
# File 'lib/quickbooks/model/base_model.rb', line 12
def initialize(attributes={})
attributes.each {|key, value| public_send("#{key}=", value) }
end
|
Class Method Details
.attribute_names ⇒ Object
67
68
69
|
# File 'lib/quickbooks/model/base_model.rb', line 67
def attribute_names
roxml_attrs.map(&:accessor)
end
|
.attrs_with_types ⇒ Object
113
114
115
116
117
118
|
# File 'lib/quickbooks/model/base_model.rb', line 113
def attrs_with_types
roxml_attrs.map do |attr|
"#{attr.accessor}:" +
"#{attr.class.block_shorthands.invert[attr.blocks.last]}:#{attr.sought_type}"
end
end
|
.inspect ⇒ Object
109
110
111
|
# File 'lib/quickbooks/model/base_model.rb', line 109
def inspect
"#{super}(#{attrs_with_types.join " "})"
end
|
.reference_attrs ⇒ Object
105
106
107
|
# File 'lib/quickbooks/model/base_model.rb', line 105
def reference_attrs
matches = roxml_attrs.select{|attr| attr.sought_type == Quickbooks::Model::BaseReference}.map{|attr| attr.accessor}
end
|
.reference_setters(*args) ⇒ Object
Automatically generate an ID setter. Example:
reference_setters :discount_ref
Would generate a method like: def discount_id=(id)
self.discount_ref = BaseReference.new(id)
end
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/quickbooks/model/base_model.rb', line 87
def reference_setters(*args)
references = args.empty? ? reference_attrs : args
references.each do |attribute|
last_index = attribute.to_s.rindex('_ref')
field_name = !last_index.nil? ? attribute.to_s.slice(0, last_index) : attribute
method_name = "#{field_name}_id=".to_sym
unless instance_methods(false).include?(method_name)
method_definition = " def \#{method_name}(id)\n self.\#{attribute} = BaseReference.new(id)\n end\n METH\n class_eval(method_definition)\n end\n end\nend\n"
|
.resource_for_collection ⇒ Object
These can be over-ridden in each model object as needed
72
73
74
|
# File 'lib/quickbooks/model/base_model.rb', line 72
def resource_for_collection
self::REST_RESOURCE
end
|
.resource_for_singular ⇒ Object
76
77
78
|
# File 'lib/quickbooks/model/base_model.rb', line 76
def resource_for_singular
self::REST_RESOURCE
end
|
.to_xml_big_decimal ⇒ Object
63
64
65
|
# File 'lib/quickbooks/model/base_model.rb', line 63
def to_xml_big_decimal
Proc.new { |val| val.nil? ? nil : val.to_f }
end
|
Instance Method Details
#as_json(options = nil) ⇒ Object
31
32
33
34
35
36
37
38
|
# File 'lib/quickbooks/model/base_model.rb', line 31
def as_json(options = nil)
options = {} if options.nil?
options = options.dup if options.frozen?
except_conditions = ["roxml_references"]
except_conditions << options[:except]
options[:except] = except_conditions.flatten.uniq.compact
super(options)
end
|
#attributes ⇒ Object
46
47
48
49
50
51
52
53
54
|
# File 'lib/quickbooks/model/base_model.rb', line 46
def attributes
attributes = self.class.attribute_names.map do |name|
value = public_send(name)
value = value.attributes if value.respond_to?(:attributes)
[name, value]
end
HashWithIndifferentAccess[attributes]
end
|
#inspect ⇒ Object
56
57
58
59
60
61
|
# File 'lib/quickbooks/model/base_model.rb', line 56
def inspect
"#<#{self.class} " +
"#{attributes.map{|k,v| "#{k}: #{v.nil? ? 'nil' : v.to_s }"}.join ", "}>"
end
|
#to_xml_inject_ns(model_name, options = {}) ⇒ Object
ROXML doesnt insert the namespaces into generated XML so we need to do it ourselves insert the static namespaces in the first opening tag that matches the model_name
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/quickbooks/model/base_model.rb', line 18
def to_xml_inject_ns(model_name, options = {})
s = StringIO.new
xml = to_xml(options).write_to(s, :indent => 0, :indent_text => '')
destination_name = options.fetch(:destination_name, nil)
destination_name ||= model_name
sparse = options.fetch(:sparse, false)
sparse_string = %{sparse="#{sparse}"}
step1 = s.string.sub("<#{model_name}>", "<#{destination_name} #{Quickbooks::Service::BaseService::XML_NS} #{sparse_string}>")
step2 = step1.sub("</#{model_name}>", "</#{destination_name}>")
step2
end
|
#to_xml_ns(options = {}) ⇒ Object
40
41
42
|
# File 'lib/quickbooks/model/base_model.rb', line 40
def to_xml_ns(options = {})
to_xml_inject_ns(self.class::XML_NODE, options)
end
|