Sha256: 2086661661573d6de59a5002dc42e208bbe9338a60da8e21aa611d90c6e289e8

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

module XeroGateway
  class TrackingCategory
    attr_accessor :name, :options
    
    def initialize(params = {})
      @options = []
      params.each do |k,v|
        self.send("#{k}=", v)
      end
    end
    
    def option
       options[0] if options.size == 1
    end
        
    def to_xml(b = Builder::XmlMarkup.new)
      b.TrackingCategory {
        b.Name self.name
        b.Options {
          if self.options.is_a?(Array)
            self.options.each do |option|
              b.Option {
                b.Name option
              }
            end
          else
            b.Option {
              b.Name self.options.to_s
            }            
          end
        }
      }
    end
    
    # When a tracking category is serialized as part of an invoice it may only have a single
    # option, and the Options tag is omitted
    def to_xml_for_invoice_messages(b = Builder::XmlMarkup.new)
      b.TrackingCategory {
        b.Name self.name
        b.Option self.options.is_a?(Array) ? self.options.first : self.options.to_s 
      }      
    end
    
    def self.from_xml(tracking_category_element)
      tracking_category = TrackingCategory.new
      tracking_category_element.children.each do |element|
        case(element.name)
          when "Name" then tracking_category.name = element.text
          when "Options" then element.children.each {|option| tracking_category.options << option.children.first.text}
        end
      end
      tracking_category              
    end  
    
    def ==(other)
      [:name, :options].each do |field|
        return false if send(field) != other.send(field)
      end
      return true
    end      
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
xero_gateway-2.0.2 lib/xero_gateway/tracking_category.rb