Sha256: b578dfdeae23303038ff8d014ae9e3e4841cfc6e7572a6a2a996ebb347981061

Contents?: true

Size: 1 KB

Versions: 8

Compression:

Stored size: 1 KB

Contents

module Representable
  class Pipeline < Array # i hate that.
    module Function
      class Insert
        def call(arr, func, options)
          arr = arr.dup
          delete!(arr, func) if options[:delete]
          replace!(arr, options[:replace], func) if options[:replace]
          arr
        end

      private
        def replace!(arr, old_func, new_func)
          arr.each_with_index { |func, index|
            if func.is_a?(Collect)
              arr[index] = Collect[*Pipeline::Insert.(func, new_func, replace: old_func)]
            end

            arr[index] = new_func if func==old_func
          }
        end

        def delete!(arr, removed_func)
          arr.delete(removed_func)

          # TODO: make nice.
          arr.each_with_index { |func, index|
            if func.is_a?(Collect)
              arr[index] = Collect[*Pipeline::Insert.(func, removed_func, delete: true)]
            end
          }
        end
      end
    end

    Insert = Pipeline::Function::Insert.new
  end # Pipeline
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
representable-3.0.4 lib/representable/insert.rb
representable-3.0.3 lib/representable/insert.rb
representable-3.0.2 lib/representable/insert.rb
representable-3.0.1 lib/representable/insert.rb
representable-3.0.0 lib/representable/insert.rb
representable-2.4.1 lib/representable/insert.rb
representable-2.4.0 lib/representable/insert.rb
representable-2.4.0.rc5 lib/representable/insert.rb