Module: Easyop::Flow
- Defined in:
- lib/easyop/flow.rb
Overview
Compose a sequence of operations that share a single ctx.
If any step calls ctx.fail!, execution halts and rollback runs in reverse. Each step can define a ‘rollback` method which will be called on failure.
Usage:
class ProcessOrder
include Easyop::Flow
flow ValidateCart, ChargeCard, CreateOrder, NotifyUser
end
## Recording plugin integration (flow tracing)
When steps have the Recording plugin installed, ‘CallBehavior#call` forwards the parent ctx keys so every step log entry shows the flow as its parent:
# Bare flow — flow itself is not recorded but steps see it as parent:
class ProcessOrder
include Easyop::Flow
flow ValidateCart, ChargeCard
end
For full tree reconstruction (flow appears in operation_logs as the root entry) inherit from your recorded base class and opt out of the transaction so step-level transactions are not shadowed by an outer one:
class ProcessOrder < ApplicationOperation
include Easyop::Flow
transactional false # EasyOp handles rollback; steps own their transactions
flow ValidateCart, ChargeCard
end
result = ProcessOrder.call(user: user, cart: cart)
result.on_success { |ctx| redirect_to order_path(ctx.order) }
result.on_failure { |ctx| flash[:alert] = ctx.error }
Steps are run via ‘.call!` so a failure raises and stops the chain. Individual steps can also be conditionally skipped:
flow ValidateCart,
-> (ctx) { ctx.coupon_code? }, ApplyCoupon, # conditional
ChargeCard,
CreateOrder
A Lambda/Proc before a step is treated as a guard — the step only runs if the lambda returns truthy when called with ctx.
Defined Under Namespace
Modules: CallBehavior, ClassMethods
Class Method Summary collapse
Class Method Details
.included(base) ⇒ Object
110 111 112 113 114 |
# File 'lib/easyop/flow.rb', line 110 def self.included(base) base.include(Operation) base.extend(ClassMethods) base.prepend(CallBehavior) end |