rescue_each 1.1.1 → 1.1.2
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/README.markdown +4 -4
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/rescue_each.rb +9 -3
- data/test/rescue_each_test.rb +29 -2
- metadata +2 -2
data/README.markdown
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# `rescue_each`
|
2
|
-
## Rescue multiple exceptions when
|
2
|
+
## Rescue multiple exceptions when enumerating over Enumerable or ActiveRecord objects.
|
3
3
|
|
4
4
|
Say you have a batch rake task which runs from cron which consists of many independent tasks ran in a loop.
|
5
5
|
These tasks could be anything from updating cached database entries to file conversions.
|
@@ -33,12 +33,12 @@ transforms into:
|
|
33
33
|
raise 'example'
|
34
34
|
end
|
35
35
|
|
36
|
-
You'll probably find this handy if your batch task
|
36
|
+
You'll probably find this handy if your batch task has its own status output as this mode will output an error summary inline.
|
37
37
|
|
38
38
|
#### Other Methods
|
39
39
|
|
40
|
-
`rescue_each` provides proxies for `each_with_index`, `find_each` and `find_in_batches`.
|
41
|
-
|
40
|
+
`rescue_each` also provides proxies for `map`, `each_with_index`, `find_each` and `find_in_batches`.
|
41
|
+
You can also use `rescue_each` on any method taking a block by calling `rescue_send`:
|
42
42
|
|
43
43
|
odds = (1..5).rescue_send(:reject) { |i| i%2 == 0 }
|
44
44
|
|
data/Rakefile
CHANGED
@@ -19,7 +19,7 @@ begin
|
|
19
19
|
require 'jeweler'
|
20
20
|
Jeweler::Tasks.new do |gem|
|
21
21
|
gem.name = "rescue_each"
|
22
|
-
gem.summary = "Rescue multiple exceptions when
|
22
|
+
gem.summary = "Rescue multiple exceptions when enumerating over Enumerable or ActiveRecord objects"
|
23
23
|
gem.email = "jason@jasoncodes.com"
|
24
24
|
gem.homepage = "http://github.com/jasoncodes/rescue_each"
|
25
25
|
gem.authors = ["Jason Weathered"]
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.1.
|
1
|
+
1.1.2
|
data/lib/rescue_each.rb
CHANGED
@@ -4,10 +4,11 @@ module RescueEach
|
|
4
4
|
|
5
5
|
class Error < StandardError
|
6
6
|
|
7
|
-
attr_reader :errors
|
7
|
+
attr_reader :errors, :aborted
|
8
8
|
|
9
|
-
def initialize(errors)
|
9
|
+
def initialize(errors, aborted=false)
|
10
10
|
@errors = errors
|
11
|
+
@aborted = aborted
|
11
12
|
end
|
12
13
|
|
13
14
|
class Item < Struct.new :exception, :args
|
@@ -56,6 +57,7 @@ module RescueEach
|
|
56
57
|
msg << "\n"
|
57
58
|
end
|
58
59
|
msg << "rescue_each caught #{errors.size} errors"
|
60
|
+
msg << ", and then aborted." if aborted
|
59
61
|
msg.join
|
60
62
|
end
|
61
63
|
|
@@ -65,7 +67,7 @@ module RescueEach
|
|
65
67
|
|
66
68
|
module Object
|
67
69
|
|
68
|
-
RESCUE_EACH_OPTIONS = [:stderr]
|
70
|
+
RESCUE_EACH_OPTIONS = [:stderr, :error_limit]
|
69
71
|
|
70
72
|
def rescue_each(options = {})
|
71
73
|
|
@@ -96,6 +98,10 @@ module RescueEach
|
|
96
98
|
|
97
99
|
errors << item
|
98
100
|
|
101
|
+
if options[:error_limit] && errors.size >= options[:error_limit]
|
102
|
+
raise RescueEach::Error.new(errors, true)
|
103
|
+
end
|
104
|
+
|
99
105
|
end
|
100
106
|
end
|
101
107
|
raise RescueEach::Error, errors unless errors.empty?
|
data/test/rescue_each_test.rb
CHANGED
@@ -16,14 +16,35 @@ class RescueEachTest < ActiveSupport::TestCase
|
|
16
16
|
end
|
17
17
|
|
18
18
|
test "continues after an error" do
|
19
|
+
error_object = nil
|
19
20
|
output = []
|
20
|
-
|
21
|
+
begin
|
21
22
|
(1..5).rescue_each do |x|
|
22
23
|
output << x
|
23
24
|
raise 'test' if x == 3
|
24
25
|
end
|
26
|
+
rescue RescueEach::Error => e
|
27
|
+
error_object = e
|
25
28
|
end
|
26
29
|
assert_equal (1..5).collect, output
|
30
|
+
assert_false error_object.aborted
|
31
|
+
assert_no_match /and then aborted/, error_object.to_s.lines.collect.last
|
32
|
+
end
|
33
|
+
|
34
|
+
test "stops after error limit" do
|
35
|
+
error_object = nil
|
36
|
+
output = []
|
37
|
+
begin
|
38
|
+
(1..10).rescue_each :error_limit => 3 do |x|
|
39
|
+
output << x
|
40
|
+
raise 'test' if x%2 == 0
|
41
|
+
end
|
42
|
+
rescue RescueEach::Error => e
|
43
|
+
error_object = e
|
44
|
+
end
|
45
|
+
assert_equal (1..6).collect, output
|
46
|
+
assert_true error_object.aborted
|
47
|
+
assert_match /and then aborted/, error_object.to_s.lines.collect.last
|
27
48
|
end
|
28
49
|
|
29
50
|
test "empty array doesn't call block" do
|
@@ -183,11 +204,17 @@ class RescueEachTest < ActiveSupport::TestCase
|
|
183
204
|
assert_match /lorem ipsum/, err
|
184
205
|
end
|
185
206
|
|
186
|
-
test "
|
207
|
+
test "rescue_map returns output of proxied method" do
|
187
208
|
output = (1..5).rescue_map { |x| x*x }
|
188
209
|
assert_equal [1,4,9,16,25], output
|
189
210
|
end
|
190
211
|
|
212
|
+
test "rescue_send calls correct method and returns result" do
|
213
|
+
odds = (1..5).rescue_send(:reject) { |i| i%2 == 0 }
|
214
|
+
assert_false odds.empty?
|
215
|
+
assert_true odds.all? &:odd?
|
216
|
+
end
|
217
|
+
|
191
218
|
test "find_each exists on active record objects" do
|
192
219
|
[:find_each, :find_in_batches].each do |method|
|
193
220
|
assert_true ActiveRecord::Base.methods.include? "#{method}"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rescue_each
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Weathered
|
@@ -77,7 +77,7 @@ rubyforge_project:
|
|
77
77
|
rubygems_version: 1.3.5
|
78
78
|
signing_key:
|
79
79
|
specification_version: 3
|
80
|
-
summary: Rescue multiple exceptions when
|
80
|
+
summary: Rescue multiple exceptions when enumerating over Enumerable or ActiveRecord objects
|
81
81
|
test_files:
|
82
82
|
- test/rescue_each_test.rb
|
83
83
|
- test/test_helper.rb
|