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

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



62
63
64
65
66
# File 'lib/easyop/flow.rb', line 62

def self.included(base)
  base.include(Operation)
  base.extend(ClassMethods)
  base.prepend(CallBehavior)
end