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

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



120
121
122
123
# File 'lib/cequel/record/properties.rb', line 120

def column(name, type, options = {})
  def_accessors(name)
  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:



97
98
99
100
101
102
103
104
105
106
# File 'lib/cequel/record/properties.rb', line 97

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 = -> { CassandraCQL::UUID.new } if options[:auto]
  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



138
139
140
141
# File 'lib/cequel/record/properties.rb', line 138

def list(name, type, options = {})
  def_collection_accessors(name, List)
  set_attribute_default(name, options.fetch(:default, []))
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



174
175
176
177
# File 'lib/cequel/record/properties.rb', line 174

def map(name, key_type, value_type, options = {})
  def_collection_accessors(name, Map)
  set_attribute_default(name, options.fetch(:default, {}))
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



156
157
158
159
# File 'lib/cequel/record/properties.rb', line 156

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