Class: Quickbooks::Model::BaseModel
- Inherits:
-
Object
- Object
- Quickbooks::Model::BaseModel
show all
- Includes:
- ActiveModel::Validations, ROXML, Validator
- Defined in:
- lib/quickbooks/model/base_model.rb
Direct Known Subclasses
AccessTokenResponse, Account, AccountBasedExpenseLineDetail, BaseReference, BatchRequest, BatchResponse, Bill, BillLineItem, BillPayment, BillPaymentCheck, BillPaymentCreditCard, BillPaymentLineItem, CheckPayment, Class, CompanyInfo, CreditCardPayment, CreditMemo, CustomField, Customer, Department, DiscountLineDetail, DiscountOverride, EmailAddress, Employee, Entity, Estimate, Fault, GroupLineDetail, Invoice, InvoiceLineItem, Item, ItemBasedExpenseLineDetail, JournalEntry, JournalEntryLineDetail, Line, LinkedTransaction, MarkupInfo, MetaData, OtherContactInfo, Payment, PaymentLineDetail, PaymentMethod, PhysicalAddress, Preferences, Purchase, PurchaseLineItem, PurchaseOrder, RefundReceipt, SalesItemLineDetail, SalesReceipt, SalesTaxRateList, SubTotalLineDetail, TaxCode, TaxLine, TaxLineDetail, TaxRate, TaxRateDetail, TelephoneNumber, Term, TimeActivity, TransactionTaxDetail, Vendor, VendorCredit, WebSiteAddress
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Validator
#line_item_size
Constructor Details
#initialize(attributes = {}) ⇒ BaseModel
Returns a new instance of BaseModel.
10
11
12
|
# File 'lib/quickbooks/model/base_model.rb', line 10
def initialize(attributes={})
attributes.each {|key, value| public_send("#{key}=", value) }
end
|
Class Method Details
.attribute_names ⇒ Object
60
61
62
|
# File 'lib/quickbooks/model/base_model.rb', line 60
def attribute_names
roxml_attrs.map(&:accessor)
end
|
.attrs_with_types ⇒ Object
97
98
99
100
101
102
|
# File 'lib/quickbooks/model/base_model.rb', line 97
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
94
95
96
|
# File 'lib/quickbooks/model/base_model.rb', line 94
def inspect
"#{super}(#{attrs_with_types.join " "})"
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
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/quickbooks/model/base_model.rb', line 80
def reference_setters(*args)
args.each do |attribute|
method_name = "#{attribute.to_s.gsub('_ref', '_id')}=".to_sym
unless instance_methods(false).include?(method_name)
method_definition = <<-METH
def #{method_name}(id)
self.#{attribute} = BaseReference.new(id)
end
METH
class_eval(method_definition)
end
end
end
|
.resource_for_collection ⇒ Object
These can be over-ridden in each model object as needed
65
66
67
|
# File 'lib/quickbooks/model/base_model.rb', line 65
def resource_for_collection
self::REST_RESOURCE
end
|
.resource_for_singular ⇒ Object
69
70
71
|
# File 'lib/quickbooks/model/base_model.rb', line 69
def resource_for_singular
self::REST_RESOURCE
end
|
.to_xml_big_decimal ⇒ Object
56
57
58
|
# File 'lib/quickbooks/model/base_model.rb', line 56
def to_xml_big_decimal
Proc.new { |val| val.nil? ? nil : val.to_f }
end
|
Instance Method Details
#attributes ⇒ Object
35
36
37
38
39
40
41
42
43
|
# File 'lib/quickbooks/model/base_model.rb', line 35
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
|
#ensure_line_items_initialization ⇒ Object
45
46
47
|
# File 'lib/quickbooks/model/base_model.rb', line 45
def ensure_line_items_initialization
self.line_items ||= []
end
|
#inspect ⇒ Object
49
50
51
52
53
54
|
# File 'lib/quickbooks/model/base_model.rb', line 49
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
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/quickbooks/model/base_model.rb', line 16
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
29
30
31
|
# File 'lib/quickbooks/model/base_model.rb', line 29
def to_xml_ns(options = {})
to_xml_inject_ns(self.class::XML_NODE, options)
end
|