Module: Sourced::Evolve

Included in:
Decider, Projector
Defined in:
lib/sourced/evolve.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

PREFIX =
'evolution'
NOOP_HANDLER =
->(*_) { nil }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
# File 'lib/sourced/evolve.rb', line 8

def self.included(base)
  super
  base.extend ClassMethods
end

Instance Method Details

#evolve(*args) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sourced/evolve.rb', line 13

def evolve(*args)
  state = self

  case args
  in [events]
    events.each do |event|
      method_name = Sourced.message_method_name(Evolve::PREFIX, event.class.to_s)
      if respond_to?(method_name)
        before_evolve(event)
        send(method_name, event) 
    end
    end
  in [obj, events]
    state = obj
    events.each do |event|
      method_name = Sourced.message_method_name(Evolve::PREFIX, event.class.to_s)
      if respond_to?(method_name)
        before_evolve(state, event)
        send(method_name, state, event)
      end
    end
  end

  state
end