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.



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

#eventsObject (readonly)

Returns the value of attribute events.



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

def events
  @events
end

#idObject

Returns the value of attribute id.



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

def id
  @id
end

#uuidObject

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_handlersObject



37
38
39
# File 'lib/dry/facts/aggregate.rb', line 37

def event_handlers
  @event_handlers
end

.event_typesObject



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_hObject 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