Module: EnotasApi::Attributable::ClassMethods

Defined in:
lib/enotas_api/support/attributable.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, type) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/enotas_api/support/attributable.rb', line 44

def attribute(name, type)
  handler = EnotasApi::TypeHandler.new(type)

  (@attributes ||= {})[name] = type
  attr_reader name

  define_method "#{name}=" do |value|
    handler.validate!(value)

    instance_variable_set("@#{name}", handler.entity? && value.is_a?(Hash) ? type.new(value) : value)
    (@attributes_changed ||= []) << name
  end
end

#attribute_type(attribute) ⇒ Object



66
67
68
# File 'lib/enotas_api/support/attributable.rb', line 66

def attribute_type(attribute)
  entity_attributes[attribute.to_sym]
end

#attributes(map) ⇒ Object



58
59
60
# File 'lib/enotas_api/support/attributable.rb', line 58

def attributes(map)
  map.each_pair { |name, type| attribute(name, type) }
end

#entity_attributesObject



62
63
64
# File 'lib/enotas_api/support/attributable.rb', line 62

def entity_attributes
  (@attributes || {}).freeze
end