Class: Fife::Pipe

Inherits:
Object
  • Object
show all
Defined in:
lib/fife/pipe.rb

Defined Under Namespace

Classes: Aborted

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ios, abort_on_fail) ⇒ Pipe

Returns a new instance of Pipe.



13
14
15
16
# File 'lib/fife/pipe.rb', line 13

def initialize(ios, abort_on_fail)
  @ios = ios
  @abort_on_fail = abort_on_fail
end

Instance Attribute Details

#abort_on_failObject (readonly)

Returns the value of attribute abort_on_fail.



11
12
13
# File 'lib/fife/pipe.rb', line 11

def abort_on_fail
  @abort_on_fail
end

#iosObject (readonly)

Returns the value of attribute ios.



11
12
13
# File 'lib/fife/pipe.rb', line 11

def ios
  @ios
end

Instance Method Details

#pipe(op, *args) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/fife/pipe.rb', line 18

def pipe(op, *args)
  op = Fife::Operations[op].new(*args) unless op.respond_to? :call
  output = []

  ios.map { |io|
    Thread.new do
      output << catch(:halt) do
        begin
          op.call(io.tap(&:rewind))
        rescue => e
          throw :halt, e
        end
      end
    end
  }.each(&:join)

  errors, output = output.tap(&:flatten!).partition{|o| o.is_a?(StandardError)}

  should_abort = abort_on_fail && (errors.any? || output.any?(&:nil?))
  if should_abort
    ios.each { |io| io.close unless io.closed? }
    raise Aborted, errors
  end

  self.class.new(output.tap(&:compact!), abort_on_fail)
end