Module: Cequel::Record::Collection Abstract

Extended by:
ActiveSupport::Concern, Forwardable
Included in:
List, Map, Set
Defined in:
lib/cequel/record/collection.rb

Overview

This module is abstract.

Including classes must descend from ‘Delegator` and implement the `::empty` class method.

The value of a collection column in a Cequel::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.

Examples:

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

Instance Method Summary collapse

Instance Method Details

#column_nameSymbol

Returns the name of the collection column.

Returns:

  • (Symbol)

    the name of the collection column



70
# File 'lib/cequel/record/collection.rb', line 70

def_delegator :@column, :name, :column_name

#initialize(model, column) ⇒ Collection

Returns a new collection.

Parameters:

  • model (Record)

    record that contains this collection

  • column (Schema::Column)

    column this collection’s data belongs to

Returns:

Since:

  • 1.0.0



86
87
88
# File 'lib/cequel/record/collection.rb', line 86

def initialize(model, column)
  @model, @column = model, column
end

#inspectString

Returns inspected underlying Ruby collection object.

Returns:

  • (String)

    inspected underlying Ruby collection object

Since:

  • 1.0.0



93
94
95
# File 'lib/cequel/record/collection.rb', line 93

def inspect
  __getobj__.inspect
end

#loaded!void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

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

Since:

  • 1.0.0



104
105
106
# File 'lib/cequel/record/collection.rb', line 104

def loaded!
  modifications.each { |modification| modification.call() }.clear
end

#loaded?Boolean

Returns ‘true` if the collection’s contents are loaded into memory.

Returns:

  • (Boolean)

    ‘true` if the collection’s contents are loaded into memory



63
# File 'lib/cequel/record/collection.rb', line 63

def_delegators :@model, :loaded?, :updater, :deleter

#persisted!void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

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

Since:

  • 1.0.0



116
117
118
# File 'lib/cequel/record/collection.rb', line 116

def persisted!
  modifications.clear
end