Class: PatternRuby::Pipeline

Inherits:
Object
  • Object
show all
Defined in:
lib/pattern_ruby/pipeline.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Pipeline

Returns a new instance of Pipeline.



3
4
5
6
7
# File 'lib/pattern_ruby/pipeline.rb', line 3

def initialize(&block)
  @steps = []
  @error_handler = nil
  instance_eval(&block) if block
end

Instance Method Details

#on_error(&block) ⇒ Object



13
14
15
# File 'lib/pattern_ruby/pipeline.rb', line 13

def on_error(&block)
  @error_handler = block
end

#process(input) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/pattern_ruby/pipeline.rb', line 17

def process(input)
  result = input
  @steps.each do |s|
    result = s[:handler].call(result)
  rescue => e
    if @error_handler
      result = @error_handler.call(e, s[:name], result)
      break if result.nil?
    else
      raise
    end
  end
  result
end

#step(name, &block) ⇒ Object



9
10
11
# File 'lib/pattern_ruby/pipeline.rb', line 9

def step(name, &block)
  @steps << { name: name, handler: block }
end

#stepsObject



32
33
34
# File 'lib/pattern_ruby/pipeline.rb', line 32

def steps
  @steps.map { |s| s[:name] }
end