Class: Operandi::Settings::Step

Inherits:
Object
  • Object
show all
Defined in:
lib/operandi/settings/step.rb

Overview

Stores configuration for a single service step. Created automatically when using the step DSL method.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, service_class, opts = {}) ⇒ Step

Initialize a new step definition.

Parameters:

  • name (Symbol)

    the step name (must match a method)

  • service_class (Class)

    the service class this step belongs to

  • opts (Hash) (defaults to: {})

    step options

Options Hash (opts):

  • :if (Symbol, Proc)

    condition to run the step

  • :unless (Symbol, Proc)

    condition to skip the step

  • :always (Boolean)

    run even after errors/warnings

Raises:

  • (Error)

    if both :if and :unless are specified



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/operandi/settings/step.rb', line 23

def initialize(name, service_class, opts = {})
  @name = name
  @service_class = service_class

  @if     = opts[:if]
  @unless = opts[:unless]
  @always = opts[:always]

  if @if && @unless
    raise Operandi::Error, "#{service_class} `if` and `unless` cannot be specified " \
                           "for the step `#{name}` at the same time"
  end
end

Instance Attribute Details

#alwaysBoolean? (readonly)

Returns true if step runs even after errors/warnings.

Returns:

  • (Boolean, nil)

    true if step runs even after errors/warnings



12
13
14
# File 'lib/operandi/settings/step.rb', line 12

def always
  @always
end

#nameSymbol (readonly)

Returns the step name (method to call).

Returns:

  • (Symbol)

    the step name (method to call)



9
10
11
# File 'lib/operandi/settings/step.rb', line 9

def name
  @name
end

Instance Method Details

#run(instance) ⇒ Boolean

Execute the step on the given service instance.

Parameters:

  • instance (Base)

    the service instance

Returns:

  • (Boolean)

    true if the step was executed, false if skipped

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/operandi/settings/step.rb', line 42

def run(instance) # rubocop:disable Naming/PredicateMethod
  return false unless run?(instance)

  unless instance.respond_to?(name, true)
    available_steps = @service_class.steps.keys.join(", ")
    message = "Step method `#{name}` is not defined in #{@service_class}. " \
              "Defined steps: [#{available_steps}]"
    raise Operandi::RuntimeError.new(message, service: instance)
  end

  execute_with_callbacks(instance)
  true
end