Module: Cequel::Record::Properties::ClassMethods

Defined in:
lib/cequel/record/properties.rb

Overview

Methods for defining columns on a record

Instance Method Summary collapse

Instance Method Details

#column(name, type, options = {}) ⇒ 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.

This method returns an undefined value.

Define a data column

Parameters:

  • name (Symbol)

    the name of the column

  • type (Symbol)

    the type of the column

  • options (Options) (defaults to: {})

    options for the column

Options Hash (options):

  • :default (Object, Proc)

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

  • :index (Boolean, Symbol)

    create a secondary index on this column



140
141
142
143
144
# File 'lib/cequel/record/properties.rb', line 140

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 = {}) ⇒ void

Note:

belongs_to implicitly defines key columns.

This method returns an undefined value.

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

Parameters:

  • name (Symbol)

    the name of the key column

  • type (Symbol)

    the type of the key column

  • options (Options) (defaults to: {})

    options for the key column

Options Hash (options):

  • :partition (Boolean) — default: false

    make this a partition key even if it is not the first key column

  • :auto (Boolean) — default: false

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

  • :order (:asc, :desc)

    whether rows should be ordered ascending or descending by this column. Only valid for clustering columns

See Also:



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cequel/record/properties.rb', line 99

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 = {}) ⇒ void

This method returns an undefined value.

Define a list column

Parameters:

  • name (Symbol)

    the name of the list

  • type (Symbol)

    the type of the elements in the list

  • options (Options) (defaults to: {})

    options for the list

Options Hash (options):

  • :default (Object, Proc) — default: []

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

See Also:

Since:

  • 1.0.0



159
160
161
162
163
# File 'lib/cequel/record/properties.rb', line 159

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 = {}) ⇒ void

This method returns an undefined value.

Define a map column

Parameters:

  • name (Symbol)

    the name of the map

  • key_type (Symbol)

    the type of the keys in the set

  • options (Options) (defaults to: {})

    options for the set

Options Hash (options):

  • :default (Object, Proc) — default: {}

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

See Also:

Since:

  • 1.0.0



197
198
199
200
201
# File 'lib/cequel/record/properties.rb', line 197

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 = {}) ⇒ void

This method returns an undefined value.

Define a set column

Parameters:

  • name (Symbol)

    the name of the set

  • type (Symbol)

    the type of the elements in the set

  • options (Options) (defaults to: {})

    options for the set

Options Hash (options):

  • :default (Object, Proc) — default: Set[]

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

See Also:

Since:

  • 1.0.0



178
179
180
181
182
# File 'lib/cequel/record/properties.rb', line 178

def set(name, type, options = {})
  def_collection_accessors(name, Set)
  set_attribute_default(name, options[:default])
  set_empty_attribute(name) { ::Set[] }
end