Class: Sequel::Extensions::Batches::Yielder

Inherits:
Object
  • Object
show all
Defined in:
lib/sequel/extensions/batches/yielder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ds:, **options) ⇒ Yielder

Returns a new instance of Yielder.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sequel/extensions/batches/yielder.rb', line 8

def initialize(ds:, **options)
  self.ds = ds.unordered
  self.pk = options.delete(:pk)
  self.of = options.delete(:of) || 1000
  self.start = options.delete(:start)
  self.finish = options.delete(:finish)
  self.order = options.delete(:order) || :asc

  raise ArgumentError, ":order must be :asc or :desc, got #{order.inspect}" unless %i[asc desc].include?(order)
  raise ArgumentError, "unknown options: #{options.keys.inspect}" if options.any?
end

Instance Attribute Details

#dsObject

Returns the value of attribute ds.



5
6
7
# File 'lib/sequel/extensions/batches/yielder.rb', line 5

def ds
  @ds
end

#finishObject

Returns the value of attribute finish.



5
6
7
# File 'lib/sequel/extensions/batches/yielder.rb', line 5

def finish
  @finish
end

#ofObject

Returns the value of attribute of.



5
6
7
# File 'lib/sequel/extensions/batches/yielder.rb', line 5

def of
  @of
end

#orderObject

Returns the value of attribute order.



5
6
7
# File 'lib/sequel/extensions/batches/yielder.rb', line 5

def order
  @order
end

#pk=(value) ⇒ Object

Sets the attribute pk

Parameters:

  • value

    the value to set the attribute pk to.



6
7
8
# File 'lib/sequel/extensions/batches/yielder.rb', line 6

def pk=(value)
  @pk = value
end

#startObject

Returns the value of attribute start.



5
6
7
# File 'lib/sequel/extensions/batches/yielder.rb', line 5

def start
  @start
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sequel/extensions/batches/yielder.rb', line 20

def call
  base_ds = setup_base_ds or return
  return enum_for(:call) unless block_given?

  current_instance = nil

  loop do
    working_ds =
      if current_instance
        base_ds.where(generate_conditions(current_instance.to_h, sign: sign_from_exclusive))
      else
        base_ds
      end

    working_ds_pk = working_ds.select(*qualified_pk).order(*order_by(qualified: true)).limit(of)
    current_instance = db.from(working_ds_pk).select(*pk).order(*order_by).last or break
    working_ds = working_ds.where(generate_conditions(current_instance.to_h, sign: sign_to_inclusive))

    yield working_ds
  end
end