lib/trailblazer/operation/railway/normalizer.rb in trailblazer-operation-0.1.3 vs lib/trailblazer/operation/railway/normalizer.rb in trailblazer-operation-0.2.0

- old
+ new

@@ -3,75 +3,56 @@ # The {Normalizer} is called for every DSL call (step/pass/fail etc.) and normalizes/defaults # the user options, such as setting `:id`, connecting the task's outputs or wrapping the user's # task via {TaskBuilder} in order to translate true/false to `Right` or `Left`. # # The Normalizer sits in the `@builder`, which receives all DSL calls from the Operation subclass. - class Normalizer - def initialize(task_builder: TaskBuilder, **options) - @task_builder = task_builder - end + module Normalizer + Pipeline = Activity::Magnetic::Normalizer::Pipeline.clone - def call(task, options, unknown_options, sequence_options) - wrapped_task, options = - if task.is_a?(::Hash) # macro. - [ - task[:task], - task.merge(options) # Note that the user options are merged over the macro options. - ] - elsif task.is_a?(Array) # TODO remove in 2.2 - Operation::DeprecatedMacro.( *task ) - else # user step - [ - @task_builder.(task), - { id: task }.merge(options) # default :id - ] - end + Pipeline.module_eval do + # Handle the :override option which is specific to Operation. + def self.override(ctx, task:, options:, sequence_options:, **) + options, locals = Activity::Magnetic::Options.normalize(options, [:override]) + sequence_options = sequence_options.merge( replace: options[:id] ) if locals[:override] - options, unknown_options = deprecate_name(options, unknown_options) # TODO remove in 2.2 + ctx[:options], ctx[:sequence_options] = options, sequence_options + end - raise "No :id given for #{wrapped_task}" unless options[:id] + # TODO remove in 2.2 + def self.deprecate_macro_with_two_args(ctx, task:, **) + return true unless task.is_a?(Array) # TODO remove in 2.2 - options = defaultize(task, options) # :plus_poles + ctx[:options] = Operation::DeprecatedMacro.( *task ) + end + # TODO remove in 2.2 + def self.deprecate_name(ctx, local_options:, connection_options:, **) + connection_options, deprecated_options = Activity::Magnetic::Options.normalize(connection_options, [:name]) + local_options, _deprecated_options = Activity::Magnetic::Options.normalize(local_options, [:name]) - options, locals, sequence_options = override(task, options, sequence_options) # :override + deprecated_options = deprecated_options.merge(_deprecated_options) - return wrapped_task, options, unknown_options, sequence_options - end + local_options = local_options.merge( name: deprecated_options[:name] ) if deprecated_options[:name] - # Merge user options over defaults. - def defaultize(task, options) - { - plus_poles: InitialPlusPoles(), - }.merge(options) - end + local_options, locals = Activity::Magnetic::Options.normalize(local_options, [:name]) + if locals[:name] + warn "[Trailblazer] The :name option for #step, #success and #failure has been renamed to :id." + local_options = local_options.merge(id: locals[:name]) + end - # Handle the :override option which is specific to Operation. - def override(task, options, sequence_options) - options, locals = Activity::Magnetic::Builder.normalize(options, [:override]) - sequence_options = sequence_options.merge( replace: options[:id] ) if locals[:override] + ctx[:local_options], ctx[:connection_options] = local_options, connection_options + end - return options, locals, sequence_options - end + def self.raise_on_missing_id(ctx, local_options:, **) + raise "No :id given for #{local_options[:task]}" unless local_options[:id] + true + end - def InitialPlusPoles - Activity::Magnetic::DSL::PlusPoles.new.merge( - Activity.Output(Activity::Right, :success) => nil, - Activity.Output(Activity::Left, :failure) => nil, - ) + # add more normalization tasks to the existing Magnetic::Normalizer::Pipeline + task Activity::TaskBuilder::Binary.( method(:deprecate_macro_with_two_args) ), before: "split_options" + task Activity::TaskBuilder::Binary.( method(:deprecate_name) ) + task Activity::TaskBuilder::Binary.( method(:override) ) + task Activity::TaskBuilder::Binary.( method(:raise_on_missing_id) ) end - - def deprecate_name(options, unknown_options) # TODO remove in 2.2 - unknown_options, deprecated_options = Activity::Magnetic::Builder.normalize(unknown_options, [:name]) - - options = options.merge( name: deprecated_options[:name] ) if deprecated_options[:name] - - options, locals = Activity::Magnetic::Builder.normalize(options, [:name]) - if locals[:name] - warn "[Trailblazer] The :name option for #step, #success and #failure has been renamed to :id." - options = options.merge(id: locals[:name]) - end - return options, unknown_options - end - end + end # Normalizer end end