Module: EventSorcerer::Aggregate::ClassMethods
- Defined in:
- lib/event_sorcerer/aggregate.rb
Overview
Public: Class methods to be extended onto the including class.
Instance Method Summary collapse
-
#all ⇒ Object
Public: Load all Aggregates of this type.
-
#event_methods ⇒ Object
Public: An array of symbols representing the names of the methods which are events.
-
#events(&block) ⇒ Object
Public: Methods defined within this block will have their method symbol added to the event_methods array.
-
#find(id) ⇒ Object
Public: Load an aggregate out of the event store.
-
#find_or_new(id) ⇒ Object
Public: Load an aggregate out of the event store or create new.
-
#new(id = EventSorcerer.generate_id) ⇒ Object
Public: Creates a new aggregate.
Instance Method Details
#all ⇒ Object
Public: Load all Aggregates of this type.
Returns an Array of AggregateProxy objects.
25 26 27 28 29 30 31 |
# File 'lib/event_sorcerer/aggregate.rb', line 25 def all with_all_loaders_for_type do |loaders| loaders.map(&:load).each do |aggregate| unit_of_work.store_aggregate(aggregate) end end end |
#event_methods ⇒ Object
Public: An array of symbols representing the names of the methods which
are events.
35 36 37 |
# File 'lib/event_sorcerer/aggregate.rb', line 35 def event_methods @event_methods ||= [] end |
#events(&block) ⇒ Object
Public: Methods defined within this block will have their method symbol
added to the event_methods array.
block - block containing the event method definitions.
Returns self.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/event_sorcerer/aggregate.rb', line 45 def events(&block) test_class ||= Class.new(BasicObject) starting_methods = test_class.instance_methods test_class.class_eval(&block) new_events = test_class.instance_methods.select do |method| !starting_methods.include? method end event_methods.concat new_events class_eval(&block) self end |
#find(id) ⇒ Object
Public: Load an aggregate out of the event store.
id - the ID of the aggregate to load.
Returns an AggregateProxy object.
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/event_sorcerer/aggregate.rb', line 65 def find(id) if unit_of_work.fetch_aggregate(id) return unit_of_work.fetch_aggregate(id) end with_loader_for_id(id) do |loader| loader.load.tap do |aggregate| unit_of_work.store_aggregate(aggregate) end end end |
#find_or_new(id) ⇒ Object
Public: Load an aggregate out of the event store or create new.
id - the ID of the aggregate to load.
Returns an AggregateProxy object.
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/event_sorcerer/aggregate.rb', line 82 def find_or_new(id) if unit_of_work.fetch_aggregate(id) return unit_of_work.fetch_aggregate(id) end with_loader_for_id(id, false) do |loader| loader.load.tap do |aggregate| unit_of_work.store_aggregate(aggregate) end end end |
#new(id = EventSorcerer.generate_id) ⇒ Object
Public: Creates a new aggregate.
id - the ID to set on the new aggregate.
Returns an AggregateProxy object.
99 100 101 102 103 |
# File 'lib/event_sorcerer/aggregate.rb', line 99 def new(id = EventSorcerer.generate_id) AggregateCreator.new(self, id).create.tap do |aggregate| unit_of_work.store_aggregate(aggregate) end end |