io-capture 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/io-capture.rb +88 -0
  2. metadata +62 -0
@@ -0,0 +1,88 @@
1
+ class Capture
2
+
3
+ class << self
4
+ attr_accessor :currently_capturing
5
+ end
6
+
7
+ Capture.currently_capturing ||= {}
8
+
9
+ def self.[] name
10
+ Capture.currently_capturing[name]
11
+ end
12
+
13
+ def self.[]= name, value
14
+ Capture.currently_capturing[name] = value
15
+ end
16
+
17
+ def self.do io = nil, &block
18
+ io = :stdout if io.nil?
19
+
20
+ if global = is_a_global_variable_and_IO?(io)
21
+ start io
22
+ block.call
23
+ stop io
24
+ else
25
+ raise "Not sure how to capture writes to io: #{ io.inspect }"
26
+ end
27
+ end
28
+
29
+ def self.is_a_global_variable_and_IO? io
30
+ io = '$' + io.to_s.sub(/^\$/,'')
31
+ if global_variables.include?(io) and eval(io).respond_to?(:write)
32
+ io
33
+ else
34
+ nil
35
+ end
36
+ end
37
+
38
+ def self.start io = nil
39
+ io = :stdout if io.nil?
40
+ if global = is_a_global_variable_and_IO?(io)
41
+
42
+ # backup original value of global
43
+ Capture[global] = eval(global).clone
44
+
45
+ # reset global to a StringIO for capturing
46
+ eval("#{global} = StringIO.open '', 'w'")
47
+
48
+ # simple return nil
49
+ nil
50
+
51
+ end
52
+ end
53
+
54
+ def self.stop io = nil
55
+ io = :stdout if io.nil?
56
+ if global = is_a_global_variable_and_IO?(io)
57
+
58
+ # get captured output
59
+ output = eval(global).string
60
+
61
+ # reset global to backed up original
62
+ eval("#{global} = Capture[#{ global.inspect }]")
63
+
64
+ # we're no longer capturing ... delete from hash
65
+ currently_capturing.delete global
66
+
67
+ # reutrn the output
68
+ return output
69
+
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ # Captures STDOUT or STDERR and returns the output.
76
+ #
77
+ # >> Capture { puts 'hello' }
78
+ # => 'hello'
79
+ #
80
+ # >> Capture(:stdout){ puts 'hello stdout' }
81
+ # => 'hello stdout'
82
+ #
83
+ # >> Capture(:stderr){ warn 'hello stderr' }
84
+ # => 'hello stderr'
85
+ #
86
+ def Capture io = nil, &block
87
+ Capture.do io, &block
88
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: io-capture
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - remi
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-11 00:00:00 -07:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: remi@remitaylor.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - lib/io-capture.rb
31
+ has_rdoc: true
32
+ homepage: http://github.com/remi/capture
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ segments:
52
+ - 0
53
+ version: "0"
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.3.6
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: easily capture writes to STDOUT, Files, or any IO
61
+ test_files: []
62
+