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.
-
.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.
-
#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.
42 43 44 45 |
# File 'lib/piperator/pipeline.rb', line 42 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.
38 39 40 |
# File 'lib/piperator/pipeline.rb', line 38 def self.call(enumerable = []) enumerable end |
.pipe(callable) ⇒ Pipeline
Build a new pipeline from a callable or an enumerable object
13 14 15 |
# File 'lib/piperator/pipeline.rb', line 13 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]
29 30 31 |
# File 'lib/piperator/pipeline.rb', line 29 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.
51 52 53 |
# File 'lib/piperator/pipeline.rb', line 51 def call(enumerable = []) @pipes.reduce(enumerable) { |pipe, memo| memo.call(pipe) } end |
#pipe(other) ⇒ Pipeline
Add a new part to the pipeline
66 67 68 |
# File 'lib/piperator/pipeline.rb', line 66 def pipe(other) Pipeline.new(@pipes + [other]) end |
#to_a(enumerable = []) ⇒ Array
Compute the pipeline and strictly evaluate the result
58 59 60 |
# File 'lib/piperator/pipeline.rb', line 58 def to_a(enumerable = []) call(enumerable).to_a end |