class Quickbooks::Model::BaseModel

Public Class Methods

attribute_names() click to toggle source
# File lib/quickbooks/model/base_model.rb, line 65
def attribute_names
  roxml_attrs.map(&:accessor)
end
attrs_with_types() click to toggle source
# File lib/quickbooks/model/base_model.rb, line 111
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() click to toggle source
# File lib/quickbooks/model/base_model.rb, line 107
def inspect
  "#{super}(#{attrs_with_types.join " "})"
end
new(attributes={}) click to toggle source
# File lib/quickbooks/model/base_model.rb, line 11
def initialize(attributes={})
  attributes.each {|key, value| public_send("#{key}=", value) }
end
reference_attrs() click to toggle source
# File lib/quickbooks/model/base_model.rb, line 103
def reference_attrs
  matches = roxml_attrs.select{|attr| attr.sought_type == Quickbooks::Model::BaseReference}.map{|attr| attr.accessor}
end
reference_setters(*args) click to toggle source

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

# File lib/quickbooks/model/base_model.rb, line 85
        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 = <<-METH
              def #{method_name}(id)
                self.#{attribute} = BaseReference.new(id)
              end
              METH
              class_eval(method_definition)
            end
          end
        end
resource_for_collection() click to toggle source

These can be over-ridden in each model object as needed

# File lib/quickbooks/model/base_model.rb, line 70
def resource_for_collection
  self::REST_RESOURCE
end
resource_for_singular() click to toggle source
# File lib/quickbooks/model/base_model.rb, line 74
def resource_for_singular
  self::REST_RESOURCE
end
to_xml_big_decimal() click to toggle source
# File lib/quickbooks/model/base_model.rb, line 61
def to_xml_big_decimal
  Proc.new { |val| val.nil? ? nil : val.to_f }
end

Public Instance Methods

as_json(options = nil) click to toggle source
Calls superclass method
# File lib/quickbooks/model/base_model.rb, line 30
def as_json(options = nil)
  options = {} if options.nil?
  except_conditions = ["roxml_references"]
  except_conditions << options[:except]
  options[:except] = except_conditions.flatten.uniq.compact
  super(options)
end
attributes() click to toggle source
# File lib/quickbooks/model/base_model.rb, line 44
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() click to toggle source
# File lib/quickbooks/model/base_model.rb, line 54
def inspect
  # it would be nice if we could inspect all the children,
  # but it's likely to blow the stack in some cases
  "#<#{self.class} " + 
  "#{attributes.map{|k,v| "#{k}: #{v.nil? ? 'nil' : v.to_s }"}.join ", "}>"
end
to_xml_inject_ns(model_name, options = {}) click to toggle source

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

# File lib/quickbooks/model/base_model.rb, line 17
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 = {}) click to toggle source
# File lib/quickbooks/model/base_model.rb, line 38
def to_xml_ns(options = {})
  to_xml_inject_ns(self.class::XML_NODE, options)
end