batch 1.0.2 → 1.0.3

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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ Yzk2NTUwZGY3OGIwZGY0MmM4MmMzZWIwMmMwYjI3YzU3YjA1MzhkYQ==
5
+ data.tar.gz: !binary |-
6
+ NmE4NGM0Y2ZkMzNhNDM5N2RjOTI1NWNlZDhmNzY1NDdhMmUwOWQ0NQ==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ ODYwOWUyNTA0OGU0Zjc0MGZhMjFkNTI5ZDU1YzUyYjVlNmQ3MjFiMDlhY2Ew
10
+ ZjFhNDEzMzkxNmRkMWYyOWQwMjRkNWYxYTg5NWE5OGMyYjZmMDJjNzRmZDdj
11
+ OTI0YjFmZDlmNTA2MTk3OTI0MGRmMjc3YWZkODEyNTU4YWI4ZGQ=
12
+ data.tar.gz: !binary |-
13
+ YzVhYjdmN2Q4MTQ5ZDlmN2ZkODQ2NTQzNDkzNDlhZjM5ZDk5M2UzMDFmMzVm
14
+ YmE4N2I0YWQyYmM2Y2NjYzQwOTBiMjAyNDRmMTBjZDk3MDVjNjAwMWQ2MTMy
15
+ NjY5MTRlZWM0N2E0N2FiOTg4MWQ4MWU5ZTgwYWEzYmRjMjM4ZDc=
data/README.markdown CHANGED
@@ -47,6 +47,30 @@ don't get interrupted right after you go to bed:
47
47
  You can determine the line width by setting the environment variable
48
48
  `BATCH_WIDTH`, which defaults to 75.
49
49
 
50
+ Disabling output
51
+ ----------------
52
+
53
+ On some environments, like a non-interactive shell, you probably want Batch
54
+ to still run your stuff and skip errors, but you don't want all the progress
55
+ output. For this purpose you can tweak `BATCH_INTERACTIVE`:
56
+
57
+ $ BATCH_INTERACTIVE=0 rake foo
58
+
59
+ It's probably useful to have `BATCH_INTERACTIVE` set to `0` on your crontabs.
60
+
61
+ Debugging
62
+ ---------
63
+
64
+ If you want Batch to halt as soon as there's an exception (just like a regular
65
+ `each` loop would do), simply set `$DEBUG` to true. If you're running Ruby
66
+ explicitly, then:
67
+
68
+ $ ruby -d <your-batch-script>
69
+
70
+ If you're running another tool which uses Batch, then set `RUBYOPT`:
71
+
72
+ $ RUBYOPT=-d rake foo
73
+
50
74
  Installation
51
75
  ------------
52
76
 
@@ -0,0 +1,9 @@
1
+ require_relative "../lib/batch"
2
+
3
+ payload = 500.times.to_a
4
+
5
+ Batch.start("Processing items", payload) do |item|
6
+ 1 / (item % 100)
7
+
8
+ sleep(0.01)
9
+ end
data/examples/slow.rb ADDED
@@ -0,0 +1,7 @@
1
+ require_relative "../lib/batch"
2
+
3
+ payload = 500.times.to_a
4
+
5
+ Batch.start("Big data", payload) do |item|
6
+ sleep(item / 100.0)
7
+ end
data/examples/start.rb ADDED
@@ -0,0 +1,6 @@
1
+ require_relative "../lib/batch"
2
+
3
+ payload = 500.times.to_a
4
+
5
+ Batch.start("Working", payload) do |item|
6
+ end
data/lib/batch.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  class Batch
2
- VERSION = "1.0.2"
2
+ VERSION = "1.0.3"
3
3
 
4
4
  attr :enumerable
5
5
 
@@ -13,35 +13,40 @@ class Batch
13
13
  @errors = []
14
14
  @current = 0
15
15
 
16
- report_progress
16
+ Batch.out do |io|
17
+ enumerable.each.with_index do |item, index|
18
+ report_progress(io) if index == 0
17
19
 
18
- enumerable.each do |item|
19
- begin
20
- yield(item)
21
- print "."
20
+ begin
21
+ yield(item)
22
+ io.print "."
22
23
 
23
- rescue Interrupt => e
24
- report_errors
25
- raise e
24
+ rescue Interrupt => e
25
+ report_errors
26
+ raise e
26
27
 
27
- rescue Exception => e
28
- print "E"
29
- @errors << [item, e]
28
+ rescue Exception => e
29
+ raise e if $DEBUG
30
+ io.print "E"
31
+ @errors << [item, e]
30
32
 
31
- ensure
32
- @current += 1
33
+ ensure
34
+ @current += 1
33
35
 
34
- if eol?
35
- print "\n"
36
- report_progress
36
+ if eol?
37
+ io.print "\n"
38
+ report_progress(io)
39
+ end
37
40
  end
38
41
  end
39
- end
40
42
 
41
- print "\n"
42
- puts "100% " unless eol?
43
+ if @current > 0
44
+ io.print "\n"
45
+ io.puts "100% " unless eol?
43
46
 
44
- report_errors
47
+ report_errors
48
+ end
49
+ end
45
50
 
46
51
  nil
47
52
  end
@@ -71,11 +76,11 @@ class Batch
71
76
  @current * 100 / @size
72
77
  end
73
78
 
74
- def report_progress
79
+ def report_progress(io)
75
80
  if progress
76
- print "#{progress.to_s.rjust 3, " "}% "
81
+ io.print "#{progress.to_s.rjust 3, " "}% "
77
82
  else
78
- print " ? "
83
+ io.print " ? "
79
84
  end
80
85
  end
81
86
 
@@ -88,9 +93,26 @@ class Batch
88
93
  end
89
94
 
90
95
  def self.start(title, enumerable, &block)
91
- puts
92
- puts(title)
93
- puts
96
+ begin
97
+ enumerable.each.next
98
+ rescue StopIteration
99
+ return
100
+ end
101
+
102
+ out do |io|
103
+ io.puts
104
+ io.puts(title)
105
+ io.puts
106
+ end
107
+
94
108
  each(enumerable, &block)
95
109
  end
110
+
111
+ def self.out(&block)
112
+ interactive? ? yield($stdout) : File.open("/dev/null", "w", &block)
113
+ end
114
+
115
+ def self.interactive?
116
+ (ENV["BATCH_INTERACTIVE"] || 1).to_i == 1
117
+ end
96
118
  end
data/test/batch_test.rb CHANGED
@@ -3,10 +3,6 @@
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
-
10
6
  should "report" do
11
7
  stdout, _ = capture do
12
8
  Batch.each((1..80).to_a) do |item|
@@ -48,11 +44,11 @@ EOS
48
44
  end
49
45
 
50
46
  should "use BATCH_WIDTH" do
51
- ENV["BATCH_WIDTH"] = "40"
52
-
53
- stdout, _ = capture do
54
- Batch.each((1..80).to_a) do |item|
55
- item + 1
47
+ stdout, _ = with_env("BATCH_WIDTH" => "40") do
48
+ capture do
49
+ Batch.each((1..80).to_a) do |item|
50
+ item + 1
51
+ end
56
52
  end
57
53
  end
58
54
 
@@ -96,15 +92,82 @@ EOS
96
92
  assert_equal expected.rstrip, stdout.rstrip
97
93
  end
98
94
 
99
- should "work with empty enumerables" do
100
- stdout, _ = capture do
95
+ should "not print anything when there's nothing to do" do
96
+ stdout, stderr = capture do
101
97
  Batch.each([]) { }
102
98
  end
103
99
 
104
- expected = <<-EOS
105
- 0%
106
- EOS
100
+ assert_empty stdout.rstrip
101
+ assert_empty stderr.rstrip
107
102
 
108
- assert_equal expected.rstrip, stdout.rstrip
103
+ stdout, stderr = capture do
104
+ Batch.start("Not much work", []) { }
105
+ end
106
+
107
+ assert_empty stdout.rstrip
108
+ assert_empty stderr.rstrip
109
+ end
110
+
111
+ should "be silent when BATCH_INTERACTIVE is 0" do
112
+ stdout, stderr = with_env("BATCH_INTERACTIVE" => "0") do
113
+ capture do
114
+ Batch.each((1..80).to_a) do |item|
115
+ item + 1
116
+ end
117
+ end
118
+ end
119
+
120
+ assert_empty stdout.rstrip
121
+ assert_empty stderr.rstrip
122
+
123
+ stdout, stderr = with_env("BATCH_INTERACTIVE" => "0") do
124
+ capture do
125
+ Batch.start("Silent actually", (1..80).to_a) do |item|
126
+ item + 1
127
+ end
128
+ end
129
+ end
130
+
131
+ assert_empty stdout.rstrip
132
+ assert_empty stderr.rstrip
133
+ end
134
+
135
+ should "halt when $DEBUG is set to true" do
136
+ items = []
137
+
138
+ stdout, stderr = capture do
139
+ begin
140
+ $DEBUG = true
141
+
142
+ assert_raises(ArgumentError) do
143
+ Batch.each((1..80).to_a) do |item|
144
+ items << item
145
+ raise ArgumentError, "Two is bad." if item == 2
146
+ end
147
+ end
148
+ ensure
149
+ $DEBUG = false
150
+ end
151
+ end
152
+
153
+ assert_equal [1, 2], items
154
+ assert_equal " 0% .", stdout
155
+ end
156
+
157
+ def with_env(env)
158
+ old = {}
159
+
160
+ begin
161
+ env.each do |key, value|
162
+ old[key] = ENV[key]
163
+ ENV[key] = value
164
+ end
165
+
166
+ yield
167
+ ensure
168
+ old.each do |key, value|
169
+ ENV[key] = value
170
+ end
171
+ end
109
172
  end
110
173
  end
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: batch
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
5
- prerelease:
4
+ version: 1.0.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Damian Janowski
@@ -10,7 +9,7 @@ authors:
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
- date: 2013-01-21 00:00:00.000000000 Z
12
+ date: 2013-06-10 00:00:00.000000000 Z
14
13
  dependencies: []
15
14
  description:
16
15
  email:
@@ -27,31 +26,33 @@ files:
27
26
  - UNLICENSE
28
27
  - batch.gemspec
29
28
  - batch.gemspec.erb
29
+ - examples/errors.rb
30
+ - examples/slow.rb
31
+ - examples/start.rb
30
32
  - lib/batch.rb
31
33
  - test/batch_test.rb
32
34
  - test/test_helper.rb
33
35
  homepage: http://github.com/djanowski/batch
34
36
  licenses: []
37
+ metadata: {}
35
38
  post_install_message:
36
39
  rdoc_options: []
37
40
  require_paths:
38
41
  - lib
39
42
  required_ruby_version: !ruby/object:Gem::Requirement
40
- none: false
41
43
  requirements:
42
44
  - - ! '>='
43
45
  - !ruby/object:Gem::Version
44
46
  version: '0'
45
47
  required_rubygems_version: !ruby/object:Gem::Requirement
46
- none: false
47
48
  requirements:
48
49
  - - ! '>='
49
50
  - !ruby/object:Gem::Version
50
51
  version: '0'
51
52
  requirements: []
52
53
  rubyforge_project:
53
- rubygems_version: 1.8.23
54
+ rubygems_version: 2.0.0
54
55
  signing_key:
55
- specification_version: 3
56
+ specification_version: 4
56
57
  summary: Iterate Enumerables with progress reporting.
57
58
  test_files: []