class Fray::AbortablePipe

Public Class Methods

new(abort_predicate, thens = [], c = nil) click to toggle source
# 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

Public Instance Methods

catch(func) click to toggle source
# File lib/fray/abortable_pipe.rb, line 50
def catch(func)
  AbortablePipe.new(@abort_predicate, @thens, func)
end
run(state = nil, thens = @thens) click to toggle source
# 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) click to toggle source
# 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