Class: RubyEventStore::Projection

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_event_store/projection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_state = nil, _internal: false) ⇒ Projection

Returns a new instance of Projection.



20
21
22
23
24
25
# File 'lib/ruby_event_store/projection.rb', line 20

def initialize(initial_state = nil, _internal: false)
  warn NEW_CONSTRUCTOR_DEPRECATION_MESSAGE unless _internal
  @handlers = {}
  @init = -> { initial_state }
  @streams = []
end

Class Method Details

.from_all_streamsObject



62
63
64
65
# File 'lib/ruby_event_store/projection.rb', line 62

def self.from_all_streams
  warn DEPRECATION_MESSAGE
  new(_internal: true)
end

.from_stream(stream_or_streams) ⇒ Object

Raises:

  • (ArgumentError)


53
54
55
56
57
58
59
60
# File 'lib/ruby_event_store/projection.rb', line 53

def self.from_stream(stream_or_streams)
  warn DEPRECATION_MESSAGE
  streams = Array(stream_or_streams)
  raise(ArgumentError, "At least one stream must be given") if streams.empty?
  projection = new(_internal: true)
  projection.instance_variable_set(:@streams, streams)
  projection
end

.init(initial_state = nil) ⇒ Object



27
28
29
# File 'lib/ruby_event_store/projection.rb', line 27

def self.init(initial_state = nil)
  new(initial_state, _internal: true)
end

Instance Method Details

#call(*scopes) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/ruby_event_store/projection.rb', line 43

def call(*scopes)
  return initial_state if handled_events.empty?

  warn MULTI_SCOPE_DEPRECATION_MESSAGE if scopes.size > 1

  scopes.reduce(initial_state) do |state, scope|
    scope.of_type(handled_events).reduce(state, &method(:transition))
  end
end

#init(handler) ⇒ Object



67
68
69
70
71
# File 'lib/ruby_event_store/projection.rb', line 67

def init(handler)
  warn DEPRECATION_MESSAGE
  @init = handler
  self
end

#on(*event_klasses, &block) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ruby_event_store/projection.rb', line 31

def on(*event_klasses, &block)
  raise(ArgumentError, "No handler block given") unless block_given?

  event_klasses.each do |event_klass|
    name = event_klass.to_s
    raise(ArgumentError, "Anonymous class is missing name") if name.start_with? ANONYMOUS_CLASS

    @handlers[name] = ->(state, event) { block.call(state, event) }
  end
  self
end

#run(event_store, start: nil, count: PAGE_SIZE) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ruby_event_store/projection.rb', line 82

def run(event_store, start: nil, count: PAGE_SIZE)
  warn DEPRECATION_MESSAGE

  if @streams.any?
    raise ArgumentError, "Start must be an array with event ids" unless valid_start_for_streams?(start)
    scopes =
      @streams.zip(start || []).map do |stream, start_event_id|
        scope = event_store.read.stream(stream).in_batches(count)
        scope = scope.from(start_event_id) if start_event_id
        scope
      end
  else
    raise ArgumentError, "Start must be valid event id" unless valid_start_for_all_streams?(start)
    scope = event_store.read.in_batches(count)
    scope = scope.from(start) if start
    scopes = [scope]
  end

  call(*scopes)
end

#when(events, handler) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/ruby_event_store/projection.rb', line 73

def when(events, handler)
  warn DEPRECATION_MESSAGE
  Array(events).each do |event_klass|
    name = event_klass.to_s
    @handlers[name] = ->(state, event) { handler.call(state, event); state }
  end
  self
end