Module: Trailblazer::Activity::Adds::Insert
- Defined in:
- lib/trailblazer/activity/adds.rb
Overview
Functions to alter the Sequence/Pipeline by inserting, replacing, or deleting a row.
they don’t mutate the data structure but rebuild it, has to respond to to_a
These methods are invoked via apply_adds and should never be called directly.
Class Method Summary collapse
-
.Append(pipeline, new_row, insert_id = nil) ⇒ Object
Append new_row after insert_id.
-
.apply_on_ary(pipeline, insert_id, raise_index_error: true, &block) ⇒ Object
Converts the pipeline structure to an array, automatically finds the index for insert_id, and calls the user block with the computed values.
- .build(sequence, rows) ⇒ Object
- .build_from_ary(pipeline, insert_id, &block) ⇒ Object
- .Delete(pipeline, _, insert_id) ⇒ Object
- .find_index(ary, insert_id) ⇒ Object
-
.Prepend(pipeline, new_row, insert_id = nil) ⇒ Object
Insert new_row before insert_id.
-
.range_before_index(ary, index) ⇒ Object
Always returns a valid, concat-able array for all indices before the index.
- .Replace(pipeline, new_row, insert_id) ⇒ Object
Class Method Details
.Append(pipeline, new_row, insert_id = nil) ⇒ Object
Append new_row after insert_id.
70 71 72 73 74 75 76 |
# File 'lib/trailblazer/activity/adds.rb', line 70 def Append(pipeline, new_row, insert_id = nil) build_from_ary(pipeline, insert_id) do |ary, index| index = ary.size if index.nil? # append to end of pipeline. range_before_index(ary, index + 1) + [new_row] + Array(ary[index + 1..-1]) end end |
.apply_on_ary(pipeline, insert_id, raise_index_error: true, &block) ⇒ Object
Converts the pipeline structure to an array, automatically finds the index for insert_id, and calls the user block with the computed values.
Single-entry point, could be named #call.
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/trailblazer/activity/adds.rb', line 115 def apply_on_ary(pipeline, insert_id, raise_index_error: true, &block) ary = pipeline.to_a if insert_id.nil? index = nil else index = find_index(ary, insert_id) # DISCUSS: this only makes sense if there are more than {Append} using this. raise IndexError.new(pipeline, insert_id) if index.nil? && raise_index_error end _new_ary = yield(ary, index) # call the block. end |
.build(sequence, rows) ⇒ Object
100 101 102 |
# File 'lib/trailblazer/activity/adds.rb', line 100 def build(sequence, rows) sequence.class.new(rows) end |
.build_from_ary(pipeline, insert_id, &block) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/trailblazer/activity/adds.rb', line 128 def build_from_ary(pipeline, insert_id, &block) new_ary = apply_on_ary(pipeline, insert_id, &block) # Wrap the sequence/pipeline array into a concrete Sequence/Pipeline. build(pipeline, new_ary) end |
.Delete(pipeline, _, insert_id) ⇒ Object
93 94 95 96 97 |
# File 'lib/trailblazer/activity/adds.rb', line 93 def Delete(pipeline, _, insert_id) build_from_ary(pipeline, insert_id) do |ary, index| range_before_index(ary, index) + ary[index + 1..-1] end end |
.find_index(ary, insert_id) ⇒ Object
105 106 107 |
# File 'lib/trailblazer/activity/adds.rb', line 105 def find_index(ary, insert_id) ary.find_index { |row| row.id == insert_id } end |
.Prepend(pipeline, new_row, insert_id = nil) ⇒ Object
Insert new_row before insert_id.
79 80 81 82 83 84 85 |
# File 'lib/trailblazer/activity/adds.rb', line 79 def Prepend(pipeline, new_row, insert_id = nil) build_from_ary(pipeline, insert_id) do |ary, index| index = 0 if index.nil? # Prepend to beginning of pipeline. range_before_index(ary, index) + [new_row] + ary[index..-1] end end |
.range_before_index(ary, index) ⇒ Object
Always returns a valid, concat-able array for all indices before the index.
138 139 140 141 |
# File 'lib/trailblazer/activity/adds.rb', line 138 def range_before_index(ary, index) return [] if index == 0 ary[0..index - 1] end |
.Replace(pipeline, new_row, insert_id) ⇒ Object
87 88 89 90 91 |
# File 'lib/trailblazer/activity/adds.rb', line 87 def Replace(pipeline, new_row, insert_id) build_from_ary(pipeline, insert_id) do |ary, index| range_before_index(ary, index) + [new_row] + ary[index + 1..-1] end end |