Class: Cequel::Record::LazyRecordCollection

Inherits:
Array
  • Object
show all
Extended by:
Util::Forwardable
Includes:
BulkWrites
Defined in:
lib/cequel/record/lazy_record_collection.rb

Overview

Encapsulates a collection of unloaded Cequel::Record instances. In the case where a record set is scoped to fully specify the keys of multiple records, those records will be returned unloaded in a LazyRecordCollection. When an attribute is read from any of the records in a LazyRecordCollection, it will eagerly load all of the records' rows from the database.

Since:

  • 1.0.0

Instance Method Summary collapse

Methods included from Util::Forwardable

delegate

Methods included from BulkWrites

#delete_all, #destroy_all, #update_all

Constructor Details

#initialize(record_set) ⇒ LazyRecordCollection

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.

Returns a new instance of LazyRecordCollection.

Parameters:

  • record_set (RecordSet)

    record set representing the records in this collection

Since:

  • 1.0.0


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cequel/record/lazy_record_collection.rb', line 29

def initialize(record_set)
  fail ArgumentError if record_set.nil?
  @record_set = record_set

  exploded_key_attributes = [{}].tap do |all_key_attributes|
    key_columns.zip(scoped_key_values) do |column, values|
      all_key_attributes.replace([values].flatten.compact.flat_map do |value|
        all_key_attributes.map do |key_attributes|
          key_attributes.merge(column.name => value)
        end
      end)
    end
  end

  unloaded_records = exploded_key_attributes.map do |key_attributes|
    record_set.target_class.new_empty(key_attributes, self)
  end

  super(unloaded_records)
end

Instance Method Details

#assert_fully_specified!Object

Since:

  • 1.0.0


72
73
74
# File 'lib/cequel/record/lazy_record_collection.rb', line 72

def assert_fully_specified!
  self
end

#connectionObject


22
# File 'lib/cequel/record/lazy_record_collection.rb', line 22

def_delegators :record_set, :table, :connection

#load!LazyRecordCollection

Hydrate all the records in this collection from a database query

Returns:

Since:

  • 1.0.0


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cequel/record/lazy_record_collection.rb', line 54

def load!
  records_by_identity = index_by { |record| record.key_values }

  record_set.find_each_row do |row|
    identity = row.values_at(*record_set.key_column_names)
    records_by_identity[identity].hydrate(row)
  end

  loaded_count = count { |record| record.loaded? }
  if loaded_count < count
    fail Cequel::Record::RecordNotFound,
         "Expected #{count} results; got #{loaded_count}"
  end

  self
end

#tableObject


22
# File 'lib/cequel/record/lazy_record_collection.rb', line 22

def_delegators :record_set, :table, :connection