Sha256: 781821214de491364a0420443056753783277a56c16946f94310f75ef8f1fc4a

Contents?: true

Size: 1.73 KB

Versions: 1

Compression:

Stored size: 1.73 KB

Contents

module Rung
  module Definition
    module StepsDSL
      def steps_definition
        @steps_definition ||= []
      end

      def add_generic_step(action, options = {}, &block)
        step = step_from_definition action, **options, &block
        steps_definition.push step
      end

      def step(*args, &block)
        add_step_from_args args, &block
      end

      def tee(*args, &block)
        add_step_from_args args, ignore_result: true, &block
      end

      def failure(*args, &block)
        add_step_from_args args, run_on: :failure, ignore_result: true, &block
      end

      def always(*args, &block)
        add_step_from_args args, run_on: :any, ignore_result: true, &block
      end

      private

      def add_step_from_args(args, options = {}, &block)
        action, action_options = extract_action_and_options!(args)
        add_generic_step action, **options, **action_options, &block
      end

      def extract_action_and_options!(args)
        options = args.last.is_a?(::Hash) ? args.pop : {}
        [args.first, options]
      end

      def step_from_definition(action, options, &block)
        if action && block
          NestedStep.new(action, calculate_block_nested_steps(&block), options)
        elsif block
          Step.new(block, **options, from_block: true)
        else
          Step.new(action, options)
        end
      end

      def calculate_block_nested_steps(&block)
        with_new_steps_definition do
          instance_exec(&block)
          steps_definition
        end
      end

      def with_new_steps_definition
        old_steps_definition = @steps_definition
        @steps_definition = []
        yield
      ensure
        @steps_definition = old_steps_definition
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rung-0.1 lib/rung/definition/steps_dsl.rb