method_decorators 0.9.5 → 0.9.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ed79ca8949f564d8260a8e2f3d6ca6c25a4c6087
4
- data.tar.gz: e300b088390c17c198cf7a22ba105dd547fde0ad
3
+ metadata.gz: 30c841da2ec60e5e17325036618cd80d5a625b72
4
+ data.tar.gz: cf294d186cba4cbbcf461fa55368bd5408501197
5
5
  SHA512:
6
- metadata.gz: cc6ef5fd815700115771f75782619fb7ea726dcc9ab60e155c68e1deeb6fa3e2aea6f8f831413541bd39c63a6a9676d9b60a2ea9fee4702594dfaaf4ce483264
7
- data.tar.gz: 75c088d37f52ea4c695f5da57b71bd8674912b8e615fab19f3089639716b11fadb6da5dca66a8e162f096f3ecd181ed72a5163ec40ad3f923f2a1d73102c9395
6
+ metadata.gz: 1a0d710846e84f0e1ff3e661b38325decb508c7d63b6aad5b58b29d57b079e739a57bbab2c7483396345e66050e42d363301edb4e929c0e11f61f0f662d35ef4
7
+ data.tar.gz: 6c0f700a8a243fdf2002a9c80d10c1bec4fa73bb52db799df58c03bc84296d9a23511bf898c7c3ffbfa4d806c3f6ce41d73eee42ea0a6a234e7d5a523b135c30
@@ -6,7 +6,6 @@ rvm:
6
6
  - 2.0.0
7
7
  - jruby-18mode
8
8
  - jruby-19mode
9
- - rbx-2.1.1
10
9
  - ruby-head
11
10
  - jruby-head
12
- - ree
11
+ - ree
@@ -5,6 +5,7 @@ module MethodDecorators
5
5
  def initialize(max, options = {})
6
6
  @max = max
7
7
  @options = options
8
+ @exceptions = options[:exceptions] || [StandardError]
8
9
  end
9
10
 
10
11
  def call(orig, this, *args, &blk)
@@ -12,7 +13,7 @@ module MethodDecorators
12
13
  begin
13
14
  attempts += 1
14
15
  orig.call(*args, &blk)
15
- rescue
16
+ rescue *@exceptions
16
17
  if attempts < @max
17
18
  sleep(@options[:sleep]) if @options[:sleep]
18
19
  retry
@@ -1,3 +1,3 @@
1
1
  module MethodDecorators
2
- VERSION = "0.9.5"
2
+ VERSION = "0.9.6"
3
3
  end
@@ -3,12 +3,13 @@ require 'timeout'
3
3
 
4
4
  module MethodDecorators
5
5
  class Within < Decorator
6
- def initialize(timeout)
6
+ def initialize(timeout, exception_class = nil)
7
7
  @seconds = timeout
8
+ @exception_class = exception_class
8
9
  end
9
10
 
10
11
  def call(orig, this, *args, &blk)
11
- Timeout::timeout(@seconds) do
12
+ Timeout.timeout(@seconds, @exception_class) do
12
13
  orig.call(*args, &blk)
13
14
  end
14
15
  end
@@ -28,7 +28,7 @@ describe MethodDecorators::Precondition do
28
28
 
29
29
  +MethodDecorators::Precondition.new{ |a| a + @x < 10 }
30
30
  def multiply(a)
31
- a * @x
31
+ a * @x
32
32
  end
33
33
 
34
34
  +MethodDecorators::Precondition.new{ |a| a + @x == 10 }
@@ -27,24 +27,65 @@ describe MethodDecorators::Retry do
27
27
  expect { subject.call(method, nil) }.to raise_error(ArgumentError)
28
28
  end
29
29
  end
30
+
31
+ context 'when :exceptions is given to #initialize' do
32
+ subject do
33
+ MethodDecorators::Retry.new(3, :exceptions => [ArgumentError])
34
+ end
35
+
36
+ context 'and the raised exception matches' do
37
+ before do
38
+ allow(method).to receive(:call) { raise ArgumentError }
39
+ end
40
+
41
+ it 'retries' do
42
+ expect(method).to receive(:call).exactly(3).times
43
+ expect { subject.call(method, nil) }.to raise_error(ArgumentError)
44
+ end
45
+ end
46
+
47
+ context 'and the raised exception does not match' do
48
+ before do
49
+ allow(method).to receive(:call) { raise IndexError }
50
+ end
51
+
52
+ it 'does not retry' do
53
+ expect(method).to receive(:call).once
54
+ expect { subject.call(method, nil) }.to raise_error(IndexError)
55
+ end
56
+ end
57
+ end
30
58
  end
31
59
 
32
60
  describe "acceptance" do
33
- let(:klass) do
34
- Class.new Base do
35
- +MethodDecorators::Retry.new(3)
61
+ def create_class(options = {})
62
+ Class.new(Base) do
63
+ attr_reader :times
64
+
65
+ +MethodDecorators::Retry.new(3, options)
36
66
  def do_it(magic_number)
37
67
  @times ||= 0
38
68
  @times += 1
39
- raise if @times == magic_number
69
+ raise if @times == magic_number
40
70
  @times
41
71
  end
42
72
  end
43
73
  end
74
+
75
+ let(:klass) { create_class }
44
76
  subject { klass.new }
45
77
 
46
78
  it 'retries calls to the method' do
47
79
  subject.do_it(1).should == 2
48
80
  end
81
+
82
+ context 'when :exceptions is given to #initialize' do
83
+ let(:klass) { create_class(:exceptions => [ArgumentError]) }
84
+
85
+ it 'does not retry if the raised exception does not match' do
86
+ expect { subject.do_it(1) }.to raise_error
87
+ expect(subject.times).to eq(1)
88
+ end
89
+ end
49
90
  end
50
91
  end
@@ -15,6 +15,15 @@ describe MethodDecorators::Within do
15
15
  method.stub(:call){ sleep 1 }
16
16
  expect{ subject.call(method, nil) }.to_not raise_error
17
17
  end
18
+
19
+ context 'when an exception class is given in #initialize' do
20
+ subject { MethodDecorators::Within.new(1, ArgumentError) }
21
+
22
+ it 'raises the given exception if timeout seconds have elapsed' do
23
+ allow(method).to receive(:call) { sleep 2 }
24
+ expect { subject.call(method, nil) }.to raise_error(ArgumentError)
25
+ end
26
+ end
18
27
  end
19
28
 
20
29
  describe "acceptance" do
@@ -32,6 +41,21 @@ describe MethodDecorators::Within do
32
41
  it "raises if the timeout period has elapsed" do
33
42
  expect{ subject.do_it(3) }.to raise_error(Timeout::Error)
34
43
  end
44
+
45
+ context 'and given an exception' do
46
+ let(:klass) do
47
+ Class.new(Base) do
48
+ +MethodDecorators::Within.new(1, ArgumentError)
49
+ def do_it(execution_period)
50
+ sleep(execution_period)
51
+ end
52
+ end
53
+ end
54
+
55
+ it 'raises the given exception' do
56
+ expect { subject.do_it(2) }.to raise_error(ArgumentError)
57
+ end
58
+ end
35
59
  end
36
60
 
37
61
  context "with shorter execution period" do
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: method_decorators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.9.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Fairley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-13 00:00:00.000000000 Z
11
+ date: 2015-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Python's function decorators for Ruby
@@ -45,9 +45,9 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
- - .gitignore
49
- - .rspec
50
- - .travis.yml
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".travis.yml"
51
51
  - Contributors.md
52
52
  - Gemfile
53
53
  - History.md
@@ -87,17 +87,17 @@ require_paths:
87
87
  - lib
88
88
  required_ruby_version: !ruby/object:Gem::Requirement
89
89
  requirements:
90
- - - '>='
90
+ - - ">="
91
91
  - !ruby/object:Gem::Version
92
92
  version: '0'
93
93
  required_rubygems_version: !ruby/object:Gem::Requirement
94
94
  requirements:
95
- - - '>='
95
+ - - ">="
96
96
  - !ruby/object:Gem::Version
97
97
  version: '0'
98
98
  requirements: []
99
99
  rubyforge_project:
100
- rubygems_version: 2.0.3
100
+ rubygems_version: 2.2.2
101
101
  signing_key:
102
102
  specification_version: 4
103
103
  summary: Python's function decorators for Ruby