spruz 0.2.2 → 0.2.5
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/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/spruz/attempt.rb +31 -0
- data/lib/spruz/subhash.rb +4 -1
- data/lib/spruz/version.rb +1 -1
- data/lib/spruz/xt/attempt.rb +7 -0
- data/lib/spruz/xt.rb +1 -0
- data/lib/spruz.rb +1 -0
- data/tests/test_spruz.rb +31 -0
- metadata +19 -5
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.5
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Spruz
|
2
|
+
module Attempt
|
3
|
+
def attempt(attempts = 1, exception_class = StandardError, sleep_duration = nil, &block)
|
4
|
+
return if attempts <= 0
|
5
|
+
count = 0
|
6
|
+
if exception_class.nil?
|
7
|
+
begin
|
8
|
+
count += 1
|
9
|
+
if block.call(count)
|
10
|
+
return true
|
11
|
+
elsif sleep_duration and count < attempts
|
12
|
+
sleep sleep_duration
|
13
|
+
end
|
14
|
+
end until count == attempts
|
15
|
+
false
|
16
|
+
else
|
17
|
+
begin
|
18
|
+
count += 1
|
19
|
+
block.call(count)
|
20
|
+
true
|
21
|
+
rescue exception_class
|
22
|
+
if count < attempts
|
23
|
+
sleep_duration and sleep sleep_duration
|
24
|
+
retry
|
25
|
+
end
|
26
|
+
false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/spruz/subhash.rb
CHANGED
@@ -10,7 +10,10 @@ module Spruz
|
|
10
10
|
# MatchData instance if the matching pattern was a regular rexpression
|
11
11
|
# otherwise it is nil.
|
12
12
|
def subhash(*patterns)
|
13
|
-
patterns.map!
|
13
|
+
patterns.map! do |pat|
|
14
|
+
pat = pat.to_sym.to_s if pat.respond_to?(:to_sym)
|
15
|
+
pat.respond_to?(:match) ? pat : pat.to_s
|
16
|
+
end
|
14
17
|
result =
|
15
18
|
if default_proc
|
16
19
|
self.class.new(&default_proc)
|
data/lib/spruz/version.rb
CHANGED
data/lib/spruz/xt.rb
CHANGED
data/lib/spruz.rb
CHANGED
data/tests/test_spruz.rb
CHANGED
@@ -465,4 +465,35 @@ module Spruz
|
|
465
465
|
end
|
466
466
|
end
|
467
467
|
end
|
468
|
+
|
469
|
+
class TryTest < Test::Unit::TestCase
|
470
|
+
require 'spruz/xt/attempt'
|
471
|
+
|
472
|
+
def test_attempt_block_condition
|
473
|
+
assert attempt(1, nil) { |c| c == 1 }
|
474
|
+
assert attempt(3, nil) { |c| c == 1 }
|
475
|
+
assert_false attempt(3, nil) { |c| c == 4 }
|
476
|
+
assert_nil attempt(0, nil) { |c| c == 4 }
|
477
|
+
assert_raise(Exception) { attempt(3, nil) { raise Exception } }
|
478
|
+
end
|
479
|
+
|
480
|
+
class MyError < StandardError; end
|
481
|
+
class MyException < Exception; end
|
482
|
+
|
483
|
+
def test_attempt_default_exception
|
484
|
+
assert attempt(1) { |c| c != 1 and raise MyError }
|
485
|
+
assert attempt(3) { |c| c != 1 and raise MyError }
|
486
|
+
assert_false attempt(3) { |c| c != 4 and raise MyError }
|
487
|
+
assert_nil attempt(0) { |c| c != 4 and raise MyError }
|
488
|
+
assert_raise(Exception) { attempt(3) { raise Exception } }
|
489
|
+
end
|
490
|
+
|
491
|
+
def test_attempt_exception
|
492
|
+
assert attempt(1, MyException) { |c| c != 1 and raise MyException }
|
493
|
+
assert attempt(3, MyException) { |c| c != 1 and raise MyException }
|
494
|
+
assert_false attempt(3, MyException) { |c| c != 4 and raise MyException }
|
495
|
+
assert_nil attempt(0, MyException) { |c| c != 4 and raise MyException }
|
496
|
+
assert_raise(Exception) { attempt(3, MyException) { raise Exception } }
|
497
|
+
end
|
498
|
+
end
|
468
499
|
end
|
metadata
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spruz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
- 5
|
10
|
+
version: 0.2.5
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Florian Frank
|
@@ -9,7 +15,7 @@ autorequire:
|
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2011-02-08 00:00:00 +01:00
|
13
19
|
default_executable:
|
14
20
|
dependencies: []
|
15
21
|
|
@@ -34,6 +40,7 @@ files:
|
|
34
40
|
- lib/spruz/minimize.rb
|
35
41
|
- lib/spruz/subhash.rb
|
36
42
|
- lib/spruz/count_by.rb
|
43
|
+
- lib/spruz/attempt.rb
|
37
44
|
- lib/spruz/round.rb
|
38
45
|
- lib/spruz/partial_application.rb
|
39
46
|
- lib/spruz/uniq_by.rb
|
@@ -52,6 +59,7 @@ files:
|
|
52
59
|
- lib/spruz/xt/shuffle.rb
|
53
60
|
- lib/spruz/xt/subhash.rb
|
54
61
|
- lib/spruz/xt/count_by.rb
|
62
|
+
- lib/spruz/xt/attempt.rb
|
55
63
|
- lib/spruz/xt/round.rb
|
56
64
|
- lib/spruz/xt/partial_application.rb
|
57
65
|
- lib/spruz/xt/uniq_by.rb
|
@@ -83,21 +91,27 @@ rdoc_options:
|
|
83
91
|
require_paths:
|
84
92
|
- lib
|
85
93
|
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
86
95
|
requirements:
|
87
96
|
- - ">="
|
88
97
|
- !ruby/object:Gem::Version
|
98
|
+
hash: 3
|
99
|
+
segments:
|
100
|
+
- 0
|
89
101
|
version: "0"
|
90
|
-
version:
|
91
102
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
92
104
|
requirements:
|
93
105
|
- - ">="
|
94
106
|
- !ruby/object:Gem::Version
|
107
|
+
hash: 3
|
108
|
+
segments:
|
109
|
+
- 0
|
95
110
|
version: "0"
|
96
|
-
version:
|
97
111
|
requirements: []
|
98
112
|
|
99
113
|
rubyforge_project:
|
100
|
-
rubygems_version: 1.3.
|
114
|
+
rubygems_version: 1.3.7
|
101
115
|
signing_key:
|
102
116
|
specification_version: 3
|
103
117
|
summary: Useful stuff.
|