tee 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/tee.rb +59 -51
  2. data/spec/tee_spec.rb +27 -0
  3. metadata +7 -7
data/lib/tee.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # A class like tee(1)
2
2
  class Tee
3
3
  # @return [String]
4
- VERSION = '0.0.2'
4
+ VERSION = '0.0.3'
5
5
 
6
6
  class << self
7
7
  # @macro new
@@ -83,8 +83,7 @@ class Tee
83
83
  # @param obj [Object]
84
84
  # @return [self]
85
85
  def <<(obj)
86
- each_ios_and_stdout { |io| io << obj }
87
- self
86
+ each_ios_and_stdout(obj, &:<<)
88
87
  end
89
88
 
90
89
  # Closes all ios except stdout
@@ -107,7 +106,25 @@ class Tee
107
106
  # @return [self]
108
107
  def flush
109
108
  each_ios_and_stdout(&:flush)
110
- self
109
+ end
110
+
111
+ # Delegates #print to ios
112
+ #
113
+ # @param obj [Object]
114
+ # @return [nil]
115
+ def print(*obj)
116
+ each_ios_and_stdout(*obj, &:print)
117
+ nil
118
+ end
119
+
120
+ # Delegates #printf to ios
121
+ #
122
+ # @param format [String]
123
+ # @param obj [Object]
124
+ # @return [nil]
125
+ def printf(format, *obj)
126
+ each_ios_and_stdout(format, *obj, &:printf)
127
+ nil
111
128
  end
112
129
 
113
130
  # Delegates #putc to ios
@@ -116,10 +133,27 @@ class Tee
116
133
  # @return [Fixnum]
117
134
  # @return [String]
118
135
  def putc(char)
119
- each_ios_and_stdout { |io| io.putc(char) }
136
+ each_ios_and_stdout(char, &:putc)
120
137
  char
121
138
  end
122
139
 
140
+ # Delegates #puts to ios
141
+ #
142
+ # @param obj [Object]
143
+ # @return [nil]
144
+ def puts(*obj)
145
+ each_ios_and_stdout(*obj, &:puts)
146
+ nil
147
+ end
148
+
149
+ # Delegates #syswrite to ios
150
+ #
151
+ # @param string [String]
152
+ # @return [Array<Integer>]
153
+ def syswrite(string)
154
+ each_ios_and_stdout(string).map(&:syswrite)
155
+ end
156
+
123
157
  # Returns self
124
158
  #
125
159
  # @return [self]
@@ -135,71 +169,45 @@ class Tee
135
169
  end
136
170
  alias isatty tty?
137
171
 
138
- # @method print(obj, ...)
139
- # Delegates #print to ios
140
- # @param obj [Object]
141
- # @return [nil]
142
-
143
- # @method printf(format[, obj, ...])
144
- # Delegates #printf to ios
145
- # @param format [String]
146
- # @param obj [Object]
147
- # @return [nil]
148
-
149
- # @method puts(obj, ...)
150
- # Delegates #puts to ios
151
- # @param obj [Object]
152
- # @return [nil]
153
- %w( print printf puts ).each do |method|
154
- class_eval(<<-EOS, __FILE__, __LINE__ + 1)
155
- def #{method}(*args)
156
- each_ios_and_stdout { |io| io.#{method}(*args) }
157
- nil
158
- end
159
- EOS
160
- end
161
-
162
- # @method syswrite(string)
163
- # Delegates #syswrite to ios
164
- # @param string [String]
165
- # @return [Array<Integer>]
166
-
167
- # @method write(string)
168
172
  # Delegates #write to ios
173
+ #
169
174
  # @param string [String]
170
175
  # @return [Array<Integer>]
176
+ def write(string)
177
+ each_ios_and_stdout(string).map(&:write)
178
+ end
171
179
 
172
- # @method write_nonblock(string)
173
180
  # Delegates #write_nonblock to ios
181
+ #
174
182
  # @param string [String]
175
183
  # @return [Array<Integer>]
176
- %w( syswrite write write_nonblock ).each do |method|
177
- class_eval(<<-EOS, __FILE__, __LINE__ + 1)
178
- def #{method}(string)
179
- each_ios_and_stdout.map { |io| io.#{method}(string) }
180
- end
181
- EOS
184
+ def write_nonblock(string)
185
+ each_ios_and_stdout(string).map(&:write_nonblock)
182
186
  end
183
187
 
184
188
  private
185
189
 
186
- def each_ios(&block)
187
- return to_enum(:each_ios) unless block_given?
190
+ # @return [self]
191
+ def each_ios(*args, &block)
192
+ return to_enum(:each_ios, *args) unless block_given?
188
193
  @ios.each do |io,|
189
- yield io
194
+ yield io, *args
190
195
  end
191
196
  self
192
197
  end
193
198
 
194
- def each_ios_and_stdout(&block)
195
- return to_enum(:each_ios_and_stdout) unless block_given?
196
- each_ios(&block)
197
- yield @stdout if @stdout
198
- self
199
+ # @return [self]
200
+ def each_ios_and_stdout(*args, &block)
201
+ return to_enum(:each_ios_and_stdout, *args) unless block_given?
202
+ yield @stdout, *args if @stdout
203
+ each_ios(*args, &block)
199
204
  end
200
205
 
206
+ # @return [nil]
201
207
  def close_ios_opened_by_self(ios = @ios)
202
- ios.each { |io, opened| io.close if opened && !io.closed? }
208
+ ios.each do |io, opened|
209
+ io.close if opened && !io.closed?
210
+ end
203
211
  nil
204
212
  end
205
213
  end
@@ -267,6 +267,30 @@ describe Tee do
267
267
  end
268
268
  end
269
269
 
270
+ describe '#closed?' do
271
+ context 'when tee is opened without argument' do
272
+ it 'returns true' do
273
+ tee.closed?.should be_true
274
+ end
275
+ end
276
+
277
+ context 'when tee is opened with an ios' do
278
+ before { @tee = Tee.open(StringIO.new) }
279
+
280
+ it 'returns false' do
281
+ @tee.closed?.should be_false
282
+ end
283
+
284
+ context 'after closing' do
285
+ before { @tee.close }
286
+
287
+ it 'returns true' do
288
+ @tee.closed?.should be_true
289
+ end
290
+ end
291
+ end
292
+ end
293
+
270
294
  describe '#flush' do
271
295
  it 'returns self' do
272
296
  tee.flush.should be tee
@@ -322,6 +346,9 @@ describe Tee do
322
346
  end
323
347
 
324
348
  %w( syswrite write write_nonblock ).each do |method|
349
+ # for JRuby 1.6
350
+ next if method == 'write_nonblock' && !StringIO.method_defined?(method)
351
+
325
352
  describe "##{method}" do
326
353
  it 'returns Array of the number of bytes written' do
327
354
  string = 'foo'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tee
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-06 00:00:00.000000000 Z
12
+ date: 2012-08-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70332175132780 !ruby/object:Gem::Requirement
16
+ requirement: &70107985808900 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.9.2.2
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70332175132780
24
+ version_requirements: *70107985808900
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70332175131820 !ruby/object:Gem::Requirement
27
+ requirement: &70107985774440 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 2.11.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70332175131820
35
+ version_requirements: *70107985774440
36
36
  description: A class like tee(1).
37
37
  email: m.ishihara@gmail.com
38
38
  executables: []
@@ -70,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
70
70
  version: '0'
71
71
  segments:
72
72
  - 0
73
- hash: 4540246555897654950
73
+ hash: 757023539280985321
74
74
  requirements: []
75
75
  rubyforge_project:
76
76
  rubygems_version: 1.8.11