progress 2.2.0 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  /pkg/
2
+ /*.gem
2
3
 
3
4
  /doc/
4
5
  /rdoc/
data/lib/progress.rb CHANGED
@@ -76,12 +76,7 @@ class Progress
76
76
  @started_at = Time.now
77
77
  @eta = nil
78
78
  @semaphore = Mutex.new
79
- @beeper = Thread.new do
80
- loop do
81
- sleep 1
82
- print_message
83
- end
84
- end
79
+ start_beeper
85
80
  end
86
81
  levels << new(title, total)
87
82
  print_message true
@@ -129,7 +124,7 @@ class Progress
129
124
  end
130
125
  levels.pop
131
126
  if levels.empty?
132
- @beeper.kill
127
+ stop_beeper
133
128
  io.puts
134
129
  end
135
130
  end
@@ -217,8 +212,31 @@ class Progress
217
212
  end
218
213
  end
219
214
 
215
+ def start_beeper
216
+ @beeper = Thread.new do
217
+ loop do
218
+ sleep 10
219
+ print_message unless Thread.current[:skip]
220
+ end
221
+ end
222
+ end
223
+
224
+ def stop_beeper
225
+ @beeper.kill
226
+ @beeper = nil
227
+ end
228
+
229
+ def restart_beeper
230
+ if @beeper
231
+ @beeper[:skip] = true
232
+ @beeper.run
233
+ @beeper[:skip] = false
234
+ end
235
+ end
236
+
220
237
  def print_message(force = false)
221
238
  lock force do
239
+ restart_beeper
222
240
  if force || time_to_print?
223
241
  inner = 0
224
242
  parts, parts_cl = [], []
@@ -20,7 +20,7 @@ module Enumerable
20
20
  # (0...100).with_progress('Numbers').all? do |numbers|
21
21
  # # code
22
22
  # end
23
- def with_progress(title = nil, &block)
24
- Progress::WithProgress.new(self, title, &block)
23
+ def with_progress(title = nil, length = nil, &block)
24
+ Progress::WithProgress.new(self, title, length, &block)
25
25
  end
26
26
  end
@@ -15,31 +15,29 @@ class Progress
15
15
 
16
16
  # each object with progress
17
17
  def each
18
+ enumerable, length = case
19
+ when @length
20
+ [@enumerable, @length]
21
+ when !@enumerable.respond_to?(:length) || @enumerable.is_a?(String) || (defined?(StringIO) && @enumerable.is_a?(StringIO)) || (defined?(TempFile) && @enumerable.is_a?(TempFile))
22
+ elements = @enumerable.each.to_a
23
+ [elements, elements.length]
24
+ else
25
+ [@enumerable, @enumerable.length]
26
+ end
27
+
18
28
  Progress.start(@title, length) do
19
- @enumerable.each do |object|
29
+ enumerable.each do |object|
20
30
  Progress.step do
21
31
  yield object
22
32
  end
23
33
  end
24
- end
25
- end
26
-
27
- # determine number of objects
28
- def length
29
- @length ||= if @enumerable.respond_to?(:length) && !@enumerable.is_a?(String)
30
- @enumerable.length
31
- elsif @enumerable.respond_to?(:count)
32
- @enumerable.count
33
- elsif @enumerable.respond_to?(:to_a)
34
- @enumerable.to_a.length
35
- else
36
- @enumerable.inject(0){ |length, obj| length + 1 }
34
+ @enumerable
37
35
  end
38
36
  end
39
37
 
40
38
  # returns self but changes title
41
- def with_progress(title = nil, &block)
42
- self.class.new(@enumerable, title, @length, &block)
39
+ def with_progress(title = nil, length = nil, &block)
40
+ self.class.new(@enumerable, title, length || @length, &block)
43
41
  end
44
42
  end
45
43
  end
data/progress.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'progress'
5
- s.version = '2.2.0'
5
+ s.version = '2.3.0'
6
6
  s.summary = %q{Show progress of long running tasks}
7
7
  s.homepage = "http://github.com/toy/#{s.name}"
8
8
  s.authors = ['Ivan Kuchin']
@@ -247,6 +247,49 @@ describe Progress do
247
247
  wp_wp.enumerable.should == wp.enumerable
248
248
  end
249
249
  end
250
+
251
+ describe "calls to each" do
252
+ class CallsToEach
253
+ include Enumerable
254
+
255
+ COUNT = 100
256
+ end
257
+
258
+ def init_calls_to_each
259
+ @enum = CallsToEach.new
260
+ @objects = 10.times.to_a
261
+ @enum.should_receive(:each).once{ |&block|
262
+ @objects.each(&block)
263
+ }
264
+ end
265
+
266
+ it "should call each only one time for object with length" do
267
+ init_calls_to_each
268
+ @enum.should_receive(:length).and_return(10)
269
+ got = []
270
+ @enum.with_progress.each{ |o| got << o }.should == @enum
271
+ got.should == @objects
272
+ end
273
+
274
+ it "should call each only one time for object without length" do
275
+ init_calls_to_each
276
+ got = []
277
+ @enum.with_progress.each{ |o| got << o }.should == @enum
278
+ got.should == @objects
279
+ end
280
+
281
+ it "should call each only one time for String" do
282
+ @objects = ('a'..'z').map{ |c| "#{c}\n" }
283
+ str = @objects.join('')
284
+ str.should_not_receive(:length)
285
+ str.should_receive(:each).once{ |&block|
286
+ @objects.each(&block)
287
+ }
288
+ got = []
289
+ str.with_progress.each{ |o| got << o }.should == str
290
+ got.should == @objects
291
+ end
292
+ end
250
293
  end
251
294
  end
252
295
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: progress
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 3
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 2.2.0
10
+ version: 2.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ivan Kuchin
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-12-04 00:00:00 Z
18
+ date: 2011-12-25 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
80
  requirements: []
81
81
 
82
82
  rubyforge_project: progress
83
- rubygems_version: 1.8.11
83
+ rubygems_version: 1.8.12
84
84
  signing_key:
85
85
  specification_version: 3
86
86
  summary: Show progress of long running tasks