Class: Quickbooks::QbxmlBase
- Inherits:
-
Object
- Object
- Quickbooks::QbxmlBase
- 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| String(d)}, "BOOLTYPE" => lambda {|d| String(d)}, "DATETIMETYPE" => lambda {|d| String(d)}, "DATETYPE" => lambda {|d| String(d)}, "ENUMTYPE" => lambda {|d| String(d)}, "FLOATTYPE" => lambda {|d| String(d)}, "GUIDTYPE" => lambda {|d| String(d)}, "IDTYPE" => lambda {|d| String(d)}, "INTTYPE" => lambda {|d| String(d)}, "PERCENTTYPE" => lambda {|d| String(d)}, "PRICETYPE" => lambda {|d| String(d)}, "QUANTYPE" => lambda {|d| String(d)}, "STRTYPE" => lambda {|d| String(d)}, "TIMEINTERVALTYPE" => lambda {|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
- #attributes(recursive = true) ⇒ Object
-
#initialize(params = nil) ⇒ QbxmlBase
constructor
A new instance of QbxmlBase.
- #inner_attributes ⇒ Object
- #to_qbxml ⇒ Object
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?
Constructor Details
#initialize(params = nil) ⇒ QbxmlBase
Returns a new instance of QbxmlBase.
44 45 46 47 48 49 50 51 |
# File 'lib/quickbooks/qbxml_base.rb', line 44 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_names ⇒ Object
92 93 94 |
# File 'lib/quickbooks/qbxml_base.rb', line 92 def self.attribute_names instance_methods(false).reject { |m| m[-1..-1] == '=' || m =~ /_xml_class/} end |
.template(recursive = false, use_disk_cache = false) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/quickbooks/qbxml_base.rb', line 84 def self.template(recursive = false, use_disk_cache = false) if recursive @template ||= load_template(true, use_disk_cache) else build_template(false) end end |
Instance Method Details
#attributes(recursive = true) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/quickbooks/qbxml_base.rb', line 112 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_attributes ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/quickbooks/qbxml_base.rb', line 97 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.size > 1 || values.first.is_a?(Array) attributes else values.first.inner_attributes end end |
#to_qbxml ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/quickbooks/qbxml_base.rb', line 54 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 end |