Class: TeeIO

Inherits:
Object
  • Object
show all
Defined in:
lib/capistrano/cli/tee_io.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ TeeIO

Returns a new instance of TeeIO.


51
52
53
54
55
56
57
# File 'lib/capistrano/cli/tee_io.rb', line 51

def initialize(*args)
  @teed_io_name = args[0]
  @old_io = eval(args[0])
  @pipe_io = args[1]
  @auto_close_pipe_io = args[2]
  @tee_on = true
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object


86
87
88
# File 'lib/capistrano/cli/tee_io.rb', line 86

def method_missing(method_name, *args, &block)
  call_on_io(method_name, *args, &block)
end

Instance Attribute Details

#auto_close_pipe_ioObject

Returns the value of attribute auto_close_pipe_io


6
7
8
# File 'lib/capistrano/cli/tee_io.rb', line 6

def auto_close_pipe_io
  @auto_close_pipe_io
end

#old_ioObject

Returns the value of attribute old_io


3
4
5
# File 'lib/capistrano/cli/tee_io.rb', line 3

def old_io
  @old_io
end

#pipe_ioObject

Returns the value of attribute pipe_io


4
5
6
# File 'lib/capistrano/cli/tee_io.rb', line 4

def pipe_io
  @pipe_io
end

#tee_onObject

Returns the value of attribute tee_on


7
8
9
# File 'lib/capistrano/cli/tee_io.rb', line 7

def tee_on
  @tee_on
end

#teed_io_nameObject

Returns the value of attribute teed_io_name


5
6
7
# File 'lib/capistrano/cli/tee_io.rb', line 5

def teed_io_name
  @teed_io_name
end

Class Method Details

.output_allObject


44
45
46
47
# File 'lib/capistrano/cli/tee_io.rb', line 44

def output_all
  $stderr.output if $stderr.is_a?(TeeIO)
  $stdout.output if $stdout.is_a?(TeeIO)
end

.tee(teed_io_name, pipe_io = nil, &block) ⇒ Object


34
35
36
37
38
39
40
41
42
# File 'lib/capistrano/cli/tee_io.rb', line 34

def tee(teed_io_name, pipe_io = nil, &block)
  eval("#{teed_io_name} = self.new(teed_io_name, pipe_io || StringIO.new, pipe_io.nil?)")
  if block_given?
    yield(eval("#{teed_io_name}.pipe_io"))
    eval("#{teed_io_name}.output")
  else
    eval("#{teed_io_name}.pipe_io")        
  end
end

.tee_all(pipe_io = nil, &block) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/capistrano/cli/tee_io.rb', line 11

def tee_all(pipe_io = nil, &block)
  if block_given?
    tee("$stdout", pipe_io) do |io|
      tee("$stderr", io) do
        yield
      end
    end
  else
    io = tee("$stdout", pipe_io)
    tee("$stderr", io)
  end
end

.tee_all_offObject


29
30
31
32
# File 'lib/capistrano/cli/tee_io.rb', line 29

def tee_all_off
  $stderr.tee_on = false if $stderr.is_a?(TeeIO)
  $stdout.tee_on = false if $stdout.is_a?(TeeIO)
end

.tee_all_onObject


24
25
26
27
# File 'lib/capistrano/cli/tee_io.rb', line 24

def tee_all_on
  $stderr.tee_on = true if $stderr.is_a?(TeeIO)
  $stdout.tee_on = true if $stdout.is_a?(TeeIO)
end

Instance Method Details

#call_on_io(method_name, *args, &block) ⇒ Object


59
60
61
62
# File 'lib/capistrano/cli/tee_io.rb', line 59

def call_on_io(method_name, *args, &block)
  call_on_old_io(method_name, *args, &block)
  call_on_pipe_io(method_name, *args, &block) if @tee_on
end

#call_on_old_io(method_name, *args, &block) ⇒ Object


64
65
66
67
68
69
# File 'lib/capistrano/cli/tee_io.rb', line 64

def call_on_old_io(method_name, *args, &block)
  if @old_io.is_a?(IO) || @old_io.is_a?(StringIO)
    @old_io.send(method_name, *args, &block)
    @old_io.flush
  end
end

#call_on_pipe_io(method_name, *args, &block) ⇒ Object


71
72
73
74
75
76
# File 'lib/capistrano/cli/tee_io.rb', line 71

def call_on_pipe_io(method_name, *args, &block)
  if @pipe_io.is_a?(IO) || @pipe_io.is_a?(StringIO)
    @pipe_io.send(method_name, *args, &block)
    @pipe_io.flush
  end
end

#outputObject


90
91
92
93
94
95
96
97
98
99
# File 'lib/capistrano/cli/tee_io.rb', line 90

def output
  reset_io
  if @auto_close_pipe_io && @pipe_io.is_a?(StringIO)
    str = @pipe_io.string
    @pipe_io.close
    str
  else
    @pipe_io
  end
end

#puts(*args) ⇒ Object


78
79
80
# File 'lib/capistrano/cli/tee_io.rb', line 78

def puts(*args)
  call_on_io(:puts, *args)
end

#reset_ioObject


101
102
103
# File 'lib/capistrano/cli/tee_io.rb', line 101

def reset_io
  eval("#{teed_io_name} = @old_io")
end

#write(str) ⇒ Object


82
83
84
# File 'lib/capistrano/cli/tee_io.rb', line 82

def write(str)
  call_on_io(:write, str)
end