rescue_each 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -6,6 +6,12 @@ These tasks could be anything from updating cached database entries to file conv
6
6
 
7
7
  Once of these tasks fails, perhaps there's a corrupt image. Normally this would mean the entire batch task fails. But with rescue_each the other items can be processed and any errors will be re-raised at the end to be caught by your cron script.
8
8
 
9
+ ### Installation
10
+
11
+ You can install from Gemcutter by running:
12
+
13
+ sudo gem install rescue_each
14
+
9
15
  ### Usage
10
16
 
11
17
  #### Basics
data/Rakefile CHANGED
@@ -25,6 +25,7 @@ begin
25
25
  gem.authors = ["Jason Weathered"]
26
26
  gem.has_rdoc = false
27
27
  gem.add_dependency 'activesupport'
28
+ gem.add_development_dependency 'activerecord'
28
29
  end
29
30
  Jeweler::GemcutterTasks.new
30
31
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.0
1
+ 1.1.1
data/lib/rescue_each.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'active_support'
2
2
 
3
3
  module RescueEach
4
+
4
5
  class Error < StandardError
5
6
 
6
7
  attr_reader :errors
@@ -16,11 +17,12 @@ module RescueEach
16
17
  end
17
18
  def args_short
18
19
  args_str = args.map do |arg|
19
- lines = arg.inspect.lines.collect
20
- if lines.size == 1
21
- lines.first
20
+ str = arg.inspect
21
+ max_length = 500
22
+ if str.size > max_length
23
+ str.slice(0, max_length) + " [#{str.size-max_length} more chars...]"
22
24
  else
23
- lines.first + " [#{lines.size-1} more...]"
25
+ str
24
26
  end
25
27
  end
26
28
  "args: " + args_str.join(", ")
@@ -58,76 +60,101 @@ module RescueEach
58
60
  end
59
61
 
60
62
  end
61
- end
62
-
63
- module Enumerable
64
63
 
65
- RESCUE_EACH_OPTIONS = [:stderr]
66
-
67
- def rescue_each(options = {})
68
-
69
- options.assert_valid_keys :method, :args, *RESCUE_EACH_OPTIONS
70
- options.reverse_merge! :method => :each
71
- options.reverse_merge! :args => []
64
+ module CoreExt
72
65
 
73
- errors = []
74
- retval = send options[:method], *options[:args] do |*args|
75
- begin
76
- yield *args.dup
77
- rescue Exception => e
66
+ module Object
67
+
68
+ RESCUE_EACH_OPTIONS = [:stderr]
69
+
70
+ def rescue_each(options = {})
78
71
 
79
- item = RescueEach::Error::Item.new e, args
80
- if options[:stderr] == :full
81
- $stderr.puts "rescue_each error: #{item}" if options[:stderr]
82
- elsif options[:stderr]
83
- $stderr.puts "rescue_each error: #{item.short_message}"
84
- end
72
+ options.assert_valid_keys :method, :args, *RESCUE_EACH_OPTIONS
73
+ options.reverse_merge! :method => :each
74
+ options.reverse_merge! :args => []
85
75
 
86
- if e.class.name == 'IRB::Abort'
87
- if errors.empty?
88
- raise
89
- else
90
- raise ::IRB::Abort, e.message + "\n" + RescueEach::Error.new(errors).to_s
76
+ errors = []
77
+ retval = __send__ options[:method], *options[:args] do |*args|
78
+ begin
79
+ yield *args.dup
80
+ rescue Exception => e
81
+
82
+ item = RescueEach::Error::Item.new e, args
83
+ if options[:stderr] == :full
84
+ $stderr.puts "rescue_each error: #{item}" if options[:stderr]
85
+ elsif options[:stderr]
86
+ $stderr.puts "rescue_each error: #{item.short_message}"
87
+ end
88
+
89
+ if e.class.name == 'IRB::Abort'
90
+ if errors.empty?
91
+ raise
92
+ else
93
+ raise ::IRB::Abort, e.message + "\n" + RescueEach::Error.new(errors).to_s
94
+ end
95
+ end
96
+
97
+ errors << item
98
+
91
99
  end
92
100
  end
101
+ raise RescueEach::Error, errors unless errors.empty?
102
+ return retval
103
+ end
104
+
105
+ def rescue_send(method, *args, &block)
106
+
107
+ args = args.dup
108
+ options = args.extract_options!
109
+ rescue_options = options.reject { |k,v| !RESCUE_EACH_OPTIONS.include? k }
110
+ options.except! *RESCUE_EACH_OPTIONS
111
+ args << options unless options.empty?
112
+
113
+ rescue_options[:method] = method
114
+ rescue_options[:args] = args
93
115
 
94
- errors << item
116
+ rescue_each rescue_options, &block
95
117
 
96
118
  end
119
+
97
120
  end
98
- raise RescueEach::Error, errors unless errors.empty?
99
- return retval
100
- end
101
-
102
- def rescue_send(method, *args, &block)
103
-
104
- args = args.dup
105
- options = args.extract_options!
106
- rescue_options = options.reject { |k,v| !RESCUE_EACH_OPTIONS.include? k }
107
- options.except! *RESCUE_EACH_OPTIONS
108
- args << options unless options.empty?
109
121
 
110
- rescue_options[:method] = method
111
- rescue_options[:args] = args
112
-
113
- rescue_each rescue_options, &block
122
+ module Enumerable
123
+ def self.included(klass)
124
+ klass.class_eval do
125
+
126
+ def rescue_map(*args, &block)
127
+ rescue_send :map, *args, &block
128
+ end
129
+
130
+ def rescue_each_with_index(*args, &block)
131
+ rescue_send :each_with_index, *args, &block
132
+ end
133
+
134
+ end
135
+ end
136
+ end
114
137
 
115
138
  end
116
139
 
117
- def rescue_map(*args, &block)
118
- rescue_send :map, *args, &block
119
- end
120
-
121
- def rescue_each_with_index(*args, &block)
122
- rescue_send :each_with_index, *args, &block
123
- end
124
-
125
- def rescue_find_each(*args, &block)
126
- rescue_send :find_each, *args, &block
127
- end
128
-
129
- def rescue_find_in_batches(*args, &block)
130
- rescue_send :find_in_batches, *args, &block
140
+ module ActiveRecord
141
+ def self.included(klass)
142
+ klass.class_eval do
143
+
144
+ def self.rescue_find_each(*args, &block)
145
+ rescue_send :find_each, *args, &block
146
+ end
147
+
148
+ def self.rescue_find_in_batches(*args, &block)
149
+ rescue_send :find_in_batches, *args, &block
150
+ end
151
+
152
+ end
153
+ end
131
154
  end
132
155
 
133
156
  end
157
+
158
+ Object.send(:include, RescueEach::CoreExt::Object)
159
+ Enumerable.send(:include, RescueEach::CoreExt::Enumerable)
160
+ ActiveRecord::Base.send(:include, RescueEach::ActiveRecord) if defined? ActiveRecord
@@ -154,6 +154,19 @@ class RescueEachTest < ActiveSupport::TestCase
154
154
  assert_match /foo bar/, err
155
155
  end
156
156
 
157
+ test "stderr output truncates long args" do
158
+
159
+ err = capture_stderr do
160
+ assert_raise RescueEach::Error do
161
+ ['foo bar '*1000].rescue_each(:stderr => true) { raise 'foo' }
162
+ end
163
+ end
164
+
165
+ assert_operator err.size, :>=, 100
166
+ assert_operator err.size, :<=, 1000
167
+
168
+ end
169
+
157
170
  test "rescue_send passes through args" do
158
171
  assert_true (1..5).rescue_send :include?, 3
159
172
  assert_false (1..5).rescue_send :include?, 6
@@ -175,4 +188,11 @@ class RescueEachTest < ActiveSupport::TestCase
175
188
  assert_equal [1,4,9,16,25], output
176
189
  end
177
190
 
191
+ test "find_each exists on active record objects" do
192
+ [:find_each, :find_in_batches].each do |method|
193
+ assert_true ActiveRecord::Base.methods.include? "#{method}"
194
+ assert_true ActiveRecord::Base.methods.include? "rescue_#{method}"
195
+ end
196
+ end
197
+
178
198
  end
data/test/test_helper.rb CHANGED
@@ -3,6 +3,7 @@ gem 'test-unit'
3
3
  require 'test/unit'
4
4
  require 'active_support'
5
5
  require 'active_support/test_case'
6
+ require 'active_record'
6
7
  require 'rescue_each'
7
8
 
8
9
  def capture_stderr
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.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Weathered
@@ -22,6 +22,16 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: "0"
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
25
35
  description:
26
36
  email: jason@jasoncodes.com
27
37
  executables: []