Module: Sourced::Evolve::ClassMethods
- Defined in:
- lib/sourced/evolve.rb
Instance Method Summary collapse
-
#before_evolve(&block) ⇒ Object
Run this block before any of the registered event handlers Example: before_evolve do |event| @updated_at = event.created_at end.
-
#evolve(event_type, &block) ⇒ Object
Example: evolve :event_type do |event| @updated_at = event.created_at end.
-
#evolve_all(event_list, &block) ⇒ Object
Example: # With an Array of event types evolve_all [:event_type1, :event_type2] do |event| @updated_at = event.created_at end.
-
#handle_events(_events) ⇒ Object
These two are the Reactor interface expected by Worker.
- #handled_events_for_evolve ⇒ Object
- #inherited(subclass) ⇒ Object
Instance Method Details
#before_evolve(&block) ⇒ Object
Run this block before any of the registered event handlers Example:
before_evolve do |event|
@updated_at = event.created_at
end
78 79 80 |
# File 'lib/sourced/evolve.rb', line 78 def before_evolve(&block) define_method(:before_evolve, &block) end |
#evolve(event_type, &block) ⇒ Object
Example:
evolve :event_type do |event|
@updated_at = event.created_at
end
67 68 69 70 71 |
# File 'lib/sourced/evolve.rb', line 67 def evolve(event_type, &block) handled_events_for_evolve << event_type unless event_type.is_a?(Symbol) block = NOOP_HANDLER unless block_given? define_method(Sourced.(Evolve::PREFIX, event_type.to_s), &block) end |
#evolve_all(event_list, &block) ⇒ Object
Example:
# With an Array of event types
evolve_all [:event_type1, :event_type2] do |event|
@updated_at = event.created_at
end
# From another Evolver that responds to #handled_events_for_evolve
evolve_all CartAggregate do |event|
@updated_at = event.created_at
end
94 95 96 97 98 99 |
# File 'lib/sourced/evolve.rb', line 94 def evolve_all(event_list, &block) event_list = event_list.handled_events_for_evolve if event_list.respond_to?(:handled_events_for_evolve) event_list.each do |event_type| evolve(event_type, &block) end end |
#handle_events(_events) ⇒ Object
These two are the Reactor interface expected by Worker
53 54 55 |
# File 'lib/sourced/evolve.rb', line 53 def handle_events(_events) raise NoMethodError, "implement .handle_events(Array<Event>) in #{self}" end |
#handled_events_for_evolve ⇒ Object
57 58 59 |
# File 'lib/sourced/evolve.rb', line 57 def handled_events_for_evolve @handled_events_for_evolve ||= [] end |
#inherited(subclass) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/sourced/evolve.rb', line 44 def inherited(subclass) super handled_events_for_evolve.each do |evt_type| subclass.handled_events_for_evolve << evt_type end end |