Sha256: b75dafe4e09de332212e9c683d1cfd652faa4ab1463a4f7fc2dec32019381af2

Contents?: true

Size: 1.74 KB

Versions: 23

Compression:

Stored size: 1.74 KB

Contents

module XeroGateway
  class TaxRate
    
    unless defined? ATTRS
      ATTRS = {
        "Name"                  => :string, 
        "TaxType"               => :string,
        "CanApplyToAssets"      => :boolean,
        "CanApplyToEquity"      => :boolean,
        "CanApplyToExpenses"    => :boolean,
        "CanApplyToLiabilities" => :boolean,
        "CanApplyToRevenue"     => :boolean,
        "DisplayTaxRate"        => :float,
        "EffectiveRate"         => :float
      }
    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.TaxRate do
        ATTRS.keys.each do |attr|
          eval("b.#{attr} '#{self.send(attr.underscore.to_sym)}'")
        end
      end
    end
    
    def self.from_xml(tax_rate_element)
      TaxRate.new.tap do |tax_rate|
        tax_rate_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  tax_rate.send("#{underscored_attribute}=", (element.text == "true"))
            when :float   then  tax_rate.send("#{underscored_attribute}=", element.text.to_f)
            else                tax_rate.send("#{underscored_attribute}=", element.text)
          end
          
        end
      end
    end
    
  end
end

Version data entries

23 entries across 23 versions & 3 rubygems

Version Path
xero_gateway-float-2.0.18 lib/xero_gateway/tax_rate.rb
xero_gateway-float-2.0.17 lib/xero_gateway/tax_rate.rb
xero_gateway-float-2.0.16 lib/xero_gateway/tax_rate.rb
xero_gateway-float-2.0.15 lib/xero_gateway/tax_rate.rb
xero_gateway-n8vision-2.0.20 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.19 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.18 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.17 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.16 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.15 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.14 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.13 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.12 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.11 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.10 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.9 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.8 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.7 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.6 lib/xero_gateway/tax_rate.rb
xero_gateway-2.0.5 lib/xero_gateway/tax_rate.rb