Class: Cequel::Record::AssociationCollection

Inherits:
RecordSet
  • Object
show all
Extended by:
Util::Forwardable
Includes:
Enumerable
Defined in:
lib/cequel/record/association_collection.rb

Overview

Collection of records from a has_many association. Encapsulates and behaves like a RecordSet, but unlike a normal RecordSet the loaded records are held in memory after they are loaded.

Instance Attribute Summary

Attributes inherited from RecordSet

#target_class

Instance Method Summary collapse

Methods included from Util::Forwardable

delegate

Methods inherited from RecordSet

#==, #[], #after, #all, #allow_filtering!, #assert_fully_specified!, #at, #before, #consistency, #data_set, default_attributes, #delete_all, #find_each, #find_each_row, #find_in_batches, #find_rows_in_batches, #first!, #first_or_initialize, #from, #in, #initialize, #last, #last_page?, #limit, #next_paging_state, #page_size, #paging_state, #reverse, #scoped_key_attributes, #to_ary, #upto, #values_at, #where

Methods included from Util::HashAccessors

#hattr_accessor, #hattr_inquirer, #hattr_reader, #hattr_writer

Methods included from BulkWrites

#delete_all, #destroy_all, #update_all

Constructor Details

This class inherits a constructor from Cequel::Record::RecordSet

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Cequel::Record::RecordSet

Instance Method Details

#countObject Also known as: length, size

Raises:

Since:

  • 1.0.0



56
57
58
# File 'lib/cequel/record/association_collection.rb', line 56

def count
  raise Cequel::Record::DangerousQueryError.new
end

#each {|Record| ... } ⇒ void

This method returns an undefined value.

Yields:

Since:

  • 1.0.0



21
22
23
# File 'lib/cequel/record/association_collection.rb', line 21

def each(&block)
  target.each(&block)
end

#find(*keys) ⇒ Record, LazyRecordCollection

Return a loaded Record or collection of loaded Records with the specified primary key values

Multiple arguments are mapped onto unscoped key columns. To specify multiple values for a given key column, use an array.

Examples:

One record with one-column primary key

# find the blog with subdomain 'cassandra'
Blog.find('cassandra')

Multiple records with one-column primary key

# find the blogs with subdomain 'cassandra' and 'postgres'
Blog.find(['cassandra', 'postgres'])

One record with two-column primary key

# find the post instance with blog subdomain 'cassandra' and
# permalink 'my-post'
Post.find('cassandra', 'my-post')

Multiple records with two-column primary key

# find the post instances with blog subdomain cassandra and
# permalinks 'my-post' and 'my-new-post'
Post.find('cassandra', ['my-post', 'my-new-post']

Parameters:

  • scoped_key_values

    one or more values for the final primary key column

Returns:

  • (Record)

    if a single key is specified, return the loaded record at that key

  • (LazyRecordCollection)

    if multiple keys are specified, return a collection of loaded records at those keys

Raises:

  • (RecordNotFound)

    if not all the keys correspond to records in the table

Since:

  • 1.0.0



28
29
30
31
32
# File 'lib/cequel/record/association_collection.rb', line 28

def find(*keys)
  if block_given? then super
  else record_set.find(*keys)
  end
end

#firstRecord #first(count) ⇒ Array

Overloads:

  • #firstRecord

    Returns the first record in this record set.

    Returns:

    • (Record)

      the first record in this record set

  • #first(count) ⇒ Array

    Returns the first ‘count` records of the record set.

    Parameters:

    • count (Integer)

      how many records to return

    Returns:

    • (Array)

      the first ‘count` records of the record set

Returns:

Since:

  • 1.0.0



46
47
48
49
50
# File 'lib/cequel/record/association_collection.rb', line 46

def first(*args)
  if loaded? then super
  else record_set.first(*args)
  end
end

#loaded?Boolean

Returns true if this collection’s records are loaded in memory.

Returns:

  • (Boolean)

    true if this collection’s records are loaded in memory

Since:

  • 1.0.0



66
67
68
# File 'lib/cequel/record/association_collection.rb', line 66

def loaded?
  !!@target
end

#select {|record| ... } ⇒ Array #select(*columns) ⇒ RecordSet

Overloads:

  • #select {|record| ... } ⇒ Array

    Returns records that pass the test given by the block.

    Yield Parameters:

    • record (Record)

      each record in the record set

    Returns:

    • (Array)

      records that pass the test given by the block

    See Also:

  • #select(*columns) ⇒ RecordSet

    Restrict which columns are selected when records are retrieved from the database

    Parameters:

    • columns (Symbol)

      column names

    Returns:

    • (RecordSet)

      record set with the given column selections applied

    See Also:

Returns:

Since:

  • 1.0.0



37
38
39
40
41
# File 'lib/cequel/record/association_collection.rb', line 37

def select(*columns)
  if block_given? then super
  else record_set.select(*columns)
  end
end