Class: Flows::Flow::Router::Custom
- Inherits:
-
Flows::Flow::Router
- Object
- Flows::Flow::Router
- Flows::Flow::Router::Custom
- Defined in:
- lib/flows/flow/router/custom.rb
Overview
Router with custom rules.
Expects routes table in a special format:
{
->(x) { x.ok? } => :step_a,
->(x) { x.err? && x.status == :validation_error } => :end,
->(x) { x.err? } => :handle_error
}
Yes, it's confusing. But by including Result::Helpers you can (and should) rewrite it like this:
{
match_ok => :route_a, # on success go to step_a
match_err(:validation_error) => :end, # on failure with status `:validation_error` - stop execution
match_err => :handle_error # on any other failure go to the step handle_error
}
So, your routes table is an ordered set of pairs predicate => route
in form of Ruby Hash.
Any time you writing a router table you can imagine that you're writing case
:
case step_result
when match_ok then :route_a # on success go to step_a
when match_err(:validation_error) then :end # on failure with status `:validation_error` - stop execution
when match_err then :handle_error # on any other failure go to the step handle_error
end
Instance Method Summary collapse
- #call(result) ⇒ Object
- #destinations ⇒ Object
-
#initialize(routes) ⇒ Custom
constructor
Creates a new custom router from a route table.
Constructor Details
#initialize(routes) ⇒ Custom
Creates a new custom router from a route table.
39 40 41 |
# File 'lib/flows/flow/router/custom.rb', line 39 def initialize(routes) @routes = routes end |
Instance Method Details
#call(result) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/flows/flow/router/custom.rb', line 44 def call(result) @routes.each_pair do |predicate, route| return route if predicate === result # rubocop:disable Style/CaseEquality end raise NoRouteError, "no route found for: `#{result.inspect}`" end |
#destinations ⇒ Object
53 54 55 |
# File 'lib/flows/flow/router/custom.rb', line 53 def destinations @routes.values end |