Class: EventSorcerer::UnitOfWork
- Inherits:
-
Object
- Object
- EventSorcerer::UnitOfWork
- Extended by:
- Uber::Delegates
- Defined in:
- lib/event_sorcerer/unit_of_work.rb
Overview
Public: Provides transactional integrity across multiple aggregate saves.
Also provides an indentity map.
Instance Method Summary collapse
-
#add_meta_data(meta_hash) ⇒ Object
Public: Add additional meta data to be sent along with any save reciepts on the message bus for the duration of the unit of work.
-
#execute_work! ⇒ Object
Public: Executes all pending saves within a transaction, clears the pending saves, and publishes the reciepts via the message bus.
-
#fetch_aggregate(id) ⇒ Object
Public: Fetches an aggregate via it’s ID from the identity map.
- #handle_save(save) ⇒ Object
-
#initialize ⇒ UnitOfWork
constructor
Public: Creates a new UnitOfWork instance.
-
#store_aggregate(aggregate) ⇒ Object
Public: Stores an aggregate via it’s ID into the identity map.
Constructor Details
#initialize ⇒ UnitOfWork
Public: Creates a new UnitOfWork instance.
11 12 13 14 15 |
# File 'lib/event_sorcerer/unit_of_work.rb', line 11 def initialize @identity_map = {} @meta = {} @pending_saves = [] end |
Instance Method Details
#add_meta_data(meta_hash) ⇒ Object
Public: Add additional meta data to be sent along with any save reciepts
on the message bus for the duration of the unit of work.
19 20 21 |
# File 'lib/event_sorcerer/unit_of_work.rb', line 19 def () .merge! end |
#execute_work! ⇒ Object
Public: Executes all pending saves within a transaction, clears the
pending saves, and publishes the reciepts via the message bus.
Returns self.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/event_sorcerer/unit_of_work.rb', line 27 def execute_work! save_receipts = event_store.with_transaction do pending_saves.map(&:call) end @pending_saves = [] save_receipts.each do |reciept| .publish_events(reciept.id, reciept.klass, reciept.events, reciept..merge()) end self end |
#fetch_aggregate(id) ⇒ Object
Public: Fetches an aggregate via it’s ID from the identity map.
id - the ID for the aggregate.
Returns nil if not found. Returns Aggregate if found.
48 49 50 |
# File 'lib/event_sorcerer/unit_of_work.rb', line 48 def fetch_aggregate(id) identity_map[id] end |
#handle_save(save) ⇒ Object
52 53 54 55 56 |
# File 'lib/event_sorcerer/unit_of_work.rb', line 52 def handle_save(save) pending_saves << save self end |
#store_aggregate(aggregate) ⇒ Object
Public: Stores an aggregate via it’s ID into the identity map.
aggregate - the aggregate to store.
Returns self.
63 64 65 66 67 68 69 |
# File 'lib/event_sorcerer/unit_of_work.rb', line 63 def store_aggregate(aggregate) return self if fetch_aggregate(aggregate.id) identity_map[aggregate.id] = aggregate self end |