trick_bag 0.58.1 → 0.59

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: 3bc667fb430486e4cc7923ea12c615bc658cdd7e
4
- data.tar.gz: 4db0fde0dd906dda2a46ebacb975908ac18eb154
3
+ metadata.gz: 80de6b95f6465d84976cfff65217064adab423c6
4
+ data.tar.gz: 6a91c5d2a35c666f85de817f64d78587978ace2a
5
5
  SHA512:
6
- metadata.gz: f51ab665365c45131ac6f90932a58c2f33d604ea2ce13ad00cd0eac622b4816ece6e79d0ced9e73d487fed1ff71ec071a485e54f87bb890387b194a08d6d3d68
7
- data.tar.gz: 44d8d27beb484babc4be8a78f773129ac0d2c790a989208c23fd2ac233a0634cc79fc5fa055b19f5b7a25cbdac53f3fd1cdb5e3e9710dd9f49add4898b11bdc6
6
+ metadata.gz: df097a1226f5ba6a5bdcaaf2200050fd8450e7a3cafdcd0827b6766ac15a8bfc5ef4f724b525c6d191891e0a272818348cabbee4a3ae27ed05e018e1f44eb493
7
+ data.tar.gz: 6f25b512ee5485ee8a7e78c089764420f29519d4abcb6a1fd2fa6faa1c3cde512f7018b4f3cd26589a3e75da057a65be5a04bb2d5457e75e5bf50a065ed8bd10
data/RELEASE_NOTES.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## v0.59
2
+
3
+ * Timing.retry_until_true_or_timeout now optionally takes a code block instead of a lambda.
4
+ Also, method signature's parameter order has changed. This is a breaking change for users
5
+ of this method. A descriptive error has been provided to explain if this happens.
6
+
1
7
  ## v0.58.1
2
8
 
3
9
  * For KmgtNumericString, support returning of nil/false on nil input.
@@ -4,6 +4,7 @@ require 'benchmark'
4
4
  module TrickBag
5
5
  module Timing
6
6
 
7
+
7
8
  module_function
8
9
 
9
10
 
@@ -26,31 +27,45 @@ module Timing
26
27
  # print "Waiting 10 seconds for true to be returned (but it won't):\n"
27
28
  # TrickBag::Timing.retry_until_true_or_timeout(predicate, 1, 10)
28
29
  def retry_until_true_or_timeout(
29
- predicate, sleep_interval, timeout_secs, output_stream = $stdout)
30
+ sleep_interval, timeout_secs, output_stream = $stdout, predicate = nil)
31
+
32
+
33
+ # Method signature has changed from:
34
+ # (predicate, sleep_interval, timeout_secs, output_stream = $stdout)
35
+ # to:
36
+ # (sleep_interval, timeout_secs, output_stream = $stdout, predicate = nil)
37
+ #
38
+ # Test to see that when old signature is used, a descriptive error is raised.
39
+ #
40
+ # This test should be removed when we go to version 1.0.
41
+ if sleep_interval.respond_to?(:call)
42
+ raise ArgumentError.new('Sorry, method signature has changed to: ' \
43
+ '(sleep_interval, timeout_secs, output_stream = $stdout, predicate = nil).' \
44
+ ' Also a code block can now be provided instead of a lambda.')
45
+ end
46
+
47
+ if block_given? && predicate
48
+ raise ArgumentError.new('Both a predicate lambda and a code block were specified.' \
49
+ ' Please specify one or the other but not both.')
50
+ end
30
51
 
31
52
  success = false
32
- start_time = Time.now
33
- end_time = start_time + timeout_secs
34
- time_elapsed = nil
53
+ end_time = Time.now + timeout_secs
35
54
  time_to_go = nil
36
- text_generator = ->() { "%9.3f %9.3f" % [time_elapsed, time_to_go] }
55
+ text_generator = ->() { '%9.3f %9.3f' % [time_elapsed, time_to_go] }
37
56
  status_updater = ::TrickBag::Io::TextModeStatusUpdater.new(text_generator, output_stream)
38
57
 
39
58
  loop do
40
- now = Time.now
41
- time_elapsed = now - start_time
42
- time_to_go = end_time - now
43
- time_up = now >= end_time
44
59
 
45
- break if time_up
60
+ break if Time.now >= end_time
46
61
 
47
- success = !! predicate.()
62
+ success = !! (block_given? ? yield : predicate.())
48
63
  break if success
49
64
 
50
65
  status_updater.print
51
66
  sleep(sleep_interval)
52
67
  end
53
- print "\n"
68
+ output_stream.print "\n"
54
69
  success
55
70
  end
56
71
 
@@ -1,3 +1,3 @@
1
1
  module TrickBag
2
- VERSION = "0.58.1"
2
+ VERSION = "0.59"
3
3
  end
@@ -7,19 +7,50 @@ module TrickBag
7
7
  describe Timing do
8
8
 
9
9
  context '.retry_until_true_or_timeout' do
10
+
10
11
  it 'returns true when the predicate returns true' do
11
12
  count = 0
12
13
  predicate = ->{ count += 1; count == 3 }
13
- return_value = Timing.retry_until_true_or_timeout(predicate, 0, 1, StringIO.new)
14
+ return_value = Timing.retry_until_true_or_timeout(0, 1, StringIO.new, predicate)
14
15
  expect(return_value).to be(true)
15
16
  expect(count).to eq(3)
16
17
  end
17
18
 
19
+ it 'returns true when the code block returns true' do
20
+ expect(Timing.retry_until_true_or_timeout(0, 1, StringIO.new) { true }).to eq(true)
21
+ end
22
+
18
23
  it 'returns false when the predicate fails to return true and a timeout occurs' do
19
24
  predicate = -> { false }
20
- return_value = Timing.retry_until_true_or_timeout(predicate, 0, 0.1, StringIO.new)
25
+ return_value = Timing.retry_until_true_or_timeout(0, 0.1, StringIO.new, predicate)
21
26
  expect(return_value).to be(false)
22
27
  end
28
+
29
+ it 'returns false when the code block fails to return true and a timeout occurs' do
30
+ expect(Timing.retry_until_true_or_timeout(0, 0.1, StringIO.new) { false }).to eq(false)
31
+ end
32
+
33
+ # Method signature has changed from:
34
+ # (predicate, sleep_interval, timeout_secs, output_stream = $stdout)
35
+ # to:
36
+ # (sleep_interval, timeout_secs, output_stream = $stdout, predicate = nil)
37
+ #
38
+ # Test to see that when old signature is used, a descriptive error is raised.
39
+ #
40
+ # This test (and the error raising functionality it tests) should probably
41
+ # be removed once this gem goes to version 1.0.
42
+ it 'raises an ArgumentError when a predicate is passed as the first parameter' do
43
+ predicate = -> { false }
44
+ expect { Timing.retry_until_true_or_timeout(predicate, 0, 0.1, StringIO.new) } \
45
+ .to raise_error(ArgumentError)
46
+ end
47
+
48
+ it 'raises an ArgumentError when both a predicate lambda and code block are specified' do
49
+ predicate = -> { false }
50
+ expect do
51
+ Timing.retry_until_true_or_timeout(0, 0.1, StringIO.new, predicate) { true }
52
+ end.to raise_error(ArgumentError)
53
+ end
23
54
  end
24
55
 
25
56
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trick_bag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.58.1
4
+ version: '0.59'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Bennett
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-03 00:00:00.000000000 Z
11
+ date: 2015-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: os