Module: Euston::AggregateRoot::InstanceMethods

Defined in:
lib/euston/aggregate_root.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#aggregate_idObject (readonly)

Returns the value of attribute aggregate_id.



25
26
27
# File 'lib/euston/aggregate_root.rb', line 25

def aggregate_id
  @aggregate_id
end

#logObject

Returns the value of attribute log.



24
25
26
# File 'lib/euston/aggregate_root.rb', line 24

def log
  @log
end

#streamObject (readonly)

Returns the value of attribute stream.



25
26
27
# File 'lib/euston/aggregate_root.rb', line 25

def stream
  @stream
end

Instance Method Details

#committed_messagesObject



35
36
37
# File 'lib/euston/aggregate_root.rb', line 35

def committed_messages
  @committed_messages ||= Set.new
end

#consume_command(headers, command) ⇒ Object



51
52
53
# File 'lib/euston/aggregate_root.rb', line 51

def consume_command headers, command
  consume_message headers, command, :command, Euston::CommandHeaders, :send_command_to_method
end

#consume_event_subscription(headers, event) ⇒ Object



55
56
57
# File 'lib/euston/aggregate_root.rb', line 55

def consume_event_subscription headers, event
  consume_message headers, event, :event_subscription, Euston::EventHeaders, :send_event_subscription_to_method
end

#has_uncommitted_changes?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/euston/aggregate_root.rb', line 31

def has_uncommitted_changes?
  !uncommitted_events.empty?
end

#initial_versionObject



27
28
29
# File 'lib/euston/aggregate_root.rb', line 27

def initial_version
  @initial_version ||= 0
end

#initialize(aggregate_id = nil) ⇒ Object



19
20
21
22
# File 'lib/euston/aggregate_root.rb', line 19

def initialize aggregate_id = nil
  @aggregate_id = aggregate_id
  @log = Euston::NullLogger.instance
end

#take_snapshotObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/euston/aggregate_root.rb', line 59

def take_snapshot
  methods = self.class.instance_methods
  regex = self.class.take_snapshot_regexp
  methods = methods.map { |m| regex.match m }.compact

  raise "You tried to take a snapshot of #{self.class.name} but no snapshot method was found." if methods.empty?

  version = methods.map { |m| m[1].to_i }.sort.last
  name = self.class.take_snapshot_method_name version

  { :version => version, :payload => send(name) }
end

#uncommitted_commandsObject



39
40
41
# File 'lib/euston/aggregate_root.rb', line 39

def uncommitted_commands
  @uncommitted_commands ||= []
end

#uncommitted_eventsObject



43
44
45
# File 'lib/euston/aggregate_root.rb', line 43

def uncommitted_events
  @uncommitted_events ||= []
end

#uncommitted_headersObject



47
48
49
# File 'lib/euston/aggregate_root.rb', line 47

def uncommitted_headers
  @uncommitted_headers ||= {}
end

#versionObject



72
73
74
# File 'lib/euston/aggregate_root.rb', line 72

def version
  initial_version + uncommitted_events.length
end