Sha256: 82e2c302dc6624449f6a49e3ac0e41441e0a41b08fd9f86e8f2672aae79cc305

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 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
      case obj 
      when expected_type, Array
        @#{attr_name} = 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]
  ruby_type = type_casting_proc.call(nil).class
  add_attribute_type(klass, attr_name, ruby_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

1 entries across 1 versions & 1 rubygems

Version Path
quickbooks_api-0.0.7 lib/quickbooks/support/class_builder.rb