Class: Welder::Pipeline
- Inherits:
-
Object
- Object
- Welder::Pipeline
- Includes:
- Support::CallableHandler
- Defined in:
- lib/welder/pipeline.rb
Overview
A wrapper around a sequence of callables (i.e. anything that responds to a single-argument ‘call’ method, which includes procs, blocks and other pipelines)
Pipelines are immutable. There are mechanisms to create more complex pipelines out of existing ones, but they will always create a new pipeline
Instance Method Summary collapse
-
#-(other) ⇒ Object
Create a new pipeline that keeps all the steps in the current one, but adds a valve overseeing the process.
-
#call(input, valves = []) ⇒ *
Apply the sequence of functions to a particular input.
-
#initialize(*lambdas, valves: nil, &block) ⇒ Pipeline
constructor
Creates a new pipeline out of a sequence of callables.
-
#|(other) ⇒ Welder::Pipeline
Compose a pipeline with another one (or a callable).
Methods included from Support::CallableHandler
Constructor Details
#initialize(*lambdas, valves: nil, &block) ⇒ Pipeline
Creates a new pipeline out of a sequence of callables. If a block is given, it is executed as the last step of the pipeline. It also accepts valves, with act as witnesses of the steps the pipeline goes through
33 34 35 36 37 38 39 40 |
# File 'lib/welder/pipeline.rb', line 33 def initialize(*lambdas, valves: nil, &block) callable!(*lambdas, *valves) @pipes = [*lambdas] @pipes << block if block @valves = [*valves] end |
Instance Method Details
#-(other) ⇒ Object
Create a new pipeline that keeps all the steps in the current one, but adds a valve overseeing the process. The valve will get called at every stage, as a side effect, but it will never modify the final outcome of the pipeline
88 89 90 |
# File 'lib/welder/pipeline.rb', line 88 def -(other) self.class.new(self, valves: [*other]) end |
#call(input, valves = []) ⇒ *
Apply the sequence of functions to a particular input. Any valves present in the pipeline are called as a side effect
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/welder/pipeline.rb', line 51 def call(input, valves = []) valves = @valves.concat(valves) @pipes.reduce(input) do |a, e| if e.is_a?(Pipeline) e.call(a, valves) else e.call(a).tap do |output| valves.each { |valve| valve.call(a, e, output) } end end end end |
#|(other) ⇒ Welder::Pipeline
Compose a pipeline with another one (or a callable). This method does not modify existing pipelines. Instead, it creates a new pipeline composed of the previous two
77 78 79 |
# File 'lib/welder/pipeline.rb', line 77 def |(other) self.class.new(self, other) end |