rescue_each 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.2
1
+ 1.1.3
data/lib/rescue_each.rb CHANGED
@@ -69,7 +69,7 @@ module RescueEach
69
69
 
70
70
  RESCUE_EACH_OPTIONS = [:stderr, :error_limit]
71
71
 
72
- def rescue_each(options = {})
72
+ def rescue_each(options = {}, &block)
73
73
 
74
74
  options.assert_valid_keys :method, :args, *RESCUE_EACH_OPTIONS
75
75
  options.reverse_merge! :method => :each
@@ -78,7 +78,7 @@ module RescueEach
78
78
  errors = []
79
79
  retval = __send__ options[:method], *options[:args] do |*args|
80
80
  begin
81
- yield *args.dup
81
+ block.call *args.dup
82
82
  rescue Exception => e
83
83
 
84
84
  item = RescueEach::Error::Item.new e, args
@@ -88,11 +88,12 @@ module RescueEach
88
88
  $stderr.puts "rescue_each error: #{item.short_message}"
89
89
  end
90
90
 
91
- if e.class.name == 'IRB::Abort'
91
+ # should fail immediately on Interrupt exceptions (i.e. Ctrl-C)
92
+ if %w(Interrupt IRB::Abort).include? e.class.name
92
93
  if errors.empty?
93
94
  raise
94
95
  else
95
- raise ::IRB::Abort, e.message + "\n" + RescueEach::Error.new(errors).to_s
96
+ raise e.class, e.message + "\n" + RescueEach::Error.new(errors).to_s
96
97
  end
97
98
  end
98
99
 
@@ -112,7 +113,7 @@ module RescueEach
112
113
 
113
114
  args = args.dup
114
115
  options = args.extract_options!
115
- rescue_options = options.reject { |k,v| !RESCUE_EACH_OPTIONS.include? k }
116
+ rescue_options = options.slice *RESCUE_EACH_OPTIONS
116
117
  options.except! *RESCUE_EACH_OPTIONS
117
118
  args << options unless options.empty?
118
119
 
@@ -66,7 +66,7 @@ class RescueEachTest < ActiveSupport::TestCase
66
66
  end
67
67
  end
68
68
 
69
- test "each_with_index args pass through correctly" do
69
+ test "rescue_each_with_index args pass through correctly with 2 params" do
70
70
  output = []
71
71
  [:foo, :bar].rescue_each_with_index do |obj,i|
72
72
  output << [obj,i]
@@ -75,6 +75,33 @@ class RescueEachTest < ActiveSupport::TestCase
75
75
  assert_equal expected, output
76
76
  end
77
77
 
78
+ test "rescue_each_with_index args pass through correctly args param" do
79
+ output = []
80
+ [:foo, :bar].rescue_each_with_index do |*args|
81
+ output << args
82
+ end
83
+ expected = [[:foo, 0], [:bar, 1]]
84
+ assert_equal expected, output
85
+ end
86
+
87
+ test "Hash#rescue_each args for single block param" do
88
+ input = {:foo => 42, :bar => 12}
89
+ output = []
90
+ input.rescue_each do |args|
91
+ output << args
92
+ end
93
+ assert_equal input.to_a, output
94
+ end
95
+
96
+ test "Hash#rescue_each args for key/value block params" do
97
+ input = {:foo => 42, :bar => 12}
98
+ output = []
99
+ input.rescue_each do |k, v|
100
+ output << [k,v]
101
+ end
102
+ assert_equal input.to_a, output
103
+ end
104
+
78
105
  test "error object contains args that triggered error" do
79
106
  error_object = nil
80
107
  begin
@@ -153,6 +180,26 @@ class RescueEachTest < ActiveSupport::TestCase
153
180
 
154
181
  end
155
182
 
183
+ test "Ctrl-C from outside IRB should break out of the loop" do
184
+
185
+ error_object = nil
186
+ output = []
187
+ begin
188
+ (1..5).rescue_each do |i|
189
+ raise 'foo bar' if i == 2
190
+ output << i
191
+ raise ::Interrupt if i == 4
192
+ end
193
+ rescue ::Interrupt => e
194
+ error_object = e
195
+ end
196
+
197
+ assert_equal [1,3,4], output
198
+ assert_kind_of ::Interrupt, error_object
199
+ assert_match /foo bar/, error_object.message
200
+
201
+ end
202
+
156
203
  test "no stderr option doesn't output to stderr" do
157
204
  err = capture_stderr do
158
205
  assert_raise RescueEach::Error do
@@ -215,10 +262,13 @@ class RescueEachTest < ActiveSupport::TestCase
215
262
  assert_true odds.all? &:odd?
216
263
  end
217
264
 
218
- test "find_each exists on active record objects" do
219
- [:find_each, :find_in_batches].each do |method|
220
- assert_true ActiveRecord::Base.methods.include? "#{method}"
221
- assert_true ActiveRecord::Base.methods.include? "rescue_#{method}"
265
+ test "rescued find methods exist on active record objects" do
266
+ [:find_each, :find_in_batches].each do |method_base|
267
+ ["#{method_base}", "rescue_#{method_base}"].each do |method|
268
+ [ActiveRecord::Base, ActiveRecord::Base.scoped(:limit => 42)].each do |object|
269
+ assert_true object.respond_to? method
270
+ end
271
+ end
222
272
  end
223
273
  end
224
274
 
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rescue_each
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 1
8
+ - 3
9
+ version: 1.1.3
5
10
  platform: ruby
6
11
  authors:
7
12
  - Jason Weathered
@@ -9,29 +14,33 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2010-02-15 00:00:00 +10:00
17
+ date: 2010-05-02 00:00:00 +10:00
13
18
  default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: activesupport
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
23
29
  version: "0"
24
- version:
30
+ type: :runtime
31
+ version_requirements: *id001
25
32
  - !ruby/object:Gem::Dependency
26
33
  name: activerecord
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
30
36
  requirements:
31
37
  - - ">="
32
38
  - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
33
41
  version: "0"
34
- version:
42
+ type: :development
43
+ version_requirements: *id002
35
44
  description:
36
45
  email: jason@jasoncodes.com
37
46
  executables: []
@@ -63,18 +72,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
63
72
  requirements:
64
73
  - - ">="
65
74
  - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
66
77
  version: "0"
67
- version:
68
78
  required_rubygems_version: !ruby/object:Gem::Requirement
69
79
  requirements:
70
80
  - - ">="
71
81
  - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
72
84
  version: "0"
73
- version:
74
85
  requirements: []
75
86
 
76
87
  rubyforge_project:
77
- rubygems_version: 1.3.5
88
+ rubygems_version: 1.3.6
78
89
  signing_key:
79
90
  specification_version: 3
80
91
  summary: Rescue multiple exceptions when enumerating over Enumerable or ActiveRecord objects