Module: BetterService::Workflows

Defined in:
lib/better_service/workflows/base.rb,
lib/better_service/workflows/dsl.rb,
lib/better_service/workflows/branch.rb,
lib/better_service/workflows/execution.rb,
lib/better_service/workflows/branch_dsl.rb,
lib/better_service/workflows/branch_group.rb,
lib/better_service/workflows/result_builder.rb,
lib/better_service/workflows/rollback_support.rb,
lib/better_service/workflows/transaction_support.rb

Overview

Workflow - Base class for composing multiple services into a pipeline

Workflows allow you to chain multiple services together with explicit data mapping, conditional execution, rollback support, and lifecycle hooks.

Example:

class OrderPurchaseWorkflow < BetterService::Workflow
  with_transaction true

  before_workflow :validate_cart
  after_workflow :clear_cart

  step :create_order,
       with: Order::CreateService,
       input: ->(ctx) { { items: ctx.cart_items, total: ctx.total } }

  step :charge_payment,
       with: Payment::ChargeService,
       input: ->(ctx) { { amount: ctx.order.total } },
       rollback: ->(ctx) { Payment::RefundService.new(ctx.user, params: { charge_id: ctx.charge.id }).call }

  step :send_email,
       with: Email::ConfirmationService,
       input: ->(ctx) { { order_id: ctx.order.id } },
       optional: true,
       if: ->(ctx) { ctx.user.notifications_enabled? }

  private

  def validate_cart(context)
    context.fail!("Cart is empty") if context.cart_items.empty?
  end

  def clear_cart(context)
    context.user.clear_cart! if context.success?
  end
end

# Usage:
result = OrderPurchaseWorkflow.new(current_user, params: { cart_items: [...] }).call
if result[:success]
  order = result[:context].order
else
  errors = result[:errors]
end

Defined Under Namespace

Modules: DSL, Execution, ResultBuilder, RollbackSupport, TransactionSupport Classes: Base, Branch, BranchDSL, BranchGroup