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, aggregate_from_snapshot = nil) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/sandthorn/aggregate_root_base.rb', line 130

def aggregate_build events, aggregate_from_snapshot = nil
  aggregate = aggregate_from_snapshot || create_new_empty_aggregate

  if events.any?
    current_aggregate_version = events.last[:aggregate_version]
    aggregate.send :set_orginating_aggregate_version!, current_aggregate_version
    aggregate.send :set_current_aggregate_version!, current_aggregate_version
    aggregate.send :set_aggregate_id, events.first.fetch(:aggregate_id)
  end
  attributes = build_instance_vars_from_events events
  aggregate.send :clear_aggregate_events

  aggregate.default_attributes
  aggregate.send :aggregate_initialize

  aggregate.send :set_instance_variables!, attributes
  aggregate
end

#aggregate_find(aggregate_id) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sandthorn/aggregate_root_base.rb', line 98

def aggregate_find aggregate_id
  begin
    aggregate_from_snapshot = Sandthorn.find_snapshot(aggregate_id) if self.snapshot
    current_aggregate_version = aggregate_from_snapshot.nil? ? 0 : aggregate_from_snapshot.aggregate_current_event_version
    events = Sandthorn.find(aggregate_id, self, current_aggregate_version)
    if aggregate_from_snapshot.nil? && events.empty?
      raise Errors::AggregateNotFound
    end

    return aggregate_build events, aggregate_from_snapshot
  rescue Exception
    raise Errors::AggregateNotFound
  end
    
end

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

Yields:

  • (_self)

Yield Parameters:



65
66
67
68
69
# File 'lib/sandthorn/aggregate_root_base.rb', line 65

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

#allObject



87
88
89
90
91
# File 'lib/sandthorn/aggregate_root_base.rb', line 87

def all
  Sandthorn.all(self).map { |events|
    aggregate_build events, nil
  }
end

#constructor_events(*event_names) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/sandthorn/aggregate_root_base.rb', line 159

def constructor_events(*event_names)
  event_names.each do |name|
    define_singleton_method name do |*args, &block|

      create_new_empty_aggregate.tap  do |aggregate|
        aggregate.aggregate_base_initialize
        aggregate.aggregate_initialize
        aggregate.send :set_aggregate_id, Sandthorn.generate_aggregate_id
        aggregate.instance_eval(&block) if block
        aggregate.send :commit_with_event_name, name.to_s
        return aggregate
      end

    end
    self.singleton_class.class_eval { private name.to_s }
  end
end

#event_store(event_store = nil) ⇒ Object



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

def event_store(event_store = nil)
  if event_store
    @event_store = event_store
  else
    @event_store
  end
end

#events(*event_names) ⇒ Object



177
178
179
180
181
182
183
184
185
# File 'lib/sandthorn/aggregate_root_base.rb', line 177

def events(*event_names)
  event_names.each do |name|
    define_method(name) do |*args, &block|
      block.call() if block
      commit_with_event_name(name.to_s)
    end
    private name.to_s
  end
end

#find(id) ⇒ Object



93
94
95
96
# File 'lib/sandthorn/aggregate_root_base.rb', line 93

def find id
  return aggregate_find id unless id.respond_to?(:each)
  return id.map { |e| aggregate_find e }
end

#new(*args, &block) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/sandthorn/aggregate_root_base.rb', line 114

def new *args, &block
  aggregate = create_new_empty_aggregate()
  aggregate.aggregate_base_initialize
  aggregate.aggregate_initialize

  aggregate.default_attributes
  aggregate.send :initialize, *args, &block
  aggregate.send :set_aggregate_id, Sandthorn.generate_aggregate_id

  aggregate.aggregate_trace @@aggregate_trace_information do |aggr|
    aggr.send :commit
    return aggr
  end

end

#snapshot(value = nil) ⇒ Object



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

def snapshot(value = nil)
  if value
    @snapshot = value
  else
    @snapshot
  end
end

#stateless_events(*event_names) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/sandthorn/aggregate_root_base.rb', line 149

def stateless_events(*event_names)
  event_names.each do |name|
    define_singleton_method name do |aggregate_id, *args|
      event = build_stateless_event(aggregate_id, name.to_s, args)
      Sandthorn.save_events([event], aggregate_id, self)
      return aggregate_id
    end
  end
end