Class: Quickbooks::Parser::QbxmlBase

Inherits:
Object
  • Object
show all
Extended by:
Logger
Includes:
Logger, XMLGeneration
Defined in:
lib/quickbooks/parser/qbxml_base.rb

Overview

inheritance base for schema classes

Constant Summary collapse

QBXML_BASE =
Quickbooks::Parser::QbxmlBase
FLOAT_CAST =
Proc.new {|d| d ? Float(d) : 0.0}
BOOL_CAST =
Proc.new {|d| d ? (d == 'True' ? true : false) : false }
DATE_CAST =
Proc.new {|d| d ? Date.parse(d).strftime("%Y-%m-%d") : Date.today.strftime("%Y-%m-%d") }
TIME_CAST =
Proc.new {|d| d ? Time.parse(d).xmlschema : Time.now.xmlschema }
INT_CAST =
Proc.new {|d| d ? Integer(d.to_i) : 0 }
STR_CAST =
Proc.new {|d| d ? String(d) : ''}
QB_TYPE_CONVERSION_MAP =
{
  "AMTTYPE"          => FLOAT_CAST,
  "BOOLTYPE"         => BOOL_CAST,
  "DATETIMETYPE"     => TIME_CAST,
  "DATETYPE"         => DATE_CAST,
  "ENUMTYPE"         => STR_CAST,
  "FLOATTYPE"        => FLOAT_CAST,
  "GUIDTYPE"         => STR_CAST,
  "IDTYPE"           => STR_CAST,
  "INTTYPE"          => INT_CAST,
  "PERCENTTYPE"      => FLOAT_CAST,
  "PRICETYPE"        => FLOAT_CAST,
  "QUANTYPE"         => INT_CAST,
  "STRTYPE"          => STR_CAST,
  "TIMEINTERVALTYPE" => STR_CAST
}

Constants included from XMLParsing

XMLParsing::COMMENT_END, XMLParsing::COMMENT_MATCHER, XMLParsing::COMMENT_START, XMLParsing::XML_COMMENT, XMLParsing::XML_DOCUMENT, XMLParsing::XML_ELEMENT, XMLParsing::XML_NODE, XMLParsing::XML_NODE_SET, XMLParsing::XML_TEXT

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logger

log

Methods included from XMLGeneration

#to_qbxml

Methods included from Support::Inflection

#underscore

Methods included from XMLParsing

#cleanup_qbxml, #leaf_node?, #parse_leaf_node_data, #parse_xml_attributes

Constructor Details

#initialize(params = nil) ⇒ QbxmlBase

Returns a new instance of QbxmlBase.



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

def initialize(params = nil)
  return unless params.is_a?(Hash)
  @xml_attributes = params['xml_attributes'] || params[:xml_attributes] || {}
  params.delete('xml_attributes')
  params.delete(:xml_attributes)

  params.each do |attr, value|
    if self.respond_to?(attr)
      expected_attr_type = self.class.send("#{attr}_type")
      value = \
        case value
        when Hash
          expected_attr_type.new(value)
        when Array
          value.inject([]) { |a,i| a << expected_attr_type.new(i) }
        else value
        end
      self.send("#{attr}=", value)
    else
      log.info "Warning: instance #{self} does not respond to attribute #{attr}"
    end
  end
end

Class Attribute Details

.xml_attributesObject

Returns the value of attribute xml_attributes.



35
36
37
# File 'lib/quickbooks/parser/qbxml_base.rb', line 35

def xml_attributes
  @xml_attributes
end

Instance Attribute Details

#xml_attributesObject

Returns the value of attribute xml_attributes.



33
34
35
# File 'lib/quickbooks/parser/qbxml_base.rb', line 33

def xml_attributes
  @xml_attributes
end

Class Method Details

.attribute_namesObject



62
63
64
65
# File 'lib/quickbooks/parser/qbxml_base.rb', line 62

def self.attribute_names
  # 1.9.2 changes instance_methods behavior to return symbols instead of strings
  instance_methods(false).reject { |m| m[-1..-1] == '=' || m.to_s =~ /xml_attributes/ || m =~ /_xml_class/}.map { |m| m.to_s }
end

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

returns a type map of the object’s attributes



113
114
115
116
117
118
# File 'lib/quickbooks/parser/qbxml_base.rb', line 113

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

Instance Method Details

#attributes(recursive = true) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/quickbooks/parser/qbxml_base.rb', line 90

def attributes(recursive = true)
  attrs = {}
  attrs['xml_attributes'] = xml_attributes
  self.class.attribute_names.inject(attrs) do |h, m|
    val = self.send(m)
    if !val.nil?
      if recursive
        h[m] = case val
          when QBXML_BASE
            val.attributes
          when Array
            val.inject([]) { |a, obj| obj.is_a?(QBXML_BASE) ? a << obj.attributes : a << obj } 
          else val
          end
      else
        h[m] = val
      end
    end; h
  end
end

#inner_attributes(parent = self) ⇒ Object

returns innermost attributes without outer layers of the hash



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/quickbooks/parser/qbxml_base.rb', line 69

def inner_attributes(parent = self)
  attrs = attributes(false)
  attrs.delete('xml_attributes')
  values = attrs.values.compact

  if values.empty?
    attributes
  elsif values.first.is_a?(Array)
    attributes
  elsif values.size > 1
    parent.attributes
  else
    first_val = values.first
    if first_val.respond_to?(:inner_attributes)
      first_val.inner_attributes(self)
    else
      parent.attributes
    end
  end
end