Class: Cucumber::Formatter::Interceptor::Pipe

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber/formatter/interceptor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pipe) ⇒ Pipe

Returns a new instance of Pipe.



9
10
11
12
13
# File 'lib/cucumber/formatter/interceptor.rb', line 9

def initialize(pipe)
  @pipe = pipe
  @buffer = []
  @wrapped = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object



33
34
35
# File 'lib/cucumber/formatter/interceptor.rb', line 33

def method_missing(method, *args, &blk)
  @pipe.send(method, *args, &blk)
end

Instance Attribute Details

#pipeObject (readonly)

Returns the value of attribute pipe.



8
9
10
# File 'lib/cucumber/formatter/interceptor.rb', line 8

def pipe
  @pipe
end

Class Method Details

.unwrap!(pipe) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cucumber/formatter/interceptor.rb', line 47

def self.unwrap!(pipe)
  validate_pipe pipe
  wrapped = nil
  case pipe
  when :stdout
    wrapped = $stdout
    $stdout = wrapped.unwrap! if $stdout.respond_to?(:unwrap!)
  when :stderr
    wrapped = $stderr
    $stderr = wrapped.unwrap! if $stderr.respond_to?(:unwrap!)
  end
  wrapped
end

.validate_pipe(pipe) ⇒ Object



41
42
43
44
45
# File 'lib/cucumber/formatter/interceptor.rb', line 41

def self.validate_pipe(pipe)
  unless [:stdout, :stderr].include? pipe
    raise ArgumentError, '#wrap only accepts :stderr or :stdout'
  end
end

.wrap(pipe) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cucumber/formatter/interceptor.rb', line 61

def self.wrap(pipe)
  validate_pipe pipe

  case pipe
  when :stderr
    $stderr = self.new($stderr)
    return $stderr
  when :stdout
    $stdout = self.new($stdout)
    return $stdout
  end
end

Instance Method Details

#bufferObject



22
23
24
25
26
# File 'lib/cucumber/formatter/interceptor.rb', line 22

def buffer
  lock.synchronize do
    return @buffer.dup
  end
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/cucumber/formatter/interceptor.rb', line 37

def respond_to?(method, include_private=false)
  super || @pipe.respond_to?(method, include_private)
end

#unwrap!Object



28
29
30
31
# File 'lib/cucumber/formatter/interceptor.rb', line 28

def unwrap!
  @wrapped = false
  @pipe
end

#write(str) ⇒ Object



15
16
17
18
19
20
# File 'lib/cucumber/formatter/interceptor.rb', line 15

def write(str)
  lock.synchronize do
    @buffer << str if @wrapped
    return @pipe.write(str)
  end
end