Class: EventSource::Get::Substitute

Inherits:
Object
  • Object
show all
Includes:
EventSource::Get
Defined in:
lib/event_source/get.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EventSource::Get

included

Class Method Details

.build(batch_size: nil, session: nil) ⇒ Object



24
25
26
# File 'lib/event_source/get.rb', line 24

def self.build(batch_size: nil, session: nil)
  new(batch_size)
end

Instance Method Details

#batch_sizeObject



16
17
18
# File 'lib/event_source/get.rb', line 16

def batch_size
  @batch_size ||= 1
end

#call(stream_name = nil, position: nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/event_source/get.rb', line 28

def call(stream_name=nil, position: nil)
  position ||= 0

  logger.trace(tag: :control) { "Getting (Position: #{position}, Batch Size: #{batch_size}, Stream Name: #{stream_name.inspect})" }

  logger.debug(tags: [:control, :data]) { "Items: \n#{items.pretty_inspect}" }
  logger.debug(tag: :control) { "Position: #{position.inspect}" }
  logger.debug(tag: :control) { "Batch Size: #{batch_size.inspect}" }

  unless EventSource::StreamName.category?(stream_name)
    index = (items.index { |i| i.position >= position })
  else
    index = (items.index { |i| i.global_position >= position })
  end

  logger.debug(tag: :control) { "Index: #{index.inspect}" }

  if index.nil?
    items = []
  else
    range = index..(index + batch_size - 1)
    logger.debug(tag: :control) { "Range: #{range.pretty_inspect}" }

    items = self.items[range]
  end

  logger.info(tags: [:control, :data]) { "Got: \n#{items.pretty_inspect}" }
  logger.info(tag: :control) { "Finished getting (Position: #{position}, Stream Name: #{stream_name.inspect})" }

  items
end

#itemsObject



20
21
22
# File 'lib/event_source/get.rb', line 20

def items
  @items ||= []
end