Sha256: e94b91dc9bb5037cc9422e07bf1b7e73d54a0c06badd51b1b3be1fa65219eba8

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

module Representable
  Pipeline.class_eval do # FIXME: this doesn't define Function in Pipeline.
    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
  end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
representable-2.4.0.rc4 lib/representable/insert.rb
representable-2.4.0.rc3 lib/representable/insert.rb