Class: Hold::Sequel::DatasetLazyArray

Inherits:
ThinModels::LazyArray::MemoizedLength
  • Object
show all
Defined in:
lib/hold/sequel/dataset_lazy_array.rb

Overview

For returning ThinModels::LazyArray instances based off a Sequel dataset:

Instance Method Summary collapse

Constructor Details

#initialize(dataset, count_dataset = nil, &block) ⇒ DatasetLazyArray

Returns a new instance of DatasetLazyArray.



4
5
6
7
8
# File 'lib/hold/sequel/dataset_lazy_array.rb', line 4

def initialize(dataset, count_dataset=nil, &block)
  @dataset = dataset
  @count_dataset = count_dataset || @dataset
  @block = block
end

Instance Method Details

#_each(&block) ⇒ Object



10
11
12
13
# File 'lib/hold/sequel/dataset_lazy_array.rb', line 10

def _each(&block)
  rows = Hold::Sequel.translate_exceptions {@dataset.all}
  (@block ? @block.call(rows) : rows).each(&block)
end

#_lengthObject



15
16
17
# File 'lib/hold/sequel/dataset_lazy_array.rb', line 15

def _length
  Hold::Sequel.translate_exceptions {@count_dataset.count}
end

#slice_from_start_and_length(offset, limit) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hold/sequel/dataset_lazy_array.rb', line 19

def slice_from_start_and_length(offset, limit)
  rows = if limit > 0
    Hold::Sequel.translate_exceptions do
      @dataset.limit(limit, offset).all
    end
  else
    []
  end
  # we're supposed to return nil if offset > length of the array,
  # as per Array#slice:
  return nil if rows.empty? && offset > 0 && offset > length
  @block ? @block.call(rows) : rows
end