module Cequel::Record::Properties

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 {Cequel::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).

@example

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 ClassMethods Methods for defining properties

Public Class Methods

new(attributes = {}, record_collection = nil) click to toggle source

@private

# File lib/cequel/record/properties.rb, line 282
def initialize(attributes = {}, record_collection = nil)
  @cequel_attributes, @record_collection = attributes, record_collection
  @collection_proxies = {}
end

Public Instance Methods

==(other) click to toggle source

@return [Boolean] true if this record has the same type and key

attributes as the other record
Calls superclass method
# File lib/cequel/record/properties.rb, line 345
def ==(other)
  if key_values.any? { |value| value.nil? }
    super
  else
    self.class == other.class && key_values == other.key_values
  end
end
[](column_name) click to toggle source

Read an attribute

@param column_name [Symbol] the name of the column @return the value of that column @raise [MissingAttributeError] if the attribute has not been loaded @raise [UnknownAttributeError] if the attribute does not exist

# File lib/cequel/record/properties.rb, line 326
def [](column_name)
  read_attribute(column_name)
end
[]=(column_name, value) click to toggle source

Write an attribute

@param column_name [Symbol] name of the column to write @param value the value to write to the column @return [void] @raise [UnknownAttributeError] if the attribute does not exist

# File lib/cequel/record/properties.rb, line 338
def []=(column_name, value)
  write_attribute(column_name, value)
end
attribute_names() click to toggle source

@return [Array<Symbol>] list of names of attributes on this record

# File lib/cequel/record/properties.rb, line 290
def attribute_names
  @cequel_attributes.keys
end
attributes() click to toggle source

@return [Hash<String,Object>] map of column names to values currently

set on this record
# File lib/cequel/record/properties.rb, line 298
def attributes
  attribute_names
    .each_with_object(HashWithIndifferentAccess.new) do |name, attributes|
    attributes[name] = read_attribute(name)
  end
end
attributes=(attributes) click to toggle source

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

@param attributes [Hash] map of attribute names to values @return [void]

# File lib/cequel/record/properties.rb, line 312
def attributes=(attributes)
  attributes.each_pair do |attribute, value|
    __send__(:"#{attribute}=", value)
  end
end
inspect() click to toggle source

@return [String] string representation of the record

# File lib/cequel/record/properties.rb, line 356
def inspect
  inspected_attributes = attributes.each_pair.map do |attr, value|
    inspected_value = Cequel.uuid?(value) ?
      value.to_s :
      value.inspect
    "#{attr}: #{inspected_value}"
  end
  "#<#{self.class} #{inspected_attributes.join(", ")}>"
end

Protected Instance Methods

read_attribute(name) click to toggle source
# File lib/cequel/record/properties.rb, line 368
def read_attribute(name)
  @cequel_attributes.fetch(name)
rescue KeyError
  if self.class.reflect_on_column(name)
    fail MissingAttributeError, "missing attribute: #{name}"
  else
    fail UnknownAttributeError, "unknown attribute: #{name}"
  end
end
write_attribute(name, value) click to toggle source
# File lib/cequel/record/properties.rb, line 378
def write_attribute(name, value)
  unless self.class.reflect_on_column(name)
    fail UnknownAttributeError, "unknown attribute: #{name}"
  end

  send("#{name}_will_change!") unless value === read_attribute(name)
  @cequel_attributes[name] = value
end

Private Instance Methods

init_attributes(new_attributes) click to toggle source
# File lib/cequel/record/properties.rb, line 398
def init_attributes(new_attributes)
  @cequel_attributes = {}
  new_attributes.each_pair do |name, value|
    if value.nil?
      value = empty_attributes.fetch(name.to_sym) { -> {} }.call
    end
    @cequel_attributes[name.to_sym] = value
  end
  @cequel_attributes
end
initialize_new_record(attributes = {}) { |self| ... } click to toggle source
# File lib/cequel/record/properties.rb, line 409
def initialize_new_record(attributes = {})
  dynamic_defaults = default_attributes
    .select { |name, value| value.is_a?(Proc) }
  new_attributes =
    Util.deep_copy(default_attributes.except(*dynamic_defaults.keys))
  dynamic_defaults.each { |name, p| new_attributes[name] = p.call }
  init_attributes(new_attributes)

  @new_record = true
  yield self if block_given?
  self.attributes = attributes
  loaded!
  self
end
proxy_collection(column_name, proxy_class) click to toggle source
# File lib/cequel/record/properties.rb, line 389
def proxy_collection(column_name, proxy_class)
  column = self.class.reflect_on_column(column_name)
  collection_proxies[column_name] ||= proxy_class.new(self, column)
end
reset_collection_proxy(name) click to toggle source
# File lib/cequel/record/properties.rb, line 394
def reset_collection_proxy(name)
  collection_proxies.delete(name)
end