Class: XeroGateway::BaseRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/xero_gateway/base_record.rb

Direct Known Subclasses

Currency, Item, Organisation, TaxRate

Defined Under Namespace

Classes: UnsupportedAttributeType

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ BaseRecord

Returns a new instance of BaseRecord.



39
40
41
42
43
# File 'lib/xero_gateway/base_record.rb', line 39

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

Class Method Details

.attribute(name, value) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/xero_gateway/base_record.rb', line 16

def attribute(name, value)
  self.attribute_definitions ||= {}
  self.attribute_definitions[name] = value

  case value
  when Hash
    value.each do |k, v|
      attribute("#{name}#{k}", v)
    end
  else
    attr_accessor name.underscore
  end
end

.attributes(hash) ⇒ Object



10
11
12
13
14
# File 'lib/xero_gateway/base_record.rb', line 10

def attributes(hash)
  hash.each do |k, v|
    attribute k, v
  end
end

.from_xml(base_element) ⇒ Object



30
31
32
# File 'lib/xero_gateway/base_record.rb', line 30

def from_xml(base_element)
  new.from_xml(base_element)
end

.xml_elementObject



34
35
36
# File 'lib/xero_gateway/base_record.rb', line 34

def xml_element
  element_name || self.name.split('::').last
end

Instance Method Details

#==(other) ⇒ Object



45
46
47
# File 'lib/xero_gateway/base_record.rb', line 45

def ==(other)
  to_xml == other.to_xml
end

#array_from_xml(element, attr_definition) ⇒ Object



89
90
91
92
# File 'lib/xero_gateway/base_record.rb', line 89

def array_from_xml(element, attr_definition)
  definition_klass = attr_definition.first
  element.children.map { |child_el| definition_klass.from_xml(child_el) }
end

#from_xml(base_element) ⇒ Object



56
57
58
59
# File 'lib/xero_gateway/base_record.rb', line 56

def from_xml(base_element)
  from_xml_attributes(base_element)
  self
end

#from_xml_attributes(element, attribute = nil, attr_definition = self.class.attribute_definitions) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/xero_gateway/base_record.rb', line 61

def from_xml_attributes(element, attribute = nil, attr_definition = self.class.attribute_definitions)
  if Hash === attr_definition
    element.children.each do |child|
      next unless child.respond_to?(:name)

      child_attribute = child.name
      child_attr_definition = attr_definition[child_attribute]
      child_attr_name       = "#{attribute}#{child_attribute}" # SalesDetails/UnitPrice => SalesDetailsUnitPrice

      next unless child_attr_definition

      from_xml_attributes(child, child_attr_name, child_attr_definition)
    end

    return
  end

  value = case attr_definition
    when :boolean  then  element.text == "true"
    when :float    then  element.text.to_f
    when :integer  then  element.text.to_i
    when Array     then  array_from_xml(element, attr_definition)
    else                 element.text
  end if element.text.present? || element.children.present?

  send("#{attribute.underscore}=", value)
end

#to_xmlObject



49
50
51
52
53
54
# File 'lib/xero_gateway/base_record.rb', line 49

def to_xml
  builder = Builder::XmlMarkup.new
  builder.__send__(self.class.xml_element) do
    to_xml_attributes(builder)
  end
end

#to_xml_attributes(builder = Builder::XmlMarkup.new, path = nil, attr_definitions = self.class.attribute_definitions) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/xero_gateway/base_record.rb', line 94

def to_xml_attributes(builder = Builder::XmlMarkup.new, path = nil, attr_definitions = self.class.attribute_definitions)
  attr_definitions.each do |attr, value|
    case value
    when Hash
      builder.__send__(attr) do
        to_xml_attributes(builder, "#{path}#{attr}", value)
      end
    when Array
      raise UnsupportedAttributeType.new("#{value} instances don't respond to #to_xml") unless value.first.method_defined?(:to_xml)
      value = send("#{path}#{attr}".underscore) || []
      builder.__send__(attr) do |array_wrapper|
        value.map do |k|
          k.to_xml(array_wrapper)
        end
      end
    else
      builder.__send__(attr, send("#{path}#{attr}".underscore))
    end
  end
end