lib/trailblazer/activity/path.rb in trailblazer-activity-dsl-linear-0.4.1 vs lib/trailblazer/activity/path.rb in trailblazer-activity-dsl-linear-0.4.2

- old
+ new

@@ -79,17 +79,32 @@ :replace => :Replace, :delete => :Delete, } end + def normalize_duplications((ctx, flow_options), *) + return Right, [ctx, flow_options] if ctx[:replace] + + signal, (ctx, flow_options) = raise_on_duplicate_id([ctx, flow_options]) + signal, (ctx, flow_options) = clone_duplicate_activity([ctx, flow_options]) + + return signal, [ctx, flow_options] + end + def raise_on_duplicate_id((ctx, flow_options), *) - id, sequence, insert = ctx[:id], ctx[:sequence], ctx[:sequence_insert][0] # DISCUSS: should we use Replace here or rather the option(s)? + id, sequence = ctx[:id], ctx[:sequence] + raise "ID #{id} is already taken. Please specify an `:id`." if sequence.find { |row| row[3][:id] == id } - if insert != Linear::Insert.method(:Replace) - raise "ID #{id} is already taken. Please specify an `:id`." if sequence.find { |row| row[3][:id] == id } - end + return Right, [ctx, flow_options] + end + def clone_duplicate_activity((ctx, flow_options), *) + return Right, [ctx, flow_options] unless ctx[:task].is_a?(Class) + + task, sequence = ctx[:task], ctx[:sequence] + ctx = ctx.merge(task: task.clone) if sequence.find { |row| row[1] == task } + return Right, [ctx, flow_options] end def normalize_magnetic_to((ctx, flow_options), *) # TODO: merge with Railway.merge_magnetic_to raise unless track_name = ctx[:track_name]# TODO: make track_name required kw. @@ -102,15 +117,15 @@ # Return {Path::Normalizer} sequence. def prepend_step_options(sequence) prepend_to_path( sequence, - "path.outputs" => method(:merge_path_outputs), - "path.connections" => method(:merge_path_connections), - "path.sequence_insert" => method(:normalize_sequence_insert), - "path.raise_on_duplicate_id" => method(:raise_on_duplicate_id), - "path.magnetic_to" => method(:normalize_magnetic_to), - "path.wirings" => Linear::Normalizer.method(:compile_wirings), + "path.outputs" => method(:merge_path_outputs), + "path.connections" => method(:merge_path_connections), + "path.sequence_insert" => method(:normalize_sequence_insert), + "path.normalize_duplications" => method(:normalize_duplications), + "path.magnetic_to" => method(:normalize_magnetic_to), + "path.wirings" => Linear::Normalizer.method(:compile_wirings), ) end # Returns an initial two-step sequence with {Start.default > End.success}. def initial_sequence(track_name:, end_task:, end_id:)