Sha256: c147fc8f75b26ec5fe18fca70f09b76b7f261e7ad6e29683e89c99a7ea3e1d10

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 KB

Contents

module Representable
  # Allows to implement a pipeline of filters where a value gets passed in and the result gets
  # passed to the next callable object.
  class Pipeline < Array
    Stop = Class.new

    # options is mutable.
    def call(input, options)
      inject(input) do |memo, block|
        res = evaluate(block, memo, options)
        return(Stop) if Stop == res

        res
      end
    end

  private
    def evaluate(block, input, options)
      block.call(input, options)
    end


    module Macros # TODO: explicit test.
      # Macro to quickly modify an array of functions via Pipeline::Insert and return a
      # Pipeline instance.
      def insert(functions, new_function, options)
        Pipeline.new(Pipeline::Insert.(functions, new_function, options))
      end
    end
    extend Macros
  end # Pipeline


  # Collect applies a pipeline to each element of input.
  class Collect < Pipeline
    # when stop, the element is skipped. (should that be Skip then?)
    def call(input, options)
      arr = []
      input.each_with_index do |item_fragment, i|
        result = super(item_fragment, options.merge(index: i)) # DISCUSS: NO :fragment set.
        Pipeline::Stop == result ? next : arr << result
      end
      arr
    end

    class Hash < Pipeline
      def call(input, options)
        {}.tap do |hsh|
          input.each { |key, item_fragment|
            hsh[key] = super(item_fragment, options) }# DISCUSS: NO :fragment set.
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/representable-3.2.0/lib/representable/pipeline.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/representable-3.2.0/lib/representable/pipeline.rb
representable-3.2.0 lib/representable/pipeline.rb
representable-3.1.1 lib/representable/pipeline.rb
representable-3.1.0 lib/representable/pipeline.rb