batch 0.0.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -7,7 +7,7 @@ Description
7
7
  -----------
8
8
 
9
9
  Say you have a thousand images to process. You write a script, fire it and go
10
- to bed, only to realize the morning after that and exception was raised and the
10
+ to bed, only to realize the morning after that an exception was raised and the
11
11
  script was aborted. Well, no more frustration: now you can use Batch to make
12
12
  sure your script continues working despite those exceptions, and you can now
13
13
  get a nice report to read while you drink your morning coffee.
data/UNLICENSE ADDED
@@ -0,0 +1,24 @@
1
+ This is free and unencumbered software released into the public domain.
2
+
3
+ Anyone is free to copy, modify, publish, use, compile, sell, or
4
+ distribute this software, either in source code form or as a compiled
5
+ binary, for any purpose, commercial or non-commercial, and by any
6
+ means.
7
+
8
+ In jurisdictions that recognize copyright laws, the author or authors
9
+ of this software dedicate any and all copyright interest in the
10
+ software to the public domain. We make this dedication for the benefit
11
+ of the public at large and to the detriment of our heirs and
12
+ successors. We intend this dedication to be an overt act of
13
+ relinquishment in perpetuity of all present and future rights to this
14
+ software under copyright law.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20
+ OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21
+ ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22
+ OTHER DEALINGS IN THE SOFTWARE.
23
+
24
+ For more information, please refer to <http://unlicense.org/>
data/batch.gemspec CHANGED
@@ -1,9 +1,9 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "batch"
3
- s.version = "0.0.3"
3
+ s.version = "1.0.0"
4
4
  s.summary = "Iterate Enumerables with progress reporting."
5
5
  s.authors = ["Damian Janowski", "Michel Martens"]
6
6
  s.email = ["djanowski@dimaion.com", "michel@soveran.com"]
7
7
  s.homepage = "http://github.com/djanowski/batch"
8
- s.files = ["LICENSE", "README.markdown", "Rakefile", "lib/batch.rb", "batch.gemspec", "test/batch_test.rb", "test/test_helper.rb"]
8
+ s.files = ["UNLICENSE", "README.markdown", "Rakefile", "lib/batch.rb", "batch.gemspec", "test/batch_test.rb", "test/test_helper.rb"]
9
9
  end
data/lib/batch.rb CHANGED
@@ -6,21 +6,22 @@ class Batch
6
6
  def initialize(enumerable)
7
7
  @enumerable = enumerable
8
8
  @width = (ENV["BATCH_WIDTH"] || 75).to_i
9
+ @size = enumerable.size if enumerable.respond_to?(:size)
9
10
  end
10
11
 
11
12
  def each(&block)
12
13
  @errors = []
13
14
  @current = 0
14
15
 
15
- print " 0% "
16
+ report_progress
16
17
 
17
18
  enumerable.each do |item|
18
19
  begin
19
20
  yield(item)
20
21
  print "."
21
22
 
22
- rescue Interrupt
23
- break
23
+ rescue Interrupt => e
24
+ raise e
24
25
 
25
26
  rescue Exception => e
26
27
  print "E"
@@ -29,11 +30,15 @@ class Batch
29
30
  ensure
30
31
  @current += 1
31
32
 
32
- report_progress if eol?
33
+ if eol?
34
+ print "\n"
35
+ report_progress
36
+ end
33
37
  end
34
38
  end
35
39
 
36
40
  print "\n"
41
+ puts "100% " unless eol?
37
42
 
38
43
  report_errors
39
44
 
@@ -55,22 +60,35 @@ class Batch
55
60
  end
56
61
 
57
62
  def total
58
- @enumerable.size
63
+ @size
59
64
  end
60
65
 
61
66
  def progress
62
- @current * 100 / total
67
+ return unless @size
68
+
69
+ @current * 100 / @size
63
70
  end
64
71
 
65
72
  def report_progress
66
- print "\n#{progress.to_s.rjust 3, " "}% "
73
+ if progress
74
+ print "#{progress.to_s.rjust 3, " "}% "
75
+ else
76
+ print " ? "
77
+ end
67
78
  end
68
79
 
69
80
  def eol?
70
- @current % @width == 0 || @current == total
81
+ @current % @width == 0
71
82
  end
72
83
 
73
84
  def self.each(enumerable, &block)
74
85
  new(enumerable).each(&block)
75
86
  end
87
+
88
+ def self.start(title, enumerable, &block)
89
+ puts
90
+ puts(title)
91
+ puts
92
+ each(enumerable, &block)
93
+ end
76
94
  end
data/test/batch_test.rb CHANGED
@@ -3,6 +3,10 @@
3
3
  require File.expand_path("./test_helper", File.dirname(__FILE__))
4
4
 
5
5
  class BatchTest < Test::Unit::TestCase
6
+ setup do
7
+ ENV["BATCH_WIDTH"] = nil
8
+ end
9
+
6
10
  should "report" do
7
11
  stdout, _ = capture do
8
12
  Batch.each((1..80).to_a) do |item|
@@ -45,6 +49,7 @@ EOS
45
49
 
46
50
  should "use BATCH_WIDTH" do
47
51
  ENV["BATCH_WIDTH"] = "40"
52
+
48
53
  stdout, _ = capture do
49
54
  Batch.each((1..80).to_a) do |item|
50
55
  item + 1
@@ -55,6 +60,37 @@ EOS
55
60
  0% ........................................
56
61
  50% ........................................
57
62
  100%
63
+ EOS
64
+
65
+ assert_equal expected.rstrip, stdout.rstrip
66
+ end
67
+
68
+ should "print a title" do
69
+ stdout, _ = capture do
70
+ Batch.start("Printing numbers", [1]) do |item|
71
+ end
72
+ end
73
+
74
+ expected = <<-EOS
75
+
76
+ Printing numbers
77
+
78
+ 0% .
79
+ 100%
80
+ EOS
81
+
82
+ assert_equal expected.rstrip, stdout.rstrip
83
+ end
84
+
85
+ should "work with unknown sizes" do
86
+ stdout, _ = capture do
87
+ Batch.each(1..80) { }
88
+ end
89
+
90
+ expected = <<-EOS
91
+ ? ...........................................................................
92
+ ? .....
93
+ 100%
58
94
  EOS
59
95
 
60
96
  assert_equal expected.rstrip, stdout.rstrip
metadata CHANGED
@@ -1,73 +1,54 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: batch
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 3
9
- version: 0.0.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Damian Janowski
13
9
  - Michel Martens
14
10
  autorequire:
15
11
  bindir: bin
16
12
  cert_chain: []
17
-
18
- date: 2010-09-20 00:00:00 -03:00
19
- default_executable:
13
+ date: 2012-12-07 00:00:00.000000000 Z
20
14
  dependencies: []
21
-
22
15
  description:
23
- email:
16
+ email:
24
17
  - djanowski@dimaion.com
25
18
  - michel@soveran.com
26
19
  executables: []
27
-
28
20
  extensions: []
29
-
30
21
  extra_rdoc_files: []
31
-
32
- files:
33
- - LICENSE
22
+ files:
23
+ - UNLICENSE
34
24
  - README.markdown
35
25
  - Rakefile
36
26
  - lib/batch.rb
37
27
  - batch.gemspec
38
28
  - test/batch_test.rb
39
29
  - test/test_helper.rb
40
- has_rdoc: true
41
30
  homepage: http://github.com/djanowski/batch
42
31
  licenses: []
43
-
44
32
  post_install_message:
45
33
  rdoc_options: []
46
-
47
- require_paths:
34
+ require_paths:
48
35
  - lib
49
- required_ruby_version: !ruby/object:Gem::Requirement
36
+ required_ruby_version: !ruby/object:Gem::Requirement
50
37
  none: false
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- segments:
55
- - 0
56
- version: "0"
57
- required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
43
  none: false
59
- requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- segments:
63
- - 0
64
- version: "0"
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
65
48
  requirements: []
66
-
67
49
  rubyforge_project:
68
- rubygems_version: 1.3.7
50
+ rubygems_version: 1.8.21
69
51
  signing_key:
70
52
  specification_version: 3
71
53
  summary: Iterate Enumerables with progress reporting.
72
54
  test_files: []
73
-
data/LICENSE DELETED
@@ -1,19 +0,0 @@
1
- Copyright (c) 2010 Michel Martens & Damian Janowski
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy
4
- of this software and associated documentation files (the "Software"), to deal
5
- in the Software without restriction, including without limitation the rights
6
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- copies of the Software, and to permit persons to whom the Software is
8
- furnished to do so, subject to the following conditions:
9
-
10
- The above copyright notice and this permission notice shall be included in
11
- all copies or substantial portions of the Software.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- THE SOFTWARE.