Class: RubyEventStore::Projection
- Inherits:
-
Object
- Object
- RubyEventStore::Projection
- Defined in:
- lib/ruby_event_store/projection.rb
Instance Attribute Summary collapse
-
#handlers ⇒ Object
readonly
Returns the value of attribute handlers.
-
#streams ⇒ Object
readonly
Returns the value of attribute streams.
Class Method Summary collapse
Instance Method Summary collapse
- #call(event) ⇒ Object
- #current_state ⇒ Object
- #handled_events ⇒ Object
- #init(handler) ⇒ Object
- #initial_state ⇒ Object
-
#initialize(streams: []) ⇒ Projection
constructor
A new instance of Projection.
- #run(event_store, start: :head, count: PAGE_SIZE) ⇒ Object
- #when(events, handler) ⇒ Object
Constructor Details
#initialize(streams: []) ⇒ Projection
Returns a new instance of Projection.
15 16 17 18 19 |
# File 'lib/ruby_event_store/projection.rb', line 15 def initialize(streams: []) @streams = streams @handlers = Hash.new { ->(_, _) {} } @init = -> { Hash.new } end |
Instance Attribute Details
#handlers ⇒ Object (readonly)
Returns the value of attribute handlers.
21 22 23 |
# File 'lib/ruby_event_store/projection.rb', line 21 def handlers @handlers end |
#streams ⇒ Object (readonly)
Returns the value of attribute streams.
21 22 23 |
# File 'lib/ruby_event_store/projection.rb', line 21 def streams @streams end |
Class Method Details
.from_all_streams ⇒ Object
11 12 13 |
# File 'lib/ruby_event_store/projection.rb', line 11 def self.from_all_streams new end |
.from_stream(*streams) ⇒ Object
6 7 8 9 |
# File 'lib/ruby_event_store/projection.rb', line 6 def self.from_stream(*streams) raise(ArgumentError, "At least one stream must be given") if streams.empty? new(streams: streams) end |
Instance Method Details
#call(event) ⇒ Object
44 45 46 |
# File 'lib/ruby_event_store/projection.rb', line 44 def call(event) handlers.fetch(event.class).(current_state, event) end |
#current_state ⇒ Object
40 41 42 |
# File 'lib/ruby_event_store/projection.rb', line 40 def current_state @current_state ||= initial_state end |
#handled_events ⇒ Object
48 49 50 |
# File 'lib/ruby_event_store/projection.rb', line 48 def handled_events handlers.keys end |
#init(handler) ⇒ Object
23 24 25 26 |
# File 'lib/ruby_event_store/projection.rb', line 23 def init(handler) @init = handler self end |
#initial_state ⇒ Object
36 37 38 |
# File 'lib/ruby_event_store/projection.rb', line 36 def initial_state @init.call end |
#run(event_store, start: :head, count: PAGE_SIZE) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/ruby_event_store/projection.rb', line 52 def run(event_store, start: :head, count: PAGE_SIZE) if streams.any? reduce_from_streams(event_store, start, count) else reduce_from_all_streams(event_store, start, count) end end |
#when(events, handler) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/ruby_event_store/projection.rb', line 28 def when(events, handler) Array(events).each do |event| @handlers[event] = handler end self end |