Module: SimpleEventSourcing::AggregateRoot::Base

Defined in:
lib/simple_event_sourcing/aggregate_root/base.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aggregate_idObject

Returns the value of attribute aggregate_id.



6
7
8
# File 'lib/simple_event_sourcing/aggregate_root/base.rb', line 6

def aggregate_id
  @aggregate_id
end

Class Method Details

.included(o) ⇒ Object



42
43
44
# File 'lib/simple_event_sourcing/aggregate_root/base.rb', line 42

def self.included(o)
  o.extend(ClassMethods)
end

Instance Method Details

#apply_event(event) ⇒ Object



26
27
28
29
# File 'lib/simple_event_sourcing/aggregate_root/base.rb', line 26

def apply_event(event)
  handler = self.class.event_mapping[event.class]
  self.instance_exec(event, &handler) if handler
end

#apply_record_event(event_class, args = {}) ⇒ Object



31
32
33
34
35
36
# File 'lib/simple_event_sourcing/aggregate_root/base.rb', line 31

def apply_record_event(event_class, args = {})
  args[:aggregate_id] ||= aggregate_id.value
  event = event_class.new(args)
  apply_event event
  record_event event
end

#count_eventsObject



16
17
18
# File 'lib/simple_event_sourcing/aggregate_root/base.rb', line 16

def count_events
  @events.count
end

#have_changed?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/simple_event_sourcing/aggregate_root/base.rb', line 12

def have_changed?
  (@events.count > 0)
end

#idObject



38
39
40
# File 'lib/simple_event_sourcing/aggregate_root/base.rb', line 38

def id
  @aggregate_id.value
end

#initialize(_args = nil) ⇒ Object



8
9
10
# File 'lib/simple_event_sourcing/aggregate_root/base.rb', line 8

def initialize(_args = nil)
  @events = []
end

#publishObject



20
21
22
23
24
# File 'lib/simple_event_sourcing/aggregate_root/base.rb', line 20

def publish
  published_events = @events
  clear_events
  published_events
end