Module: Cequel::Record::Properties

Extended by:
ActiveSupport::Concern
Defined in:
lib/cequel/record/properties.rb

Overview

Properties on a Cequel record acts as attributes on record instances, and are persisted as column values to Cassandra. Properties are declared explicitly on a record instance in the body.

Properties can be **key columns**, **data columns**, or **collection columns**. Key columns combine to form the primary key for the record; they cannot be changed once a record has been saved. Data columns contain scalar data values like strings, integers, and timestamps. Collection columns are lists, sets, or maps that can be atomically updated.

All varieties of column have a type; see Type for the full list of possibilities. A collection column’s type is the type of its elements (in the case of a map collection, there is both a key type and a value type).

Examples:

class Post
  key :blog_subdomain, :text
  key :id, :timeuuid, auto: true

  column :title, :text
  column :body, :text
  column :updated_at, :timestamp

  list :categories, :text
  set :tags, :text
  map :referers, :text, :integer
end

See Also:

Defined Under Namespace

Modules: ClassMethods, ConstructorMethods

Instance Method Summary collapse

Instance Method Details

#==(other) ⇒ Boolean

Returns true if this record has the same type and key attributes as the other record.

Returns:

  • (Boolean)

    true if this record has the same type and key attributes as the other record



289
290
291
292
293
294
295
# File 'lib/cequel/record/properties.rb', line 289

def ==(other)
  if key_values.any? { |value| value.nil? }
    super
  else
    self.class == other.class && key_values == other.key_values
  end
end

#[](column_name) ⇒ Object

Read an attribute

Parameters:

  • column_name (Symbol)

    the name of the column

Returns:

  • the value of that column

Raises:



270
271
272
# File 'lib/cequel/record/properties.rb', line 270

def [](column_name)
  read_attribute(column_name)
end

#[]=(column_name, value) ⇒ void

This method returns an undefined value.

Write an attribute

Parameters:

  • column_name (Symbol)

    name of the column to write

  • value

    the value to write to the column

Raises:



282
283
284
# File 'lib/cequel/record/properties.rb', line 282

def []=(column_name, value)
  write_attribute(column_name, value)
end

#attribute_namesArray<Symbol>

Returns list of names of attributes on this record.

Returns:

  • (Array<Symbol>)

    list of names of attributes on this record



235
236
237
# File 'lib/cequel/record/properties.rb', line 235

def attribute_names
  @attributes.keys
end

#attributesHash<Symbol,Object>

Returns map of column names to values currently set on this record.

Returns:

  • (Hash<Symbol,Object>)

    map of column names to values currently set on this record



243
244
245
246
247
# File 'lib/cequel/record/properties.rb', line 243

def attributes
  attribute_names.each_with_object({}) do |name, attributes|
    attributes[name] = read_attribute(name)
  end
end

#attributes=(attributes) ⇒ void

This method returns an undefined value.

Set attributes on the record. Each attribute is set via the setter method; virtual (non-column) attributes are allowed.

Parameters:

  • attributes (Hash)

    map of attribute names to values



256
257
258
259
260
# File 'lib/cequel/record/properties.rb', line 256

def attributes=(attributes)
  attributes.each_pair do |attribute, value|
    __send__(:"#{attribute}=", value)
  end
end

#initialize(attributes = {}, record_collection = nil) ⇒ Object



227
228
229
230
# File 'lib/cequel/record/properties.rb', line 227

def initialize(attributes = {}, record_collection = nil)
  @attributes, @record_collection = attributes, record_collection
  @collection_proxies = {}
end

#inspectString

Returns string representation of the record.

Returns:

  • (String)

    string representation of the record



300
301
302
303
304
305
306
307
308
# File 'lib/cequel/record/properties.rb', line 300

def inspect
  inspected_attributes = attributes.each_pair.map do |attr, value|
    inspected_value = value.is_a?(CassandraCQL::UUID) ?
      value.to_guid :
      value.inspect
    "#{attr}: #{inspected_value}"
  end
  "#<#{self.class} #{inspected_attributes.join(", ")}>"
end