retry_unsafe_method 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +10 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +64 -0
- data/lib/retry_unsafe_method.rb +5 -0
- data/lib/retry_unsafe_method/retry_unsafe_method.rb +65 -0
- data/lib/retry_unsafe_method/version.rb +3 -0
- data/retry_unsafe_method.gemspec +27 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1378c34387ccd8cd8eed38c959a0fefcf5860168
|
4
|
+
data.tar.gz: 052ef0487a7ff2c333aae872fa7e0f682b9b239e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bbfe99896d058a1fc3fcb9c47aa3dfb7568b862dd8d3c8b449dba54d545d994a8d9ec6ff277422c0992f715874599d69fc1ddda0fe99d0bc7f56b625640726fe
|
7
|
+
data.tar.gz: cee5c1827a9b61cd97d5083d48f22df613194dbd06a780a21acda8b9d841d7bc411a6ab5f9bc3b1d6f84c7232c95986041f5f60461f674a0324459b04317c266
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Denis Talakevich
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# RetryUnsafeMethod
|
2
|
+
|
3
|
+
Allow easily make methods retriable
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'retry_unsafe_method'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install retry_unsafe_method
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
include module to your class
|
24
|
+
``
|
25
|
+
|
26
|
+
and add after method definition
|
27
|
+
|
28
|
+
`retry_unsafe_method <method_name>, <retries_qty>, *<exceptions>`
|
29
|
+
|
30
|
+
or
|
31
|
+
|
32
|
+
`retry_unsafe_method <method_name>, <retries_qty>, &<block>`
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'retry_unsafe_method'
|
36
|
+
|
37
|
+
class SomeError < StandardError
|
38
|
+
end
|
39
|
+
|
40
|
+
class A
|
41
|
+
include RetryUnsafeMethod::RetryUnsafeMethod
|
42
|
+
|
43
|
+
def some_method
|
44
|
+
# can raise SomeError
|
45
|
+
end
|
46
|
+
|
47
|
+
retry_unsafe_method :some_method, 2, SomeError
|
48
|
+
# or
|
49
|
+
retry_unsafe_method :some_method, 2 do |e|
|
50
|
+
e.is_a?(SomeError)
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/senid231/retry_unsafe_method.
|
59
|
+
|
60
|
+
|
61
|
+
## License
|
62
|
+
|
63
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
64
|
+
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module RetryUnsafeMethod
|
2
|
+
|
3
|
+
module RetryUnsafeMethod
|
4
|
+
def self.included(base)
|
5
|
+
base.extend(ClassMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
def retry_unsafe_method(method_name, retry_count, *exceptions, &block)
|
11
|
+
|
12
|
+
unless method_defined?(method_name) || private_method_defined?(method_name)
|
13
|
+
raise StandardError.new("method #{method_name} is not defined")
|
14
|
+
end
|
15
|
+
|
16
|
+
if block_given?
|
17
|
+
raise StandardError.new('you must not pass exceptions and block simultaneously') if exceptions.size > 0
|
18
|
+
check_proc = block
|
19
|
+
else
|
20
|
+
raise StandardError.new('you must pass exceptions or block') if exceptions.size == 0
|
21
|
+
check_proc = Proc.new { |e| exceptions.map(&:to_s).include?(e.class.to_s) }
|
22
|
+
end
|
23
|
+
|
24
|
+
safe_method_name = :"#{method_name}_safe_with_retry"
|
25
|
+
unsafe_method_name = :"#{method_name}_unsafe_without_retry"
|
26
|
+
|
27
|
+
define_method(safe_method_name) do |*args, &block|
|
28
|
+
begin
|
29
|
+
__send__(unsafe_method_name, *args, &block)
|
30
|
+
rescue StandardError => e
|
31
|
+
ivar = :"@current_retry_#{method_name}"
|
32
|
+
if check_proc.call(e)
|
33
|
+
instance_variable_set(ivar, 0) unless instance_variable_defined?(ivar)
|
34
|
+
|
35
|
+
if instance_variable_get(ivar) < retry_count
|
36
|
+
instance_variable_set(ivar, instance_variable_get(ivar) + 1)
|
37
|
+
retry
|
38
|
+
else
|
39
|
+
remove_instance_variable(ivar)
|
40
|
+
raise e
|
41
|
+
end
|
42
|
+
|
43
|
+
else
|
44
|
+
remove_instance_variable(ivar) if instance_variable_defined?(ivar)
|
45
|
+
raise e
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end # define_method
|
49
|
+
|
50
|
+
if private_method_defined?(method_name)
|
51
|
+
private safe_method_name
|
52
|
+
elsif protected_method_defined?(method_name)
|
53
|
+
protected safe_method_name
|
54
|
+
else
|
55
|
+
public safe_method_name
|
56
|
+
end
|
57
|
+
|
58
|
+
alias_method unsafe_method_name, method_name
|
59
|
+
alias_method method_name, safe_method_name
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'retry_unsafe_method/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'retry_unsafe_method'
|
8
|
+
spec.version = RetryUnsafeMethod::VERSION
|
9
|
+
spec.authors = ['Denis Talakevich']
|
10
|
+
spec.email = ['senid231@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Allow to retry unsafe methods.'
|
13
|
+
spec.description = 'Allow to retry unsafe methods.'
|
14
|
+
spec.homepage = 'https://github.com/senid231/retry_unsafe_method'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.8.9'
|
24
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
25
|
+
spec.add_development_dependency 'rspec'
|
26
|
+
spec.add_development_dependency 'activesupport', '~> 4.0'
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: retry_unsafe_method
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Denis Talakevich
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.8.9
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.8.9
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activesupport
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.0'
|
69
|
+
description: Allow to retry unsafe methods.
|
70
|
+
email:
|
71
|
+
- senid231@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".travis.yml"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- lib/retry_unsafe_method.rb
|
82
|
+
- lib/retry_unsafe_method/retry_unsafe_method.rb
|
83
|
+
- lib/retry_unsafe_method/version.rb
|
84
|
+
- retry_unsafe_method.gemspec
|
85
|
+
homepage: https://github.com/senid231/retry_unsafe_method
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.4.8
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Allow to retry unsafe methods.
|
109
|
+
test_files: []
|