Class: EventSorcerer::AggregateProxy
- Inherits:
-
Object
- Object
- EventSorcerer::AggregateProxy
- Extended by:
- Uber::Delegates
- Defined in:
- lib/event_sorcerer/aggregate_proxy.rb
Overview
Public: Transparent wrapped around Aggregate objects which tracks dirty
events and handles persisting these.
Defined Under Namespace
Classes: SaveReciept
Instance Method Summary collapse
-
#initialize(aggregate) ⇒ AggregateProxy
constructor
Public: Creates a new AggregateProxy instance.
-
#method_missing(method_sym, *arguments, &block) ⇒ Object
Public: Forwards messages to the aggregate.
-
#save(meta = {}) ⇒ Object
Public: Saves the current dirty events via the current unit_of_work.
Constructor Details
#initialize(aggregate) ⇒ AggregateProxy
Public: Creates a new AggregateProxy instance.
aggregate - aggregate to wrap.
22 23 24 25 |
# File 'lib/event_sorcerer/aggregate_proxy.rb', line 22 def initialize(aggregate) @_aggregate = aggregate @_dirty_events = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments, &block) ⇒ Object
Public: Forwards messages to the aggregate. Saves the details of event
to the _dirty_events array.
29 30 31 32 33 34 35 |
# File 'lib/event_sorcerer/aggregate_proxy.rb', line 29 def method_missing(method_sym, *arguments, &block) if event_method?(method_sym) send_event_to_aggregate(method_sym, *arguments, &block) else @_aggregate.send method_sym, *arguments, &block end end |
Instance Method Details
#save(meta = {}) ⇒ Object
Public: Saves the current dirty events via the current unit_of_work.
meta - Hash of extra data to publish on the message bus with the events
after save.
Returns self.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/event_sorcerer/aggregate_proxy.rb', line 43 def save( = {}) dirty_events = @_dirty_events version = persisted_version unit_of_work.handle_save(proc do event_store.append_events(id, self.class.name, dirty_events, version) SaveReciept.new(id, self.class, dirty_events, ) end) @_dirty_events = [] @_aggregate.instance_variable_set(:@persisted_version, local_version) self end |