Class: Piperator::Pipeline
- Inherits:
-
Object
- Object
- Piperator::Pipeline
- Defined in:
- lib/piperator/pipeline.rb
Overview
Pipeline is responsible of composition of a lazy enumerable from callables. It contains a collection of pipes that respond to #call and return a enumerable.
For streaming purposes, it usually is desirable to have pipes that takes a lazy Enumerator as an argument a return a (modified) lazy Enumerator.
Class Method Summary collapse
-
.call(enumerable = []) ⇒ Enumerable
Returns enumerable given as an argument without modifications.
-
.lazy(&block) ⇒ Pipeline
Build a new pipeline from a lazily evaluated callable or an enumerable object.
-
.pipe(callable) ⇒ Pipeline
Build a new pipeline from a callable or an enumerable object.
-
.wrap(value) ⇒ Pipeline
Build a new pipeline from a from a non-callable, i.e.
Instance Method Summary collapse
-
#call(enumerable = []) ⇒ Enumerable
Compute the pipeline and return a lazy enumerable with all the pipes.
-
#initialize(pipes = []) ⇒ Pipeline
constructor
A new instance of Pipeline.
-
#lazy(&block) ⇒ Pipeline
Add a new lazily evaluated part to the pipeline.
-
#pipe(other) ⇒ Pipeline
Add a new part to the pipeline.
-
#to_a(enumerable = []) ⇒ Array
Compute the pipeline and strictly evaluate the result.
-
#wrap(other) ⇒ Pipeline
Add a new value to the pipeline.
Constructor Details
#initialize(pipes = []) ⇒ Pipeline
Returns a new instance of Pipeline.
56 57 58 59 |
# File 'lib/piperator/pipeline.rb', line 56 def initialize(pipes = []) @pipes = pipes freeze end |
Class Method Details
.call(enumerable = []) ⇒ Enumerable
Returns enumerable given as an argument without modifications. Usable when Pipeline is used as an identity transformation.
52 53 54 |
# File 'lib/piperator/pipeline.rb', line 52 def self.call(enumerable = []) enumerable end |
.lazy(&block) ⇒ Pipeline
Build a new pipeline from a lazily evaluated callable or an enumerable object.
callable.
15 16 17 18 19 20 21 |
# File 'lib/piperator/pipeline.rb', line 15 def self.lazy(&block) callable = nil Pipeline.new([lambda do |e| callable ||= block.call callable.call(e) end]) end |
.pipe(callable) ⇒ Pipeline
Build a new pipeline from a callable or an enumerable object
27 28 29 |
# File 'lib/piperator/pipeline.rb', line 27 def self.pipe(callable) Pipeline.new([callable]) end |
.wrap(value) ⇒ Pipeline
Build a new pipeline from a from a non-callable, i.e. string, array, etc. This method will wrap the value in a proc, thus making it callable.
Piperator::Pipeline.wrap([1, 2, 3]).pipe(add_one)
# => [2, 3, 4]
# Wrap is syntactic sugar for wrapping a value in a proc
Piperator::Pipeline.pipe(->(_) { [1, 2, 3] }).pipe(add_one)
# => [2, 3, 4]
43 44 45 |
# File 'lib/piperator/pipeline.rb', line 43 def self.wrap(value) Pipeline.new([->(_) { value }]) end |
Instance Method Details
#call(enumerable = []) ⇒ Enumerable
Compute the pipeline and return a lazy enumerable with all the pipes.
65 66 67 |
# File 'lib/piperator/pipeline.rb', line 65 def call(enumerable = []) @pipes.reduce(enumerable) { |pipe, memo| memo.call(pipe) } end |
#lazy(&block) ⇒ Pipeline
Add a new lazily evaluated part to the pipeline.
pipeline.
81 82 83 84 85 86 87 |
# File 'lib/piperator/pipeline.rb', line 81 def lazy(&block) callable = nil Pipeline.new(@pipes + [lambda do |e| callable ||= block.call callable.call(e) end]) end |
#pipe(other) ⇒ Pipeline
Add a new part to the pipeline
93 94 95 |
# File 'lib/piperator/pipeline.rb', line 93 def pipe(other) Pipeline.new(@pipes + [other]) end |
#to_a(enumerable = []) ⇒ Array
Compute the pipeline and strictly evaluate the result
72 73 74 |
# File 'lib/piperator/pipeline.rb', line 72 def to_a(enumerable = []) call(enumerable).to_a end |