Module: Sandthorn::AggregateRoot::Base::ClassMethods

Defined in:
lib/sandthorn/aggregate_root_base.rb

Constant Summary collapse

@@aggregate_trace_information =
nil

Instance Method Summary collapse

Instance Method Details

#aggregate_build(events) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/sandthorn/aggregate_root_base.rb', line 90

def aggregate_build events
  first_event = events.first()
  current_aggregate_version = 0
  if first_event[:event_name] == "aggregate_set_from_snapshot"
    aggregate = first_event[:event_args][0]
    current_aggregate_version = aggregate.aggregate_originating_version
    events.shift
  else
    new_args = events.first()[:event_args][:method_args]

    if new_args.nil?
      aggregate = new
    else
      aggregate = new *new_args
    end
    aggregate.send :aggregate_clear_current_event_version!
  end

  attributes = {}
  events.each do |event|
    event_args = event[:event_args]
    event_name = event[:event_name]

    next if event_name == "aggregate_set_from_snapshot"
    next if event_name == "instance_extended_as_aggregate"

    attribute_deltas = event_args[:attribute_deltas]

    unless event[:aggregate_version].nil?
      current_aggregate_version = event[:aggregate_version]
    end

    unless attribute_deltas.nil?
      deltas = attribute_deltas.each_with_object({}) do |delta, acc|
        acc[delta[:attribute_name]] = delta[:new_value]
      end

      attributes.merge! deltas
    end
  end
  aggregate.send :clear_aggregate_events
  aggregate.send :set_orginating_aggregate_version!, current_aggregate_version
  aggregate.send :set_current_aggregate_version!, current_aggregate_version
  aggregate.send :set_instance_variables!, attributes
  aggregate
end

#aggregate_trace(args) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



61
62
63
64
65
66
67
# File 'lib/sandthorn/aggregate_root_base.rb', line 61

def aggregate_trace args
  @@aggregate_trace_information = args
  @aggregate_trace_information = args
  yield self
  @@aggregate_trace_information = nil
  @aggregate_trace_information = nil
end

#find(aggregate_id) ⇒ Object



69
70
71
72
73
74
75
76
# File 'lib/sandthorn/aggregate_root_base.rb', line 69

def find aggregate_id
  class_name = self.respond_to?(:name) ? self.name : self.class # to be able to extend a string for example.
  events = Sandthorn.get_aggregate(aggregate_id, class_name)
  raise Sandthorn::Errors::AggregateNotFound unless events and !events.empty?

  transformed_events = events.map { |e| e.merge(event_args: Sandthorn.deserialize(e[:event_data])) }
  aggregate_build transformed_events
end

#new(*args) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/sandthorn/aggregate_root_base.rb', line 78

def new *args
  aggregate = super
  aggregate.aggregate_base_initialize

  aggregate.aggregate_trace @@aggregate_trace_information do |aggr|
    aggr.aggregate_initialize
    aggr.send :set_aggregate_id, Sandthorn.generate_aggregate_id
    aggr.send :commit, *args, method_name: "new"
    return aggr
  end
end