Class: Dry::Facts::Aggregate
- Inherits:
-
Object
- Object
- Dry::Facts::Aggregate
- Defined in:
- lib/dry/facts/aggregate.rb
Instance Attribute Summary collapse
-
#events ⇒ Object
readonly
Returns the value of attribute events.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#uuid ⇒ Object
readonly
Returns the value of attribute uuid.
Class Method Summary collapse
- .aggregate_data_from_event_types(*event_klasses) ⇒ Object
- .build_all_from_events(events) ⇒ Object
- .build_one_from_events(events) ⇒ Object
- .define_event_handler(name, method_name = :no_op) ⇒ Object
- .event_handlers ⇒ Object
- .find_event_handler(key) ⇒ Object
Instance Method Summary collapse
-
#initialize(events) ⇒ Aggregate
constructor
A new instance of Aggregate.
Constructor Details
#initialize(events) ⇒ Aggregate
Returns a new instance of Aggregate.
47 48 49 50 51 52 53 |
# File 'lib/dry/facts/aggregate.rb', line 47 def initialize(events) @events = events || [] @uuid = @id = if @events.first @events.first.aggregate_id end @events.each {|e| self.send :_handle_event, e} end |
Instance Attribute Details
#events ⇒ Object (readonly)
Returns the value of attribute events.
10 11 12 |
# File 'lib/dry/facts/aggregate.rb', line 10 def events @events end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
11 12 13 |
# File 'lib/dry/facts/aggregate.rb', line 11 def id @id end |
#uuid ⇒ Object (readonly)
Returns the value of attribute uuid.
12 13 14 |
# File 'lib/dry/facts/aggregate.rb', line 12 def uuid @uuid end |
Class Method Details
.aggregate_data_from_event_types(*event_klasses) ⇒ Object
39 40 41 42 43 |
# File 'lib/dry/facts/aggregate.rb', line 39 def aggregate_data_from_event_types(*event_klasses) event_klasses.each do |klass| define_event_handler klass.name, :_transfer_data_from_event end end |
.build_all_from_events(events) ⇒ Object
20 21 22 23 24 |
# File 'lib/dry/facts/aggregate.rb', line 20 def build_all_from_events events events .group_by {|e| e.aggregate_id } .map {|_, g_events| build_one_from_events(g_events) } end |
.build_one_from_events(events) ⇒ Object
15 16 17 18 |
# File 'lib/dry/facts/aggregate.rb', line 15 def build_one_from_events events self .new(events) end |
.define_event_handler(name, method_name = :no_op) ⇒ Object
34 35 36 37 |
# File 'lib/dry/facts/aggregate.rb', line 34 def define_event_handler(name, method_name= :no_op) @event_handlers ||= {} @event_handlers[name] = method_name end |
.event_handlers ⇒ Object
26 27 28 |
# File 'lib/dry/facts/aggregate.rb', line 26 def event_handlers @event_handlers end |
.find_event_handler(key) ⇒ Object
30 31 32 |
# File 'lib/dry/facts/aggregate.rb', line 30 def find_event_handler(key) (@event_handlers)[key] end |