Class: Emit::Alternation

Inherits:
Object
  • Object
show all
Defined in:
lib/emit/alternation.rb

Instance Method Summary collapse

Constructor Details

#initialize(guards) ⇒ Alternation



3
4
5
# File 'lib/emit/alternation.rb', line 3

def initialize(guards)
  @guards = guards
end

Instance Method Details

#executeObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/emit/alternation.rb', line 7

def execute
  idx, request, channel, operation = choose

  if @guards[idx]
    action = @guards[idx].last[:action]

    case action
    when Choice
      case operation
      when :write then action.invoke_on_output
      when :read  then action.invoke_on_input(request.message)
      end
    when Proc, Method
      case operation
      when :write then action.()
      when :read  then action.(message: request.message)
      end
    when nil
      # no-op
    else
      fail "Failed executing action: #{action}."
    end
  end

  [channel, request.message]
end