spruz 0.2.5 → 0.2.6

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.5
1
+ 0.2.6
@@ -1,6 +1,15 @@
1
1
  module Spruz
2
2
  module Attempt
3
- def attempt(attempts = 1, exception_class = StandardError, sleep_duration = nil, &block)
3
+ def attempt(opts = {}, &block)
4
+ sleep = nil
5
+ exception_class = StandardError
6
+ if Numeric === opts
7
+ attempts = opts
8
+ else
9
+ attempts = opts[:attempts] || 1
10
+ exception_class = opts[:exception_class] if opts.key?(:exception_class)
11
+ sleep = opts[:sleep]
12
+ end
4
13
  return if attempts <= 0
5
14
  count = 0
6
15
  if exception_class.nil?
@@ -8,8 +17,8 @@ module Spruz
8
17
  count += 1
9
18
  if block.call(count)
10
19
  return true
11
- elsif sleep_duration and count < attempts
12
- sleep sleep_duration
20
+ elsif count < attempts
21
+ sleep_duration(sleep, count)
13
22
  end
14
23
  end until count == attempts
15
24
  false
@@ -20,12 +29,23 @@ module Spruz
20
29
  true
21
30
  rescue exception_class
22
31
  if count < attempts
23
- sleep_duration and sleep sleep_duration
32
+ sleep_duration(sleep, count)
24
33
  retry
25
34
  end
26
35
  false
27
36
  end
28
37
  end
29
38
  end
39
+
40
+ private
41
+
42
+ def sleep_duration(duration, count)
43
+ case duration
44
+ when Numeric
45
+ sleep duration
46
+ when Proc
47
+ sleep duration.call(count)
48
+ end
49
+ end
30
50
  end
31
51
  end
@@ -1,7 +1,12 @@
1
1
  module Spruz
2
2
  module HashUnion
3
3
  def |(other)
4
- other = other.to_hash if other.respond_to?(:to_hash)
4
+ case
5
+ when other.respond_to?(:to_hash)
6
+ other = other.to_hash
7
+ when other.respond_to?(:to_h)
8
+ other = other.to_h
9
+ end
5
10
  other.merge(self)
6
11
  end
7
12
  end
@@ -1,6 +1,6 @@
1
1
  module Spruz
2
2
  # Spruz version
3
- VERSION = '0.2.5'
3
+ VERSION = '0.2.6'
4
4
  VERSION_ARRAY = VERSION.split(/\./).map { |x| x.to_i } # :nodoc:
5
5
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
6
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
@@ -320,6 +320,18 @@ module Spruz
320
320
  class HashUnionTest < Test::Unit::TestCase
321
321
  require 'spruz/xt/hash_union'
322
322
 
323
+ class HashLike1
324
+ def to_hash
325
+ { 'foo' => true }
326
+ end
327
+ end
328
+
329
+ class HashLike2
330
+ def to_h
331
+ { 'foo' => true }
332
+ end
333
+ end
334
+
323
335
  def test_union
324
336
  defaults = { 'foo' => true, 'bar' => false, 'quux' => nil }
325
337
  hash = { 'foo' => false }
@@ -337,6 +349,11 @@ module Spruz
337
349
  ['quux', true] ],
338
350
  hash.sort
339
351
  end
352
+
353
+ def test_hash_conversion
354
+ assert_equal({ 'foo' => true }, { } | HashLike1.new)
355
+ assert_equal({ 'foo' => true }, { } | HashLike2.new)
356
+ end
340
357
  end
341
358
 
342
359
  class SubhashTest < Test::Unit::TestCase
@@ -470,11 +487,11 @@ module Spruz
470
487
  require 'spruz/xt/attempt'
471
488
 
472
489
  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 } }
490
+ assert attempt(:attempts => 1, :exception_class => nil) { |c| c == 1 }
491
+ assert attempt(:attempts => 3, :exception_class => nil) { |c| c == 1 }
492
+ assert_false attempt(:attempts => 3, :exception_class => nil) { |c| c == 4 }
493
+ assert_nil attempt(:attempts => 0, :exception_class => nil) { |c| c == 4 }
494
+ assert_raise(Exception) { attempt(:attempts => 3, :exception_class => nil) { raise Exception } }
478
495
  end
479
496
 
480
497
  class MyError < StandardError; end
@@ -489,11 +506,11 @@ module Spruz
489
506
  end
490
507
 
491
508
  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 } }
509
+ assert attempt(:attempts => 1, :exception_class => MyException) { |c| c != 1 and raise MyException }
510
+ assert attempt(:attempts => 3, :exception_class => MyException) { |c| c != 1 and raise MyException }
511
+ assert_false attempt(:attempts => 3, :exception_class => MyException) { |c| c != 4 and raise MyException }
512
+ assert_nil attempt(:attempts => 0, :exception_class => MyException) { |c| c != 4 and raise MyException }
513
+ assert_raise(Exception) { attempt(:attempts => 3, :exception_class => MyException) { raise Exception } }
497
514
  end
498
515
  end
499
516
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spruz
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease: false
4
+ hash: 27
5
+ prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 5
10
- version: 0.2.5
9
+ - 6
10
+ version: 0.2.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Florian Frank
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-08 00:00:00 +01:00
19
- default_executable:
18
+ date: 2011-04-29 00:00:00 Z
20
19
  dependencies: []
21
20
 
22
21
  description: All the stuff that isn't good/big enough for a real library.
@@ -28,57 +27,56 @@ extensions: []
28
27
  extra_rdoc_files:
29
28
  - README
30
29
  files:
31
- - bin/enum
32
- - VERSION
33
- - README
30
+ - tests/test_spruz_memoize.rb
31
+ - tests/test_spruz.rb
32
+ - LICENSE
34
33
  - Rakefile
35
- - lib/spruz/limited.rb
36
- - lib/spruz/null.rb
37
- - lib/spruz/time_dummy.rb
34
+ - lib/spruz.rb
35
+ - lib/spruz/hash_union.rb
36
+ - lib/spruz/write.rb
37
+ - lib/spruz/version.rb
38
+ - lib/spruz/round.rb
39
+ - lib/spruz/go.rb
40
+ - lib/spruz/generator.rb
38
41
  - lib/spruz/shuffle.rb
39
- - lib/spruz/to_proc.rb
40
- - lib/spruz/minimize.rb
42
+ - lib/spruz/deep_dup.rb
43
+ - lib/spruz/memoize.rb
41
44
  - lib/spruz/subhash.rb
42
- - lib/spruz/count_by.rb
43
- - lib/spruz/attempt.rb
44
- - lib/spruz/round.rb
45
- - lib/spruz/partial_application.rb
46
- - lib/spruz/uniq_by.rb
47
- - lib/spruz/p.rb
48
45
  - lib/spruz/bijection.rb
46
+ - lib/spruz/limited.rb
47
+ - lib/spruz/attempt.rb
48
+ - lib/spruz/to_proc.rb
49
49
  - lib/spruz/module_group.rb
50
- - lib/spruz/deep_dup.rb
51
- - lib/spruz/memoize.rb
52
- - lib/spruz/write.rb
53
- - lib/spruz/generator.rb
54
- - lib/spruz/hash_union.rb
55
- - lib/spruz/version.rb
56
- - lib/spruz/go.rb
57
- - lib/spruz/xt/null.rb
58
- - lib/spruz/xt/time_dummy.rb
50
+ - lib/spruz/null.rb
51
+ - lib/spruz/xt.rb
52
+ - lib/spruz/once.rb
53
+ - lib/spruz/xt/hash_union.rb
54
+ - lib/spruz/xt/write.rb
55
+ - lib/spruz/xt/irb.rb
56
+ - lib/spruz/xt/round.rb
59
57
  - lib/spruz/xt/shuffle.rb
58
+ - lib/spruz/xt/deep_dup.rb
59
+ - lib/spruz/xt/symbol_to_proc.rb
60
60
  - lib/spruz/xt/subhash.rb
61
- - lib/spruz/xt/count_by.rb
62
61
  - lib/spruz/xt/attempt.rb
63
- - lib/spruz/xt/round.rb
64
- - lib/spruz/xt/partial_application.rb
65
- - lib/spruz/xt/uniq_by.rb
62
+ - lib/spruz/xt/null.rb
66
63
  - lib/spruz/xt/p.rb
67
- - lib/spruz/xt/blank.rb
68
- - lib/spruz/xt/deep_dup.rb
69
- - lib/spruz/xt/symbol_to_proc.rb
70
- - lib/spruz/xt/write.rb
71
- - lib/spruz/xt/hash_union.rb
64
+ - lib/spruz/xt/partial_application.rb
65
+ - lib/spruz/xt/time_dummy.rb
72
66
  - lib/spruz/xt/full.rb
73
- - lib/spruz/xt/irb.rb
74
- - lib/spruz/once.rb
75
- - lib/spruz/xt.rb
76
- - lib/spruz.rb
77
- - tests/test_spruz.rb
78
- - tests/test_spruz_memoize.rb
67
+ - lib/spruz/xt/count_by.rb
68
+ - lib/spruz/xt/blank.rb
69
+ - lib/spruz/xt/uniq_by.rb
70
+ - lib/spruz/p.rb
71
+ - lib/spruz/minimize.rb
72
+ - lib/spruz/partial_application.rb
73
+ - lib/spruz/time_dummy.rb
74
+ - lib/spruz/count_by.rb
75
+ - lib/spruz/uniq_by.rb
76
+ - bin/enum
77
+ - README
78
+ - VERSION
79
79
  - install.rb
80
- - LICENSE
81
- has_rdoc: true
82
80
  homepage: http://flori.github.com/spruz
83
81
  licenses: []
84
82
 
@@ -111,10 +109,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
109
  requirements: []
112
110
 
113
111
  rubyforge_project:
114
- rubygems_version: 1.3.7
112
+ rubygems_version: 1.7.1
115
113
  signing_key:
116
114
  specification_version: 3
117
115
  summary: Useful stuff.
118
116
  test_files:
119
- - tests/test_spruz.rb
120
117
  - tests/test_spruz_memoize.rb
118
+ - tests/test_spruz.rb