module Cequel::Record::Properties::ClassMethods

Methods for defining columns on a record

@see Properties

Protected Instance Methods

column(name, type, options = {}) click to toggle source

Define a data column

@param name [Symbol] the name of the column @param type [Symbol] the type of the column @param options [Options] options for the column @option options [Object,Proc] :default a default value for the

column, or a proc that returns a default value for the column

@option options [Boolean,Symbol] :index create a secondary index on

this column

@return [void]

@note Using type :enum will behave similar to an ActiveRecord enum:

example: `column :status, :enum, values: { open: 1, closed: 2 }`
will be handled as type Int
calling model.status will return the symbol ie. :open or :closed
expects setter to be called with symbol ie. model.status(:open)
exposes helpers ie. model.open?
exposes values-mapping on a class-level ModelClass.status

@note Secondary indexes are not nearly as flexible as primary keys:

you cannot query for multiple values or for ranges of values. You
also cannot combine a secondary index restriction with a primary
key restriction in the same query, nor can you combine more than
one secondary index restriction in the same query.
# File lib/cequel/record/properties.rb, line 139
def column(name, type, options = {})
  def_accessors(name)
  def_enum(name, options[:values]) if type == :enum
  set_attribute_default(name, options[:default])
end
key(name, type, options = {}) click to toggle source

Define a key column. By default, the first key column defined for a record will be a partition key, and the following keys will be clustering columns. This behavior can be changed using the `:partition` option

@param name [Symbol] the name of the key column @param type [Symbol] the type of the key column @param options [Options] options for the key column @option options [Boolean] :partition (false) make this a partition

key even if it is not the first key column

@option options [Boolean] :auto (false) automatically initialize this

key with a UUID value for new records. Only valid for `uuid` and
`timeuuid` columns.

@option options [:asc,:desc] :order whether rows should be ordered

ascending or descending by this column. Only valid for clustering
columns

@return [void]

@note {Associations::ClassMethods#belongs_to belongs_to} implicitly

defines key columns.

@see

http://cassandra.apache.org/doc/cql3/CQL.html#createTablepartitionClustering
CQL documentation on compound primary keys
# File lib/cequel/record/properties.rb, line 98
def key(name, type, options = {})
  def_accessors(name)
  if options.fetch(:auto, false)
    unless Type[type].is_a?(Cequel::Type::Uuid)
      fail ArgumentError, ":auto option only valid for UUID columns"
    end
    default = -> { Cequel.uuid } if options[:auto]
  else
    default = options[:default]
  end
  set_attribute_default(name, default)
end
list(name, type, options = {}) click to toggle source

Define a list column

@param name [Symbol] the name of the list @param type [Symbol] the type of the elements in the list @param options [Options] options for the list @option options [Object,Proc] :default ([]) a default value for the

column, or a proc that returns a default value for the column

@return [void]

@see Record::List @since 1.0.0

# File lib/cequel/record/properties.rb, line 158
def list(name, type, options = {})
  def_collection_accessors(name, List)
  set_attribute_default(name, options[:default])
  set_empty_attribute(name) { [] }
end
map(name, key_type, value_type, options = {}) click to toggle source

Define a map column

@param name [Symbol] the name of the map @param key_type [Symbol] the type of the keys in the set @param options [Options] options for the set @option options [Object,Proc] :default ({}) a default value for the

column, or a proc that returns a default value for the column

@return [void]

@see Record::Map @since 1.0.0

# File lib/cequel/record/properties.rb, line 196
def map(name, key_type, value_type, options = {})
  def_collection_accessors(name, Map)
  set_attribute_default(name, options[:default])
  set_empty_attribute(name) { {} }
end
set(name, type, options = {}) click to toggle source

Define a set column

@param name [Symbol] the name of the set @param type [Symbol] the type of the elements in the set @param options [Options] options for the set @option options [Object,Proc] :default (Set[]) a default value for

the column, or a proc that returns a default value for the column

@return [void]

@see Record::Set @since 1.0.0

# File lib/cequel/record/properties.rb, line 177
def set(name, type, options = {})
  def_collection_accessors(name, Set)
  set_attribute_default(name, options[:default])
  set_empty_attribute(name) { ::Set[] }
end

Private Instance Methods

def_accessors(name) click to toggle source
# File lib/cequel/record/properties.rb, line 232
def def_accessors(name)
  name = name.to_sym
  def_reader(name)
  def_writer(name)
end
def_collection_accessors(name, collection_proxy_class) click to toggle source
# File lib/cequel/record/properties.rb, line 250
def def_collection_accessors(name, collection_proxy_class)
  def_collection_reader(name, collection_proxy_class)
  def_collection_writer(name)
end
def_collection_reader(name, collection_proxy_class) click to toggle source
# File lib/cequel/record/properties.rb, line 255
        def def_collection_reader(name, collection_proxy_class)
          module_eval <<-RUBY, __FILE__, __LINE__+1
            def #{name}
              proxy_collection(#{name.inspect}, #{collection_proxy_class})
            end
          RUBY
        end
def_collection_writer(name) click to toggle source
# File lib/cequel/record/properties.rb, line 263
        def def_collection_writer(name)
          module_eval <<-RUBY, __FILE__, __LINE__+1
            def #{name}=(value)
              reset_collection_proxy(#{name.inspect})
              write_attribute(#{name.inspect}, value)
            end
          RUBY
        end
def_enum(name, values) click to toggle source
# File lib/cequel/record/properties.rb, line 204
def def_enum(name, values)
  name = name.to_sym
  def_enum_values(name, values)
  def_enum_reader(name, values)
  def_enum_writer(name, values)
end
def_enum_reader(name, values) click to toggle source
# File lib/cequel/record/properties.rb, line 215
        def def_enum_reader(name, values)
          module_eval <<-RUBY, __FILE__, __LINE__+1
            def #{name}; #{values.invert}[read_attribute(#{name.inspect})]; end
          RUBY
          values.each do |key, value|
            module_eval <<-RUBY, __FILE__, __LINE__+1
            def #{key}?; read_attribute(#{name.inspect}) == #{value}; end
            RUBY
          end
        end
def_enum_values(name, values) click to toggle source
# File lib/cequel/record/properties.rb, line 211
def def_enum_values(name, values)
  define_singleton_method(name) { values }
end
def_enum_writer(name, values) click to toggle source
# File lib/cequel/record/properties.rb, line 226
        def def_enum_writer(name, values)
          module_eval <<-RUBY, __FILE__, __LINE__+1
            def #{name}=(value); write_attribute(#{name.inspect}, #{values}[value]); end
          RUBY
        end
def_reader(name) click to toggle source
# File lib/cequel/record/properties.rb, line 238
        def def_reader(name)
          module_eval <<-RUBY, __FILE__, __LINE__+1
            def #{name}; read_attribute(#{name.inspect}); end
          RUBY
        end
def_writer(name) click to toggle source
# File lib/cequel/record/properties.rb, line 244
        def def_writer(name)
          module_eval <<-RUBY, __FILE__, __LINE__+1
            def #{name}=(value); write_attribute(#{name.inspect}, value); end
          RUBY
        end
set_attribute_default(name, default) click to toggle source
# File lib/cequel/record/properties.rb, line 272
def set_attribute_default(name, default)
  default_attributes[name.to_sym] = default
end
set_empty_attribute(name, &block) click to toggle source
# File lib/cequel/record/properties.rb, line 276
def set_empty_attribute(name, &block)
  empty_attributes[name.to_sym] = block
end