Sha256: 039cdcb6b8ac8efa8b99e5668dbc3fa858679e67f7b4b3074fc68eebb7f88d72

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

module Quickbooks::Support::ClassBuilder

private

def add_strict_attribute(klass, attr_name, type)
  add_attribute_type(klass, attr_name, type)
  
  eval <<-class_body
  class #{klass}
    attr_accessor :#{attr_name}

    def #{attr_name}=(obj)
      expected_type = self.class.#{attr_name}_type
      if obj.class == expected_type
        @#{attr_name} = obj
      elsif obj.is_a?(Hash)
        @#{attr_name} = #{type}.new(obj)
      else
        raise(TypeError, "expecting an object of type \#{expected_type}") 
      end
    end
  end
  class_body
end

def add_casting_attribute(klass, attr_name, type)
  type_casting_proc = klass::QB_TYPE_CONVERSION_MAP[type]
  type = type_casting_proc.call(nil).class
  add_attribute_type(klass, attr_name, type)

  eval <<-class_body
  class #{klass}
    attr_accessor :#{attr_name}

    def #{attr_name}=(obj)
      type_casting_proc = QB_TYPE_CONVERSION_MAP["#{type}"] 
      @#{attr_name} = type_casting_proc ? type_casting_proc.call(obj) : obj
    end
  end
  class_body
end

def add_attribute_type(klass, attr_name, type)
  eval <<-class_body
  class #{klass}
    @@#{attr_name}_type = #{type}
    def self.#{attr_name}_type
      #{type}
    end
  end
  class_body
end

def add_xml_template(klass, xml_template)
  eval <<-class_body
  class #{klass}
    def self.xml_template
      #{xml_template.dump}
    end
  end
  class_body
end


end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
quickbooks_api-0.0.3 lib/quickbooks/support/class_builder.rb
quickbooks_api-0.0.2 lib/quickbooks/support/class_builder.rb