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
# File 'lib/cucumber/formatter/interceptor.rb', line 8

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



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

def method_missing(method, *args, &blk)
  @pipe.send(method, *args, &blk)
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



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

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



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

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

.wrap(pipe) ⇒ Object



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

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



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

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

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

Returns:

  • (Boolean)


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

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

#unwrap!Object



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

def unwrap!
  @wrapped = false
  @pipe
end

#write(str) ⇒ Object



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

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