Class: Philiprehberger::Pipe

Inherits:
Object
  • Object
show all
Defined in:
lib/philiprehberger/pipe.rb,
lib/philiprehberger/pipe/step.rb,
lib/philiprehberger/pipe/error.rb,
lib/philiprehberger/pipe/version.rb,
lib/philiprehberger/pipe/pipeline.rb

Defined Under Namespace

Classes: Error, Halted, PipeError, Pipeline, Step

Constant Summary collapse

VERSION =
'0.5.0'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_value = nil, pipeline: nil) ⇒ Pipe

Returns a new instance of Pipe.



20
21
22
23
24
# File 'lib/philiprehberger/pipe.rb', line 20

def initialize(initial_value = nil, pipeline: nil)
  @initial_value = initial_value
  @pipeline = pipeline || Pipeline.new
  @error_handler = nil
end

Class Method Details

.halt!(value) ⇒ Object

Short-circuit the currently executing pipeline, returning ‘value` as the final result of `#call` / `#value`. Safe to call from inside any step; does NOT trigger `on_error` handlers.

Parameters:

  • value (Object)

    the value to return as the pipeline result

Raises:

  • (Halted)

    always (caught internally by the Pipeline)



16
17
18
# File 'lib/philiprehberger/pipe.rb', line 16

def self.halt!(value)
  raise Halted, value
end

Instance Method Details

#call(value) ⇒ Object



57
58
59
# File 'lib/philiprehberger/pipe.rb', line 57

def call(value)
  @pipeline.execute(value, error_handler: @error_handler)
end

#compose(other) ⇒ Object Also known as: >>



48
49
50
51
52
53
# File 'lib/philiprehberger/pipe.rb', line 48

def compose(other)
  composed_pipeline = Pipeline.new
  @pipeline.steps.each { |s| composed_pipeline.add_step(s) }
  other.send(:pipeline).steps.each { |s| composed_pipeline.add_step(s) }
  self.class.new(@initial_value, pipeline: composed_pipeline)
end

#on_error(&block) ⇒ Object



43
44
45
46
# File 'lib/philiprehberger/pipe.rb', line 43

def on_error(&block)
  @error_handler = block
  self
end

#step(name = nil, guard_if: nil, guard_unless: nil, &block) ⇒ Object



26
27
28
29
30
31
# File 'lib/philiprehberger/pipe.rb', line 26

def step(name = nil, guard_if: nil, guard_unless: nil, &block)
  @pipeline.add_step(
    Step.new(callable: block, name: name, guard_if: guard_if, guard_unless: guard_unless)
  )
  self
end

#tap_value(name = nil, &block) ⇒ Object



38
39
40
41
# File 'lib/philiprehberger/pipe.rb', line 38

def tap_value(name = nil, &block)
  @pipeline.add_step(Step.new(callable: block, name: name, type: :tee))
  self
end

#tee(&block) ⇒ Object



33
34
35
36
# File 'lib/philiprehberger/pipe.rb', line 33

def tee(&block)
  @pipeline.add_step(Step.new(callable: block, type: :tee))
  self
end

#to_procObject



61
62
63
# File 'lib/philiprehberger/pipe.rb', line 61

def to_proc
  method(:call).to_proc
end

#valueObject



65
66
67
# File 'lib/philiprehberger/pipe.rb', line 65

def value
  @pipeline.execute(@initial_value, error_handler: @error_handler)
end