Sha256: 11a5c4b3ddb15102b1d3211df281e0330b649ed0754c413af631eec3949c0fa4

Contents?: true

Size: 1.13 KB

Versions: 8

Compression:

Stored size: 1.13 KB

Contents

module Yaks
  class Pipeline
    include Concord.new(:steps)

    def call(input, env)
      steps.inject(input) {|memo, (_, step)| step.call(memo, env) }
    end

    def insert_hooks(hooks)
      new_steps = hooks.inject(steps) do |steps, (type, target_step, name, hook)|
        steps.flat_map do |step_name, callable|
          if step_name.equal? target_step
            case type
            when :before
              [[name, hook], [step_name, callable]]
            when :after
              [[step_name, callable], [name, hook]]
            when :around
              [[name, ->(x, env) { hook.call(x, env, &callable) }]]
            when :skip
              []
            end
          end || [[step_name, callable]]
        end
      end

      self.class.new(new_steps)
    end

    def transitive?
      steps.all? {|name, step| step.respond_to?(:transitive?) && step.transitive?}
    end

    def inverse
      unless transitive?
        raise RuntimeError, "Unable to get inverse pipeline, not all pipeline steps are transitive."
      end

      self.class.new(steps.map {|name, step| [name, step.inverse]}.reverse)
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
yaks-0.9.0 lib/yaks/pipeline.rb
yaks-0.8.3 lib/yaks/pipeline.rb
yaks-0.8.2 lib/yaks/pipeline.rb
yaks-0.8.1 lib/yaks/pipeline.rb
yaks-0.8.0 lib/yaks/pipeline.rb
yaks-0.8.0.beta2 lib/yaks/pipeline.rb
yaks-0.8.0.beta1 lib/yaks/pipeline.rb
yaks-0.8.0.alpha lib/yaks/pipeline.rb