Sha256: 9159b8a8b89d6d391a7a68224c03c3e8c04ce123265d3e369011065dde033a21
Contents?: true
Size: 1.72 KB
Versions: 4
Compression:
Stored size: 1.72 KB
Contents
module CassandraObject module AttributeMethods module Typecasting extend ActiveSupport::Concern included do class_attribute :attribute_definitions self.attribute_definitions = {} %w(array boolean date float hash_t integer json string time).each do |type| instance_eval <<-EOV, __FILE__, __LINE__ + 1 def #{type}(*args) options = args.extract_options! args.each do |name| attribute(name, options.merge(:type => :#{type == 'hash_t' ? 'hash' : type})) end end EOV end end module ClassMethods def inherited(child) super child.attribute_definitions = attribute_definitions.dup end # # attribute :name, type: :string # attribute :ammo, type: Ammo, coder: AmmoCodec # 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_sym] = AttributeMethods::Definition.new(name, coder, options) end def typecast_attribute(record, name, value) if attribute_definition = attribute_definitions[name.to_sym] attribute_definition.instantiate(record, value) else raise NoMethodError, "Unknown attribute #{name.inspect}" end end def coder_for(attribute) attribute_definitions[attribute.to_sym].coder end end end end end
Version data entries
4 entries across 4 versions & 1 rubygems