Module: EventSorcerer::Aggregate::ClassMethods

Defined in:
lib/event_sorcerer/aggregate.rb

Overview

Public: Class methods to be extended onto the including class.

Instance Method Summary collapse

Instance Method Details

#allObject

Public: Load all Aggregates of this type.

Returns an Array of AggregateProxy objects.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/event_sorcerer/aggregate.rb', line 25

def all
  all_loaders_for_type.map do |loader|
    cached = unit_of_work.fetch_aggregate(to_s, loader.id)

    next cached if cached

    loader.load.tap do |aggregate|
      unit_of_work.store_aggregate(aggregate)
    end
  end
end

#event_methodsObject

Public: An array of symbols representing the names of the methods which

are events. Includes event methods of superclass.


39
40
41
42
43
44
45
# File 'lib/event_sorcerer/aggregate.rb', line 39

def event_methods
  if superclass.respond_to? :event_methods
    _event_methods.concat(superclass.event_methods).uniq
  else
    _event_methods
  end
end

#events(&block) ⇒ Object

Public: Methods defined within this block will have their method symbol

added to the event_methods array.

block - block containing the event method definitions.

Returns self.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/event_sorcerer/aggregate.rb', line 53

def events(&block)
  test_class ||= Class.new(BasicObject)
  starting_methods = test_class.instance_methods
  test_class.class_eval(&block)

  new_events = test_class.instance_methods.select do |method|
    !starting_methods.include? method
  end

  _event_methods.concat new_events
  class_eval(&block)

  self
end

#find(id_or_ids) ⇒ Object

Public: Load an aggregate(s) out of the event store.

id_or_ids - the ID of the aggregate to load or an array of the same.

Returns an AggregateProxy object or an array of the same.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/event_sorcerer/aggregate.rb', line 73

def find(id_or_ids)
  return find_many(id_or_ids) if id_or_ids.respond_to?(:each)

  if unit_of_work.fetch_aggregate(to_s, id_or_ids)
    return unit_of_work.fetch_aggregate(to_s, id_or_ids)
  end

  loader_for_id(id_or_ids).load.tap do |aggregate|
    unit_of_work.store_aggregate(aggregate)
  end
end

#find_or_new(id) ⇒ Object

Public: Load an aggregate out of the event store or create new.

id - the ID of the aggregate to load.

Returns an AggregateProxy object.



90
91
92
93
94
95
96
97
98
# File 'lib/event_sorcerer/aggregate.rb', line 90

def find_or_new(id)
  if unit_of_work.fetch_aggregate(to_s, id)
    return unit_of_work.fetch_aggregate(to_s, id)
  end

  loader_for_id(id, false).load.tap do |aggregate|
    unit_of_work.store_aggregate(aggregate)
  end
end

#new(id = EventSorcerer.generate_id) ⇒ Object

Public: Creates a new aggregate.

id - the ID to set on the new aggregate.

Returns an AggregateProxy object.



105
106
107
108
109
# File 'lib/event_sorcerer/aggregate.rb', line 105

def new(id = EventSorcerer.generate_id)
  AggregateCreator.new(self, id).create.tap do |aggregate|
    unit_of_work.store_aggregate(aggregate)
  end
end