Class: Fray::AbortablePipe

Inherits:
Object
  • Object
show all
Defined in:
lib/fray/abortable_pipe.rb

Direct Known Subclasses

ResponsePipe

Instance Method Summary collapse

Constructor Details

#initialize(abort_predicate, thens = [], c = nil) ⇒ AbortablePipe

Returns a new instance of AbortablePipe.



33
34
35
36
37
# File 'lib/fray/abortable_pipe.rb', line 33

def initialize(abort_predicate, thens = [], c = nil)
  @abort_predicate = abort_predicate
  @thens = thens
  @catch = c || ->(x) { x }
end

Instance Method Details

#catch(func) ⇒ Object



50
51
52
# File 'lib/fray/abortable_pipe.rb', line 50

def catch(func)
  AbortablePipe.new(@abort_predicate, @thens, func)
end

#run(state = nil, thens = @thens) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fray/abortable_pipe.rb', line 55

def run(state = nil, thens = @thens)
  if thens.empty?
    state
  else
    first, rest = thens[0], thens[1..-1]
    next_state = first[:function].call(state, *first[:args])

    @abort_predicate.call(next_state) ?
      @catch.call(next_state) :
      run(next_state, rest)
  end
end

#then(func, *args) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/fray/abortable_pipe.rb', line 40

def then(func, *args)
  new_then = {
    function: func,
    args: args
  }

  AbortablePipe.new(@abort_predicate, @thens + [new_then], @catch)
end