Class: Helpstation::Processors::ConditionalProcessor

Inherits:
Helpstation::Processor show all
Defined in:
lib/helpstation/processors.rb

Overview

Helps to run one chain or another depending on a request

Examples:

success_chain = substation.chain do
  # success action
end

fallback_chain = substation.chain do
  # fallback action
end

process ConditionalProcessor[
  -> (request) { request.input.has_key?(:optional_key) },
  success_chain,
  fallback_chain
]

Class Method Summary collapse

Methods inherited from Helpstation::Processor

#success

Methods inherited from Evaluator

call, #initialize

Constructor Details

This class inherits a constructor from Helpstation::Evaluator

Class Method Details

.[](condition, success_chain, fallback_chain) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/helpstation/processors.rb', line 65

def self.[](condition, success_chain, fallback_chain)
  Proc.new do |request|
    result =
      if condition.call(request)
        success_chain.call(request)
      else
        fallback_chain.call(request)
      end

    if result.is_a?(Substation::Response)
      result
    else
      request.success(result.input)
    end
  end
end