Class: Dry::Facts::Aggregate

Inherits:
Object
  • Object
show all
Defined in:
lib/dry/facts/aggregate.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#eventsObject (readonly)

Returns the value of attribute events.



10
11
12
# File 'lib/dry/facts/aggregate.rb', line 10

def events
  @events
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/dry/facts/aggregate.rb', line 11

def id
  @id
end

#uuidObject (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_handlersObject



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