Sha256: 506a0096e107994d106b36db0f4b9828b4b9b70794299ba15fdf35b1399ad137

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Grumlin
  class ShortcutsApplyer
    class << self
      def call(steps)
        new.call(steps)
      end
    end

    def call(steps)
      return steps unless steps.uses_shortcuts?

      shortcuts = steps.shortcuts

      configuration_steps = process_steps(steps.configuration_steps, shortcuts)
      regular_steps = process_steps(steps.steps, shortcuts)

      Steps.new(shortcuts).tap do |processed_steps|
        (configuration_steps + regular_steps).each do |step|
          processed_steps.add(step.name, args: step.args, params: step.params)
        end
      end
    end

    private

    def process_steps(steps, shortcuts) # rubocop:disable Metrics/AbcSize
      steps.each_with_object([]) do |step, result|
        args = step.args.map do |arg|
          arg.is_a?(Steps) ? ShortcutsApplyer.call(arg) : arg
        end

        if shortcuts.include?(step.name)
          t = TraversalStart.new(shortcuts)
          action = shortcuts[step.name].apply(t, *args, **step.params)
          next if action.nil? || action == t # Shortcut did not add any steps

          new_steps = ShortcutsApplyer.call(Steps.from(action))
          result.concat(new_steps.configuration_steps)
          result.concat(new_steps.steps)
        else
          result << StepData.new(step.name, args: args, params: step.params)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
grumlin-0.18.1 lib/grumlin/shortcuts_applyer.rb
grumlin-0.18.0 lib/grumlin/shortcuts_applyer.rb