lib/grumlin/steps.rb in grumlin-0.22.5 vs lib/grumlin/steps.rb in grumlin-0.23.0

- old
+ new

@@ -1,26 +1,19 @@ # frozen_string_literal: true module Grumlin class Steps - CONFIGURATION_STEPS = Action::CONFIGURATION_STEPS - ALL_STEPS = Action::ALL_STEPS + CONFIGURATION_STEPS = Step::CONFIGURATION_STEPS + ALL_STEPS = Step::ALL_STEPS - def self.from(action) - raise ArgumentError, "expected: #{Action}, given: #{action.class}" unless action.is_a?(Action) + def self.from(step) + raise ArgumentError, "expected: #{Step}, given: #{step.class}" unless step.is_a?(Step) - shortcuts = action.shortcuts - actions = [] - - until action.nil? || action.is_a?(TraversalStart) - actions.unshift(action) - action = action.previous_step - end - - new(shortcuts).tap do |chain| - actions.each do |act| - chain.add(act.name, args: act.args, params: act.params) + new(step.shortcuts).tap do |chain| + until step.nil? || step.is_a?(TraversalStart) + chain.add(step.name, args: step.args, params: step.params, to: :begin) + step = step.previous_step end end end attr_reader :configuration_steps, :steps, :shortcuts @@ -29,17 +22,20 @@ @shortcuts = shortcuts @configuration_steps = configuration_steps @steps = steps end - def add(name, args: [], params: {}) + def add(name, args: [], params: {}, to: :end) if CONFIGURATION_STEPS.include?(name) || name.to_sym == :tx - return add_configuration_step(name, args: args, params: params) + return add_configuration_step(name, args: args, params: params, to: to) end StepData.new(name, args: cast_arguments(args), params: params).tap do |step| - @steps << step + next @steps << step if to == :end + next @steps.unshift(step) if to == :begin + + raise ArgumentError, "'to:' must be either :begin or :end, given: '#{to}'" end end def uses_shortcuts? shortcuts?(@configuration_steps) || shortcuts?(@steps) @@ -62,18 +58,21 @@ arg.is_a?(Steps) ? arg.uses_shortcuts? : false end end end - def add_configuration_step(name, args: [], params: {}) - raise ArgumentError, "cannot use configuration steps after start step was used" unless @steps.empty? + def add_configuration_step(name, args: [], params: {}, to: :end) + raise ArgumentError, "cannot use configuration steps after start step was used" if @steps.any? && to == :end StepData.new(name, args: cast_arguments(args), params: params).tap do |step| - @configuration_steps << step + next @configuration_steps << step if to == :end + next @configuration_steps.unshift(step) if to == :begin + + raise ArgumentError, "to must be either :begin or :end, given: '#{to}'" end end def cast_arguments(arguments) - arguments.map { |arg| arg.is_a?(Action) ? Steps.from(arg) : arg } + arguments.map { |arg| arg.is_a?(Step) ? Steps.from(arg) : arg } end end end