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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/sandthorn/aggregate_root_base.rb', line 109

def aggregate_build events
  if first_event_snapshot?(events)
    aggregate = events.first[:aggregate]
    events.shift
  else
    aggregate = create_new_empty_aggregate
  end

  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
  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



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

def aggregate_find aggregate_id
  events = Sandthorn.get_aggregate(aggregate_id, self)
  unless events && !events.empty?
    raise Sandthorn::Errors::AggregateNotFound
  end

  aggregate_build events
end

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

Yields:

  • (_self)

Yield Parameters:



60
61
62
63
64
# File 'lib/sandthorn/aggregate_root_base.rb', line 60

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

#allObject



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

def all
  aggregate_id_list = Sandthorn.get_aggregate_list_by_type(self)
  find aggregate_id_list
end

#event_store(event_store = nil) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/sandthorn/aggregate_root_base.rb', line 66

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

#events(*event_names) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/sandthorn/aggregate_root_base.rb', line 133

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, args)
    end
    private name.to_s
  end
end

#find(id) ⇒ Object



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

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

#new(*args, &block) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/sandthorn/aggregate_root_base.rb', line 93

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, *args
    return aggr
  end

end