Class: Quickbooks::Model::BaseModel
- Inherits:
-
Object
- Object
- Quickbooks::Model::BaseModel
- Includes:
- ActiveModel::Validations, ROXML
- Defined in:
- lib/quickbooks/model/base_model.rb
Direct Known Subclasses
Account, AccountBasedExpenseLineDetail, BaseReference, Bill, BillLineItem, BillPayment, BillPaymentCheck, BillPaymentCreditCard, BillPaymentLineItem, CheckPayment, CompanyInfo, CreditCardPayment, CreditMemo, CustomField, Customer, DiscountOverride, EmailAddress, Employee, Estimate, GroupLineDetail, Invoice, InvoiceLineItem, Item, ItemBasedExpenseLineDetail, Line, LinkedTransaction, MarkupInfo, MetaData, OtherContactInfo, Payment, PaymentLineDetail, PaymentMethod, PhysicalAddress, Purchase, PurchaseLineItem, PurchaseOrder, SalesItemLineDetail, SalesReceipt, SubTotalLineDetail, TaxLine, TaxLineDetail, TelephoneNumber, Term, TransactionTaxDetail, Vendor, VendorCredit, WebSiteAddress
Class Method Summary collapse
- .attribute_names ⇒ Object
-
.reference_setters(*args) ⇒ Object
Automatically generate an ID setter.
-
.resource_for_collection ⇒ Object
These can be over-ridden in each model object as needed.
- .resource_for_singular ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
-
#initialize(attributes = {}) ⇒ BaseModel
constructor
A new instance of BaseModel.
-
#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. - #to_xml_ns(options = {}) ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ BaseModel
Returns a new instance of BaseModel.
9 10 11 |
# File 'lib/quickbooks/model/base_model.rb', line 9 def initialize(attributes={}) attributes.each {|key, value| public_send("#{key}=", value) } end |
Class Method Details
.attribute_names ⇒ Object
45 46 47 |
# File 'lib/quickbooks/model/base_model.rb', line 45 def attribute_names roxml_attrs.map(&: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
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/quickbooks/model/base_model.rb', line 65 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
50 51 52 |
# File 'lib/quickbooks/model/base_model.rb', line 50 def resource_for_collection self::REST_RESOURCE end |
.resource_for_singular ⇒ Object
54 55 56 |
# File 'lib/quickbooks/model/base_model.rb', line 54 def resource_for_singular self::REST_RESOURCE end |
Instance Method Details
#attributes ⇒ Object
34 35 36 37 38 39 40 41 42 |
# File 'lib/quickbooks/model/base_model.rb', line 34 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 |
#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
15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/quickbooks/model/base_model.rb', line 15 def to_xml_inject_ns(model_name, = {}) s = StringIO.new xml = to_xml().write_to(s, :indent => 0, :indent_text => '') destination_name = .fetch(:destination_name, nil) destination_name ||= model_name sparse = .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
28 29 30 |
# File 'lib/quickbooks/model/base_model.rb', line 28 def to_xml_ns( = {}) to_xml_inject_ns(self.class::XML_NODE, ) end |