Class: Capture
- Inherits:
-
Object
- Object
- Capture
- Defined in:
- lib/io-capture.rb
Class Attribute Summary collapse
-
.currently_capturing ⇒ Object
Returns the value of attribute currently_capturing.
Class Method Summary collapse
- .[](name) ⇒ Object
- .[]=(name, value) ⇒ Object
- .do(io = nil, &block) ⇒ Object
- .is_a_global_variable_and_IO?(io) ⇒ Boolean
- .start(io = nil) ⇒ Object
- .stop(io = nil) ⇒ Object
Class Attribute Details
.currently_capturing ⇒ Object
Returns the value of attribute currently_capturing.
4 5 6 |
# File 'lib/io-capture.rb', line 4 def currently_capturing @currently_capturing end |
Class Method Details
.[](name) ⇒ Object
9 10 11 |
# File 'lib/io-capture.rb', line 9 def self.[] name Capture.currently_capturing[name] end |
.[]=(name, value) ⇒ Object
13 14 15 |
# File 'lib/io-capture.rb', line 13 def self.[]= name, value Capture.currently_capturing[name] = value end |
.do(io = nil, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/io-capture.rb', line 17 def self.do io = nil, &block io = :stdout if io.nil? if global = is_a_global_variable_and_IO?(io) start io block.call stop io else raise "Not sure how to capture writes to io: #{ io.inspect }" end end |
.is_a_global_variable_and_IO?(io) ⇒ Boolean
29 30 31 32 33 34 35 36 |
# File 'lib/io-capture.rb', line 29 def self.is_a_global_variable_and_IO? io io = '$' + io.to_s.sub(/^\$/,'') if global_variables.include?(io) and eval(io).respond_to?(:write) io else nil end end |
.start(io = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/io-capture.rb', line 38 def self.start io = nil io = :stdout if io.nil? if global = is_a_global_variable_and_IO?(io) # backup original value of global Capture[global] = eval(global).clone # reset global to a StringIO for capturing eval("#{global} = StringIO.open '', 'w'") # simple return nil nil end end |
.stop(io = nil) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/io-capture.rb', line 54 def self.stop io = nil io = :stdout if io.nil? if global = is_a_global_variable_and_IO?(io) # get captured output output = eval(global).string # reset global to backed up original eval("#{global} = Capture[#{ global.inspect }]") # we're no longer capturing ... delete from hash currently_capturing.delete global # reutrn the output return output end end |