module Sandthorn::AggregateRoot::Base

Attributes

aggregate_current_event_version[R]
aggregate_events[R]
aggregate_id[R]
aggregate_originating_version[R]
aggregate_trace_information[R]
aggregate_version[R]
id[R]

Public Instance Methods

==(other) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 37
def ==(other)
  other.respond_to?(:aggregate_id) && aggregate_id == other.aggregate_id
end
aggregate_base_initialize() click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 15
def aggregate_base_initialize
  @aggregate_current_event_version = 0
  @aggregate_originating_version = 0
  @aggregate_events = []
end
aggregate_trace(args) { |self| ... } click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 45
def aggregate_trace args
  @aggregate_trace_information = args
  yield self if block_given?
  @aggregate_trace_information = nil
end
commit() click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 51
def commit
  event_name = caller_locations(1,1)[0].label.gsub(/block ?(.*) in /, "")
  commit_with_event_name(event_name)
end
Also aliased as: record_event
default_attributes() click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 56
def default_attributes
  #NOOP
end
record_event()
Alias for: commit
save() click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 21
def save
  if aggregate_events.any?
    Sandthorn.save_events(
      aggregate_events,
      aggregate_id,
      self.class
    )
    @aggregate_events = []
    @aggregate_originating_version = @aggregate_current_event_version
  end

  Sandthorn.save_snapshot self if self.class.snapshot

  self
end
unsaved_events?() click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 41
def unsaved_events?
  aggregate_events.any?
end

Private Instance Methods

aggregate_clear_current_event_version!() click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 254
def aggregate_clear_current_event_version!
  @aggregate_current_event_version = 0
end
clear_aggregate_events() click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 250
def clear_aggregate_events
  @aggregate_events = []
end
commit_with_event_name(event_name) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 262
def commit_with_event_name(event_name)
  increase_current_aggregate_version!

  @aggregate_events << ({
    aggregate_version: @aggregate_current_event_version,
    aggregate_id: @aggregate_id,
    event_name: event_name,
    event_data: get_delta(),
    event_metadata: @aggregate_trace_information
  })

  self
end
extract_relevant_aggregate_instance_variables() click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 232
def extract_relevant_aggregate_instance_variables
  instance_variables.select do |variable|
     !variable.to_s.start_with?("@aggregate_")
  end
end
increase_current_aggregate_version!() click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 242
def increase_current_aggregate_version!
  @aggregate_current_event_version += 1
end
set_aggregate_id(aggregate_id) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 258
def set_aggregate_id aggregate_id
  @aggregate_id = aggregate_id
end
set_current_aggregate_version!(aggregate_version) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 246
def set_current_aggregate_version! aggregate_version
  @aggregate_current_event_version = aggregate_version
end
set_instance_variables!(attributes) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 226
def set_instance_variables! attributes
  attributes.each_pair do |k,v|
    self.instance_variable_set "@#{k}", v
  end
end
set_orginating_aggregate_version!(aggregate_version) click to toggle source
# File lib/sandthorn/aggregate_root_base.rb, line 238
def set_orginating_aggregate_version! aggregate_version
  @aggregate_originating_version = aggregate_version
end