Module: Easyop::Operation

Defined in:
lib/easyop/operation.rb

Overview

The core module. Include this in any class to turn it into an operation.

Usage:

class DoSomething
  include Easyop::Operation

  def call
    ctx.fail!(error: "nope") unless ctx.allowed
    ctx.result = do_work(ctx.input)
  end
end

ctx = DoSomething.call(input: "data", allowed: true)
ctx.success?  # => true
ctx.result    # => ...

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/easyop/operation.rb', line 18

def self.included(base)
  base.extend(ClassMethods)
  base.include(Hooks)
  base.include(Rescuable)
  base.include(Skip)
  base.include(Schema)
end

Instance Method Details

#_easyop_run(ctx, raise_on_failure:) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/easyop/operation.rb', line 77

def _easyop_run(ctx, raise_on_failure:)
  @ctx = ctx
  if raise_on_failure
    _run_raising
  else
    _run_safe
  end
  ctx
end

#callObject

Override this in subclasses.



65
66
67
# File 'lib/easyop/operation.rb', line 65

def call
  # no-op default
end

#ctxObject

The shared context. Available inside ‘call`, hooks, and rescue handlers.



60
61
62
# File 'lib/easyop/operation.rb', line 60

def ctx
  @ctx
end

#rollbackObject

Override to add rollback logic for use in Flow.



70
71
72
# File 'lib/easyop/operation.rb', line 70

def rollback
  # no-op default
end