Class: Decider::Reactor::Module

Inherits:
Module
  • Object
show all
Defined in:
lib/decider/reactor.rb

Defined Under Namespace

Classes: React

Constant Summary collapse

REACT_FALLBACK =
proc { [nil, proc {}] }

Instance Method Summary collapse

Constructor Details

#initialize(reactions:) ⇒ Module

Returns a new instance of Module.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/decider/reactor.rb', line 14

def initialize(reactions:)
  define_method(:react) do |action_result|
    context = React.new(action_result: action_result, _actions: [])

    reactions.find(REACT_FALLBACK) do |arg, _|
      case arg
      in Proc => fn
        context.instance_exec(&fn)
      in artype
        action_result in ^artype
      else
        false
      end
    end => [_, handler]

    context.instance_exec(&handler)
    context._actions
  end

  define_method(:lmap_on_action_result) do |fn|
    Decider::Reactor.lmap_on_action_result(fn, self)
  end

  define_method(:rmap_on_action) do |fn|
    Decider::Reactor.rmap_on_action(fn, self)
  end

  define_method(:map_on_action) do |fn|
    Decider::Reactor.rmap_on_action(fn, self)
  end

  define_method(:combine_with_decider) do |decider|
    Decider::Reactor.combine_with_decider(self, decider)
  end
end