Sha256: 7e609af02eb58dd3d2ad8b6b1be5113d9038e4441d4dd6584191221869f68e79

Contents?: true

Size: 1.51 KB

Versions: 29

Compression:

Stored size: 1.51 KB

Contents

module XeroGateway
  class Currency
    
    unless defined? ATTRS
      ATTRS = {
        "Code" 	       => :string,     # 3 letter alpha code for the currency – see list of currency codes
        "Description"  => :string, 	   # Name of Currency
      }
    end
    
    attr_accessor *ATTRS.keys.map(&:underscore)
    
    def initialize(params = {})
      params.each do |k,v|
        self.send("#{k}=", v)
      end
    end
    
    def ==(other)
      ATTRS.keys.map(&:underscore).each do |field|
        return false if send(field) != other.send(field)
      end
      return true
    end
    
    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
    
    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
    
  end
end

Version data entries

29 entries across 29 versions & 3 rubygems

Version Path
xero_gateway-float-2.1.7 lib/xero_gateway/currency.rb
xero_gateway-float-2.1.6 lib/xero_gateway/currency.rb
xero_gateway-float-2.1.4 lib/xero_gateway/currency.rb
xero_gateway-float-2.1.3 lib/xero_gateway/currency.rb
xero_gateway-float-2.1.1 lib/xero_gateway/currency.rb
xero_gateway-float-2.0.18 lib/xero_gateway/currency.rb
xero_gateway-float-2.0.17 lib/xero_gateway/currency.rb
xero_gateway-float-2.0.16 lib/xero_gateway/currency.rb
xero_gateway-float-2.0.15 lib/xero_gateway/currency.rb
xero_gateway-2.1.0 lib/xero_gateway/currency.rb
xero_gateway-n8vision-2.0.20 lib/xero_gateway/currency.rb
xero_gateway-2.0.19 lib/xero_gateway/currency.rb
xero_gateway-2.0.18 lib/xero_gateway/currency.rb
xero_gateway-2.0.17 lib/xero_gateway/currency.rb
xero_gateway-2.0.16 lib/xero_gateway/currency.rb
xero_gateway-2.0.15 lib/xero_gateway/currency.rb
xero_gateway-2.0.14 lib/xero_gateway/currency.rb
xero_gateway-2.0.13 lib/xero_gateway/currency.rb
xero_gateway-2.0.12 lib/xero_gateway/currency.rb
xero_gateway-2.0.11 lib/xero_gateway/currency.rb