module Cequel::Record::Collection

The value of a collection column in a {Record}. Collections track modifications that can be expressed as atomic collection mutations in CQL, and persist those modifications when their owning record is saved. Such modifications can be done even if the collection has not loaded data from CQL, in the case of an unloaded record or where the collection column was not included in the `SELECT` statement.

Mutation operations that require reading data before writing it are not supported (e.g. `Array#map!).

Each collection implementation wraps a built-in Ruby collection type.

@abstract Including classes must descend from `Delegator` and implement

the `::empty` class method.

@example

class Blog
  include Cequel::Record

  key :subdomain

  list :categories, :text
end

# Get an unloaded Blog instance; no data read
blog = Blog['cassandra']

# Stage modification to collection, still no data read
blog.categories << 'Big Data'

# Issue an UPDATE statement which pushes "Big Data" onto the
# collection. Still no data read
blog.save!

# Stage another modification to the collection
blog.categories.unshift('Distributed Database')

# Collection is lazily read from the database, and then staged
# modifications are made to the loaded collection
puts blog.categories.join(', ') 

# Issues an UPDATE statement which prepends "Distributed Data" onto the
# collection
blog.save!

@since 1.0.0

Attributes

column[R]
model[R]

Public Class Methods

new(model, column) click to toggle source

@param model [Record] record that contains this collection @param column [Schema::Column] column this collection's data belongs to @return [Collection] a new collection

# File lib/cequel/record/collection.rb, line 85
def initialize(model, column)
  @model, @column = model, column
end

Public Instance Methods

inspect() click to toggle source

@return [String] inspected underlying Ruby collection object

# File lib/cequel/record/collection.rb, line 92
def inspect
  __getobj__.inspect
end
loaded!() click to toggle source

Notify the collection that its underlying data is loaded in memory.

@return [void]

@api private

# File lib/cequel/record/collection.rb, line 103
def loaded!
  modifications.each { |modification| modification.call() }.clear
end
persisted!() click to toggle source

Notify the collection that its staged changes have been written to the data store.

@return [void]

@api private

# File lib/cequel/record/collection.rb, line 115
def persisted!
  modifications.clear
end

Protected Instance Methods

__getobj__() click to toggle source
# File lib/cequel/record/collection.rb, line 121
def __getobj__
  model.__send__(:read_attribute, column_name)
end
__setobj__(obj) click to toggle source
# File lib/cequel/record/collection.rb, line 125
def __setobj__(obj)
  fail "Attempted to call __setobj__ on read-only delegate!"
end

Private Instance Methods

modifications() click to toggle source
# File lib/cequel/record/collection.rb, line 149
def modifications
  @modifications ||= []
end
to_modify(&block) click to toggle source
# File lib/cequel/record/collection.rb, line 136
def to_modify(&block)
  if loaded?
    model.__send__("#{column_name}_will_change!")
    block.call
  else modifications << block
  end
  self
end
to_update() { || ... } click to toggle source
# File lib/cequel/record/collection.rb, line 145
def to_update
  yield unless model.new_record?
end