Sha256: affda7e653921ab191dc6360cf7dc25d10c8fd63e017b45b2b07bffe1d4857ec

Contents?: true

Size: 1.68 KB

Versions: 11

Compression:

Stored size: 1.68 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 integer json string time).each do |type|
          instance_eval <<-EOV, __FILE__, __LINE__ + 1
            def #{type}(name, options = {})                             # def string(name, options = {})
              attribute(name, options.update(type: :#{type}))           #   attribute(name, options.update(type: :string))
            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
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
gotime-cassandra_object-2.11.7 lib/cassandra_object/attribute_methods/typecasting.rb
gotime-cassandra_object-2.11.6 lib/cassandra_object/attribute_methods/typecasting.rb
gotime-cassandra_object-2.11.5 lib/cassandra_object/attribute_methods/typecasting.rb
gotime-cassandra_object-2.11.4 lib/cassandra_object/attribute_methods/typecasting.rb
gotime-cassandra_object-2.11.3 lib/cassandra_object/attribute_methods/typecasting.rb
gotime-cassandra_object-2.11.2 lib/cassandra_object/attribute_methods/typecasting.rb
gotime-cassandra_object-2.11.1 lib/cassandra_object/attribute_methods/typecasting.rb
gotime-cassandra_object-2.11.0 lib/cassandra_object/attribute_methods/typecasting.rb
gotime-cassandra_object-2.10.11 lib/cassandra_object/attribute_methods/typecasting.rb
gotime-cassandra_object-2.10.10 lib/cassandra_object/attribute_methods/typecasting.rb
gotime-cassandra_object-2.10.9 lib/cassandra_object/attribute_methods/typecasting.rb