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
Returns the value of attribute id.
-
#uuid ⇒ Object
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_type_handler(klass, method_name) ⇒ Object
- .event_handlers ⇒ Object
- .event_types ⇒ Object
- .find_event_handler(key) ⇒ Object
Instance Method Summary collapse
-
#initialize(events) ⇒ Aggregate
constructor
A new instance of Aggregate.
- #to_h ⇒ Object (also: #to_hash)
Constructor Details
#initialize(events) ⇒ Aggregate
Returns a new instance of Aggregate.
50 51 52 53 54 55 56 57 58 |
# File 'lib/dry/facts/aggregate.rb', line 50 def initialize(events) @events = events || [] first_event_aggregate_id= if @events.first @events.first.aggregate_id end @uuid = @id = first_event_aggregate_id @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
Returns the value of attribute id.
11 12 13 |
# File 'lib/dry/facts/aggregate.rb', line 11 def id @id end |
#uuid ⇒ Object
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
15 16 17 18 19 |
# File 'lib/dry/facts/aggregate.rb', line 15 def aggregate_data_from_event_types *event_klasses event_klasses.each do |klass| define_event_type_handler klass, :_transfer_data_from_event end end |
.build_all_from_events(events) ⇒ Object
21 22 23 24 25 |
# File 'lib/dry/facts/aggregate.rb', line 21 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
27 28 29 30 |
# File 'lib/dry/facts/aggregate.rb', line 27 def build_one_from_events events self .new(events) end |
.define_event_type_handler(klass, method_name) ⇒ Object
32 33 34 35 |
# File 'lib/dry/facts/aggregate.rb', line 32 def define_event_type_handler klass, method_name @event_handlers ||= {} @event_handlers[klass.name] = method_name end |
.event_handlers ⇒ Object
37 38 39 |
# File 'lib/dry/facts/aggregate.rb', line 37 def event_handlers @event_handlers end |
.event_types ⇒ Object
41 42 43 |
# File 'lib/dry/facts/aggregate.rb', line 41 def event_types @event_handlers.keys end |
.find_event_handler(key) ⇒ Object
45 46 47 |
# File 'lib/dry/facts/aggregate.rb', line 45 def find_event_handler(key) (@event_handlers)[key] end |
Instance Method Details
#to_h ⇒ Object Also known as: to_hash
60 61 62 63 64 65 66 |
# File 'lib/dry/facts/aggregate.rb', line 60 def to_h self .instance_variables .map {|ivar_name| ivar_name.to_s.gsub(/@/, '')} .yield_self {|ivars| ivars - ['events', 'uuid']} .reduce(Hash.new) {|memo, obj| memo[obj.to_sym] = self.send(obj); memo } end |