Class: RubyEventStore::Projection
- Inherits:
-
Object
- Object
- RubyEventStore::Projection
- Defined in:
- lib/ruby_event_store/projection.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call(scope) ⇒ Object
-
#initialize(initial_state) ⇒ Projection
constructor
A new instance of Projection.
- #on(*event_klasses, &block) ⇒ Object
Constructor Details
#initialize(initial_state) ⇒ Projection
Returns a new instance of Projection.
14 15 16 17 |
# File 'lib/ruby_event_store/projection.rb', line 14 def initialize(initial_state) @handlers = {} @init = -> { initial_state } end |
Class Method Details
.init(initial_state = nil) ⇒ Object
10 11 12 |
# File 'lib/ruby_event_store/projection.rb', line 10 def self.init(initial_state = nil) new(initial_state) end |
Instance Method Details
#call(scope) ⇒ Object
31 32 33 34 35 |
# File 'lib/ruby_event_store/projection.rb', line 31 def call(scope) return initial_state if handled_events.empty? scope.of_type(handled_events).reduce(initial_state, &method(:transition)) end |
#on(*event_klasses, &block) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ruby_event_store/projection.rb', line 19 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 |