Module: Superstore::AttributeMethods::Typecasting::ClassMethods

Defined in:
lib/superstore/attribute_methods/typecasting.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, options) ⇒ Object

attribute :name, type: :string attribute :ammo, type: :integer



32
33
34
35
36
37
# File 'lib/superstore/attribute_methods/typecasting.rb', line 32

def attribute(name, options)
  type_name  = options[:type]
  type_class = Superstore::Type.get_type_class(type_name) || (raise "Unknown type #{type_name}")

  attribute_definitions[name.to_s] = AttributeMethods::Definition.new(self, name, type_class, options)
end

#inherited(child) ⇒ Object



23
24
25
26
# File 'lib/superstore/attribute_methods/typecasting.rb', line 23

def inherited(child)
  super
  child.attribute_definitions = attribute_definitions.dup
end

#type_for(attribute) ⇒ Object



47
48
49
# File 'lib/superstore/attribute_methods/typecasting.rb', line 47

def type_for(attribute)
  attribute_definitions[attribute.to_s].type
end

#typecast_attribute(name, value) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/superstore/attribute_methods/typecasting.rb', line 39

def typecast_attribute(name, value)
  if attribute_definition = attribute_definitions[name.to_s]
    attribute_definition.instantiate(value)
  else
    raise NoMethodError, "Unknown attribute #{name.inspect}"
  end
end