Class: Quickbooks::QbxmlBase

Inherits:
Object
  • Object
show all
Extended by:
Support, Support::API
Includes:
Support, Support::QBXML
Defined in:
lib/quickbooks/qbxml_base.rb

Overview

inheritance base for schema classes

Constant Summary collapse

QB_TYPE_CONVERSION_MAP =
{
  "AMTTYPE"          => lambda {|d| d ? Float(d) : 0.0 },
  #"BOOLTYPE"         => lambda {|d| d ? (d == 'True' ? true : false) : false },
  "BOOLTYPE"         => lambda {|d| d ? String(d) : '' },
  "DATETIMETYPE"     => lambda {|d| d ? Time.parse(d).xmlschema : Time.now.xmlschema },
  "DATETYPE"         => lambda {|d| d ? Date.parse(d).xmlschema : Date.today.xmlschema },
  "ENUMTYPE"         => lambda {|d| d ? String(d) : ''},
  "FLOATTYPE"        => lambda {|d| d ? Float(d) : 0.0},
  "GUIDTYPE"         => lambda {|d| d ? String(d) : ''},
  "IDTYPE"           => lambda {|d| d ? String(d) : ''},
  "INTTYPE"          => lambda {|d| d ? Integer(d) : 0 },
  "PERCENTTYPE"      => lambda {|d| d ? Float(d) : 0.0 },
  "PRICETYPE"        => lambda {|d| d ? Float(d) : 0.0 },
  "QUANTYPE"         => lambda {|d| d ? Integer(d.to_i) : 0 },
  "STRTYPE"          => lambda {|d| d ? String(d) : ''},
  "TIMEINTERVALTYPE" => lambda {|d| d ? String(d) : ''}
}

Constants included from Support::API

Support::API::API_ROOT, Support::API::DEFAULT_LOG_LEVEL, Support::API::RUBY_SCHEMA_PATH, Support::API::SCHEMA_MAP, Support::API::XML_SCHEMA_PATH

Constants included from Support::QBXML

Support::QBXML::COMMENT_END, Support::QBXML::COMMENT_MATCHER, Support::QBXML::COMMENT_START, Support::QBXML::XML_COMMENT, Support::QBXML::XML_DOCUMENT, Support::QBXML::XML_ELEMENT, Support::QBXML::XML_NODE, Support::QBXML::XML_NODE_SET, Support::QBXML::XML_TEXT

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support

inflector, log, simple_class_name, to_attribute_name

Methods included from Support::QBXML

#cleanup_qbxml, #is_leaf_node?, #qbxml_class_defined?, #set_required_attributes

Constructor Details

#initialize(params = nil) ⇒ QbxmlBase

Returns a new instance of QbxmlBase.



28
29
30
31
32
33
34
35
# File 'lib/quickbooks/qbxml_base.rb', line 28

def initialize(params = nil)
  return unless params.is_a?(Hash)
  params.each do |k,v|
    if self.respond_to?(k)
      self.send("#{k}=", v)
    end
  end
end

Class Method Details

.attribute_namesObject



76
77
78
# File 'lib/quickbooks/qbxml_base.rb', line 76

def self.attribute_names
  instance_methods(false).reject { |m| m[-1..-1] == '=' || m =~ /_xml_class/} 
end

.template(recursive = false, use_disk_cache = false, reload = false) ⇒ Object



68
69
70
71
72
73
# File 'lib/quickbooks/qbxml_base.rb', line 68

def self.template(recursive = false, use_disk_cache = false, reload = false)
  if recursive
    @template = (!reload && @template) || load_template(true, use_disk_cache)
  else build_template(false)
  end
end

Instance Method Details

#attributes(recursive = true) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/quickbooks/qbxml_base.rb', line 98

def attributes(recursive = true)
  self.class.attribute_names.inject({}) do |h, m|
    val = self.send(m)
    if val
      unless recursive
        h[m] = val
      else
        h[m] = nested_attributes(val)
      end
    end; h
  end
end

#inner_attributesObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/quickbooks/qbxml_base.rb', line 81

def inner_attributes
  top_level_attrs = \
    self.class.attribute_names.inject({}) do |h, m|
      h[m] = self.send(m); h
    end
  
  values = top_level_attrs.values.compact
  if values.empty?
    {}
  elsif values.size > 1 || values.first.is_a?(Array)
    attributes
  else
    values.first.inner_attributes
  end
end

#to_qbxmlObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/quickbooks/qbxml_base.rb', line 38

def to_qbxml
  xml_doc = Nokogiri::XML(self.class.xml_template)
  root = xml_doc.root
  log.debug "to_qbxml#nodes_size: #{root.children.size}"

  # replace all children nodes of the template with populated data nodes
  xml_nodes = []
  root.children.each do |xml_template|
    next unless xml_template.is_a? XML_ELEMENT
    attr_name = to_attribute_name(xml_template)
    log.debug "to_qbxml#attr_name: #{attr_name}"

    val = self.send(attr_name)
    next unless val

    case val
    when Array
      xml_nodes += build_qbxml_nodes(xml_template, val)
    else
      xml_nodes << build_qbxml_node(xml_template, val)
    end
    log.debug "to_qbxml#val: #{val}"
  end

  log.debug "to_qbxml#xml_nodes_size: #{xml_nodes.size}"
  root.children = xml_nodes.join('')
  root.to_s
end