Module: Piperator

Defined in:
lib/piperator.rb,
lib/piperator/io.rb,
lib/piperator/builder.rb,
lib/piperator/version.rb,
lib/piperator/pipeline.rb

Overview

Top-level shortcuts

Defined Under Namespace

Classes: Builder, IO, Pipeline

Constant Summary collapse

VERSION =

Piperator version

'1.1.0'.freeze

Class Method Summary collapse

Class Method Details

.build(&block) ⇒ Pipeline

Build a new pipeline using DSL. This enables easy control of the pipeline stucture.

Piperator.build do
  wrap [1, 2, 3]
  pipe(-> (enumerable) { enumerable.map { |i| i + 1 } })
end
# => Pipeline that returns [2, 3, 4] called

# Alternatively, the Builder is also given as argument to the block
Piperator.build do |p|
  p.wrap [1, 2, 3]
  p.pipe(-> (enumerable) { enumerable.map { |i| i + 1 } })
end
# This is similar, but allows access to instance variables.

Returns:

  • (Pipeline)

    Pipeline containing defined steps



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/piperator.rb', line 25

def self.build(&block)
  return Pipeline.new unless block_given?

  Builder.new(block&.binding).tap do |builder|
    if block.arity.positive?
      yield builder
    else
      builder.instance_eval(&block)
    end
  end.to_pipeline
end

.pipe(enumerable) ⇒ Pipeline

Build a new pipeline from a callable or an enumerable object

Parameters:

  • enumerable

    An object responding to call(enumerable)

Returns:

  • (Pipeline)

    A pipeline containing only the callable

See Also:



42
43
44
# File 'lib/piperator.rb', line 42

def self.pipe(enumerable)
  Pipeline.pipe(enumerable)
end

.wrap(value) ⇒ Pipeline

Build a new pipeline from a from a non-callable, i.e. string, array, etc.

Parameters:

  • value

    A raw value which will be passed through the pipeline

Returns:

  • (Pipeline)

    A pipeline containing only the callable

See Also:



51
52
53
# File 'lib/piperator.rb', line 51

def self.wrap(value)
  Pipeline.wrap(value)
end