Class: XeroGateway::Currency

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

Constant Summary collapse

ATTRS =
{
  "Code" 	       => :string,     # 3 letter alpha code for the currency – see list of currency codes
  "Description"  => :string, 	   # Name of Currency
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Currency

Returns a new instance of Currency.



13
14
15
16
17
# File 'lib/xero_gateway/currency.rb', line 13

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

Class Method Details

.from_xml(currency_element) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/xero_gateway/currency.rb', line 36

def self.from_xml(currency_element)
  Currency.new.tap do |currency|
    currency_element.children.each do |element|
    
      attribute             = element.name
      underscored_attribute = element.name.underscore
    
      raise "Unknown attribute: #{attribute}" unless ATTRS.keys.include?(attribute)
    
      case (ATTRS[attribute])
        when :boolean then  currency.send("#{underscored_attribute}=", (element.text == "true"))
        when :float   then  currency.send("#{underscored_attribute}=", element.text.to_f)
        else                currency.send("#{underscored_attribute}=", element.text)
      end
      
    end
  end
end

Instance Method Details

#==(other) ⇒ Object



19
20
21
22
23
24
# File 'lib/xero_gateway/currency.rb', line 19

def ==(other)
  ATTRS.keys.map(&:underscore).each do |field|
    return false if send(field) != other.send(field)
  end
  return true
end

#to_xmlObject



26
27
28
29
30
31
32
33
34
# File 'lib/xero_gateway/currency.rb', line 26

def to_xml
  b = Builder::XmlMarkup.new
  
  b.Currency do
    ATTRS.keys.each do |attr|
      eval("b.#{attr} '#{self.send(attr.underscore.to_sym)}'")
    end
  end
end