Module: CassandraObject::AttributeMethods::Typecasting::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#attribute(name, options) ⇒ Object

attribute :name, type: :string attribute :ammo, type: Ammo, coder: AmmoCodec



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cassandra_object/attribute_methods/typecasting.rb', line 32

def attribute(name, options)
  type  = options[:type]
  coder = options[:coder]

  if type.is_a?(Symbol)
    coder = CassandraObject::Type.get_coder(type) || (raise "Unknown type #{type}")
  elsif coder.nil?
    raise "Must supply a :coder for #{name}"
  end

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

#coder_for(attribute) ⇒ Object



53
54
55
# File 'lib/cassandra_object/attribute_methods/typecasting.rb', line 53

def coder_for(attribute)
  attribute_definitions[attribute.to_s].coder
end

#inherited(child) ⇒ Object



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

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

#typecast_attribute(record, name, value) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/cassandra_object/attribute_methods/typecasting.rb', line 45

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