Class: Easyop::FlowBuilder
- Inherits:
-
Object
- Object
- Easyop::FlowBuilder
- Defined in:
- lib/easyop/flow_builder.rb
Overview
FlowBuilder accumulates callbacks before executing a flow. Returned by FlowClass.flow (no args) and FlowClass.result.
Usage:
ProcessCheckout.flow
.on_success { |ctx| redirect_to order_path(ctx.order) }
.on_failure { |ctx| flash[:error] = ctx.error; redirect_back }
.call(user: current_user, cart: current_cart)
# With bound object (e.g. a Rails controller):
ProcessCheckout.flow
.bind_with(self)
.on(success: :redirect_to_dashboard, fail: :render_form)
.call(user: current_user, cart: current_cart)
Instance Method Summary collapse
-
#bind_with(obj) ⇒ Object
Bind a context object for use with symbol shortcuts in ‘.on(…)`.
-
#call(attrs = {}) ⇒ Object
Execute the flow with the given attributes, then fire the registered callbacks.
-
#initialize(flow_class) ⇒ FlowBuilder
constructor
A new instance of FlowBuilder.
-
#on(success: nil, fail: nil) ⇒ Object
Register named-method callbacks.
-
#on_failure(&block) ⇒ Object
Register a callback to run when the flow fails.
-
#on_success(&block) ⇒ Object
Register a callback to run when the flow succeeds.
Constructor Details
#initialize(flow_class) ⇒ FlowBuilder
Returns a new instance of FlowBuilder.
17 18 19 20 21 22 |
# File 'lib/easyop/flow_builder.rb', line 17 def initialize(flow_class) @flow_class = flow_class @success_callbacks = [] @failure_callbacks = [] @bound_object = nil end |
Instance Method Details
#bind_with(obj) ⇒ Object
Bind a context object for use with symbol shortcuts in ‘.on(…)`. Typically `self` in a Rails controller.
38 39 40 41 |
# File 'lib/easyop/flow_builder.rb', line 38 def bind_with(obj) @bound_object = obj self end |
#call(attrs = {}) ⇒ Object
Execute the flow with the given attributes, then fire the registered callbacks. Returns the ctx (Easyop::Ctx).
62 63 64 65 66 67 |
# File 'lib/easyop/flow_builder.rb', line 62 def call(attrs = {}) ctx = @flow_class.call(attrs) callbacks = ctx.success? ? @success_callbacks : @failure_callbacks callbacks.each { |cb| cb.call(ctx) } ctx end |
#on(success: nil, fail: nil) ⇒ Object
Register named-method callbacks. Requires bind_with to have been called when the methods live on another object.
.on(success: :redirect_to_dashboard, fail: :render_form)
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/easyop/flow_builder.rb', line 47 def on(success: nil, fail: nil) bound = @bound_object if success success_name = success @success_callbacks << ->(ctx) { _invoke_named(bound, success_name, ctx) } end if fail fail_name = fail @failure_callbacks << ->(ctx) { _invoke_named(bound, fail_name, ctx) } end self end |
#on_failure(&block) ⇒ Object
Register a callback to run when the flow fails.
31 32 33 34 |
# File 'lib/easyop/flow_builder.rb', line 31 def on_failure(&block) @failure_callbacks << block self end |
#on_success(&block) ⇒ Object
Register a callback to run when the flow succeeds.
25 26 27 28 |
# File 'lib/easyop/flow_builder.rb', line 25 def on_success(&block) @success_callbacks << block self end |