rrrretry 0.0.1

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/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :development, :test do
4
+ gem 'rake'
5
+ gem 'jeweler', '>=1.5.2'
6
+ end
7
+
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.8.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rdoc
10
+ json (1.7.6)
11
+ rake (10.0.3)
12
+ rdoc (3.12)
13
+ json (~> 1.4)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ jeweler (>= 1.5.2)
20
+ rake
@@ -0,0 +1,20 @@
1
+ Copyright 2011 Schneems
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,80 @@
1
+ # rrrretry
2
+
3
+ Because `retry` was already taken.
4
+
5
+ ## Install
6
+
7
+ In your `Gemfile` add:
8
+
9
+ gem 'rrrretry'
10
+
11
+ Then run `bundle install`.
12
+
13
+ ## What Does it Do?
14
+
15
+ Monkey patches Enumerable to add a method called `retry` that attempts to run code for every element, returns the first valid run.
16
+
17
+ If code does not return, the original error will be raised.
18
+
19
+ ## Whoa Crazy! Give me an example:
20
+
21
+ Most people will use it for network communication like this:
22
+
23
+ ```ruby
24
+ 10.times.retry do
25
+ SomeNetworkCommunicationThingyHere.new
26
+ end
27
+ ```
28
+
29
+ But if you prefer runnable examples check these out:
30
+
31
+ First run returns a `divided by 0` error, so the next result is returned.
32
+
33
+ ```ruby
34
+ [0, 1, 2].each.retry { |i| 1/i }
35
+ # => 1
36
+ ```
37
+
38
+ If there are no valid returns, the last error will be raised.
39
+
40
+ ```ruby
41
+ [0, 1, 2].each.retry { raise "bar" }
42
+ # => RuntimeError: bar
43
+ ```
44
+
45
+ By default only `StandardError` is caught, multiple error types can be
46
+ specified.
47
+
48
+ ```ruby
49
+ array = [->{ raise Exception }, ->{ raise SignalException }, ->{ 1 }]
50
+ array.each.retry(Exception, SignalException) { |i| i.call }
51
+ # => 1
52
+ ```
53
+
54
+ ## Is it Complex?
55
+
56
+ Naw, it's 8 lines of ruby:
57
+
58
+ ```ruby
59
+ def retry(*exceptions, &block)
60
+ exceptions << StandardError if exceptions.empty?
61
+ enum ||= self.to_enum
62
+ yield enum.next
63
+ rescue *exceptions => e
64
+ last_exception = e and retry unless e.is_a? StopIteration
65
+ raise last_exception unless last_exception.nil?
66
+ end
67
+ ```
68
+
69
+ ## Why?
70
+
71
+ Because I needed this code in more than one project, I didn't like the other solutions, and because coding is fun.
72
+
73
+ ## Isn't Monkey Patching Evil?
74
+
75
+ Yes. Do as I say, not as I do.
76
+
77
+ ## License
78
+
79
+ MIT YO. See MIT-LICENSE for more info
80
+
@@ -0,0 +1,38 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+
5
+ begin
6
+ Bundler.setup(:default, :development, :test)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+
13
+ require 'rake'
14
+ require 'rdoc/task'
15
+ require 'rake/testtask'
16
+
17
+ Rake::TestTask.new(:test) do |t|
18
+ t.libs << 'lib'
19
+ t.libs << 'test'
20
+ t.pattern = 'test/**/*_test.rb'
21
+ t.verbose = false
22
+ end
23
+
24
+
25
+
26
+ require 'jeweler'
27
+ Jeweler::Tasks.new do |gem|
28
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
29
+ gem.name = "rrrretry"
30
+ gem.homepage = "http://github.com/schneems/rrrretry"
31
+ gem.license = "MIT"
32
+ gem.summary = %Q{re-run your code}
33
+ gem.description = %Q{did i stutter?}
34
+ gem.email = "richard.schneeman@gmail.com"
35
+ gem.authors = ["schneems"]
36
+ # dependencies defined in Gemfile
37
+ end
38
+ Jeweler::RubygemsDotOrgTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,35 @@
1
+ # We're monkey patching Enumerable Directly
2
+
3
+ # See Enumerable
4
+ module Rrrretry
5
+ end
6
+
7
+ module Enumerable
8
+ # Attempts to run code for every element, returns the first valid run.
9
+ # If code does not return, the original error will be raised.
10
+ #
11
+ # First run returns a `divided by 0` error, so the next result is returned.
12
+ #
13
+ # [0, 1, 2].each.retry { |i| 1/i }
14
+ # # => 1
15
+ #
16
+ # If there are no valid returns, the last error will be raised.
17
+ #
18
+ # [0, 1, 2].each.retry { raise "bar" }
19
+ # # => RuntimeError: bar
20
+ #
21
+ # By default only `StandardError` is caught, multiple error types can be
22
+ # specified.
23
+ #
24
+ # array = [->{ raise Exception }, ->{ raise SignalException }, ->{ 1 }]
25
+ # array.each.retry(Exception, SignalException) { |i| i.call }
26
+ # # => 1
27
+ def retry(*exceptions, &block)
28
+ exceptions << StandardError if exceptions.empty?
29
+ enum ||= self.to_enum
30
+ yield enum.next
31
+ rescue *exceptions => e
32
+ last_exception = e and retry unless e.is_a? StopIteration
33
+ raise last_exception unless last_exception.nil?
34
+ end
35
+ end
@@ -0,0 +1,33 @@
1
+ require 'test_helper'
2
+
3
+ class Rrrrretry_test < Test::Unit::TestCase
4
+ Enumerator = [].each.class
5
+
6
+ class GenericEnumerable
7
+ include Enumerable
8
+ def initialize(values = [1, 2, 3])
9
+ @values = values
10
+ end
11
+
12
+ def each
13
+ @values.each{|v| yield v}
14
+ end
15
+ end
16
+
17
+ def test_retry
18
+ assert_equal 0.5, GenericEnumerable.new([ 2.0, 0 ]).retry { |i| 1/i }
19
+ assert_equal 1, GenericEnumerable.new([ 0, 1 ]).retry { |i| 1/i }
20
+ assert_raise(ZeroDivisionError) {GenericEnumerable.new([0]).retry { |i| 1/i } }
21
+ result = GenericEnumerable.new([ Exception, 1 ]).retry(Exception) do |i|
22
+ raise i if i == Exception
23
+ i
24
+ end
25
+ assert_equal 1, result
26
+ result = GenericEnumerable.new([ Exception, SecurityError, 1 ]).retry(Exception, SecurityError) do |i|
27
+ raise i if i == Exception || i == SecurityError
28
+ i
29
+ end
30
+ assert_equal 1, result
31
+ GenericEnumerable.new([]).retry {} # ensure no errors
32
+ end
33
+ end
@@ -0,0 +1,4 @@
1
+ require 'test/unit'
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+ require 'rrrretry'
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rrrretry
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - schneems
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: jeweler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.5.2
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.5.2
46
+ description: did i stutter?
47
+ email: richard.schneeman@gmail.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files:
51
+ - README.md
52
+ files:
53
+ - Gemfile
54
+ - Gemfile.lock
55
+ - MIT-LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - VERSION
59
+ - lib/rrrretry.rb
60
+ - test/rrrretry_test.rb
61
+ - test/test_helper.rb
62
+ homepage: http://github.com/schneems/rrrretry
63
+ licenses:
64
+ - MIT
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ segments:
76
+ - 0
77
+ hash: 3391354679544867714
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.24
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: re-run your code
90
+ test_files: []