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.


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

def initialize(pipe)
  @pipe = pipe
  @buffer = StringIO.new
  @wrapped = true
  @lock = Mutex.new
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) || super
end

Instance Attribute Details

#pipeObject (readonly)

Returns the value of attribute pipe


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

def pipe
  @pipe
end

Class Method Details

.unwrap!(pipe) ⇒ Object


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

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

Raises:

  • (ArgumentError)

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

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

.wrap(pipe) ⇒ Object


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

def self.wrap(pipe)
  validate_pipe pipe

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

Instance Method Details

#buffer_stringObject


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

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

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

Returns:

  • (Boolean)

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

def respond_to_missing?(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