try-until 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZTQ5NDkxMzE4ZDc2MGMzMjQ1OWYzM2NjNTE0NGI4MmUwMGE3M2ZlZA==
4
+ YmU5N2NkZTJlMjgxODcyZTZkZjM4YzM1MDQyMWY2NGE2M2YzNjZmZg==
5
5
  data.tar.gz: !binary |-
6
- ODZiMjJjYTllY2U4M2RjYjIyZDJhMjJkZWRlYWVhNjZiMzM2MTc4OA==
6
+ NjljNDU1NmFkZjgyNjFhNmVmZGFkNDFkNTUzZjdiYjcxYzJmYWEzNg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- MjEwNjg4MDQ5NWFkZWE5MTdmNmFlZjU1MWQ2ZWRlM2I0ODA3NDI2YzFmMjY1
10
- YWFiMDg5NTllMjNiY2QxMjYxZjNmODQ1ODA1MGVjNWU2Zjg4YjQxOTNhODBl
11
- OWFkNTk5MTIxZWZhMjk0OTA4NDJjN2ZjYzU0ZWJjY2QyNjcwNWQ=
9
+ ODgzMDIxMzY3ZWY3Mzc1OWI5MTdjYTQ3OWZlZjI0NGNjZmIzY2QzM2QwOWVm
10
+ ZWY5MDIyYTBiM2I3MWU4OTdiNTIyMDc3ZGQwY2E4OTczYjhiYWE0ZjExY2Rm
11
+ MzA2NzhkYmNiODlmMmVlNGNlNjhkY2Q4YWJhYjVhMTBiMjNhMTk=
12
12
  data.tar.gz: !binary |-
13
- MjI0MGExMjU4Yjc3ZjE1MzcyOWVkMjM5YWI4YjM3MjlhM2Y1ZDE3MzczYTNk
14
- OTAwZDA1ZmE1NjkwMmVkYzI3MWQxMmRjZDhkOTI3YmEzY2JkNjY1YWVlYzkz
15
- ODlkYjU4YmFlOThhYjM5MDFhMjlhMTdmNTgwYjhiNThkMWRmN2E=
13
+ YzgzZDBhNGM3MzBkMGFjYmVmNzM5NTg1MThhMzhmNjRhMDg4YjlmMmIwODIx
14
+ ZWM2ZmY2YTRjZGM1M2U2NDFkZjMyNzVlMzU0M2QzZTdiZDE1ZWZlZTQ0Yjk1
15
+ NWNkZWU3Zjg2NmIyZDJkYzdlMTFhN2VjOTM1NDdkZjVhMjY0OWM=
@@ -9,7 +9,7 @@ module TryUntil
9
9
  # .rescues([ ArgumentError, IOError ])
10
10
  # .stop_when(lambda { |response| JSON.parse(response.body)['id'] == 'some_id' })
11
11
  # .log_to($stdout)
12
- # .execute
12
+ # .execute
13
13
  #
14
14
  # Not all of the above settings are required. These are the default values:
15
15
  # attempts = 3
@@ -23,6 +23,7 @@ module TryUntil
23
23
 
24
24
  def initialize(probe)
25
25
  @probe = probe
26
+ defaults
26
27
  end
27
28
 
28
29
  def attempts(int_num)
@@ -60,19 +61,14 @@ module TryUntil
60
61
  # In case of errors it will rescue those and continue, provided the type
61
62
  # of error is among the ones defined in @rescues.
62
63
  def execute
63
- @attempts = 3 unless @attempts
64
- @interval = 0 unless @interval
65
- @delay = 0 unless @delay
66
- @rescues = [] unless @rescues
67
- @stop_when = lambda { |response| false } unless @stop_when
68
- @log_to = NullPrinter.new unless @log_to
69
-
70
64
  Kernel.sleep(@delay) if @delay > 0
71
65
  count = 0
66
+ condition_met = false
72
67
  while count < @attempts
73
68
  begin
74
69
  result = @probe.sample
75
70
  if @stop_when.call(result)
71
+ condition_met = true
76
72
  log_outcome(count, 'CONDITION_MET')
77
73
  return result
78
74
  end
@@ -81,8 +77,10 @@ module TryUntil
81
77
  log_outcome(count, exception.class)
82
78
  raise exception, "During final attempt (#{@attempts} configured) target returned #{exception}" if count + 1 == @attempts
83
79
  ensure
84
- count += 1
85
- Kernel.sleep @interval if @interval > 0
80
+ unless condition_met
81
+ count += 1
82
+ Kernel.sleep @interval if @interval > 0
83
+ end
86
84
  end
87
85
  end
88
86
  raise "After #{@attempts} attempts, the expected result was not returned!"
@@ -90,12 +88,21 @@ module TryUntil
90
88
 
91
89
  def configuration
92
90
  { :probe => @probe.to_s, :attempts => @attempts, :interval => @interval,
93
- :rescues => @rescues, :stop_when => @stop_when }
91
+ :delay => @delay, :rescues => @rescues, :log_to => @log_to }
94
92
  end
95
93
 
96
94
  private
97
95
  def log_outcome(count, outcome)
98
96
  @log_to.printf("#{Time.new}|attempt ##{count + 1}|outcome: #{outcome}|#{@attempts - count - 1} attempts left\n")
99
97
  end
98
+
99
+ def defaults
100
+ @attempts = 3 unless @attempts
101
+ @interval = 0 unless @interval
102
+ @delay = 0 unless @delay
103
+ @rescues = [] unless @rescues
104
+ @stop_when = lambda { |response| false } unless @stop_when
105
+ @log_to = NullPrinter.new unless @log_to
106
+ end
100
107
  end
101
108
  end
@@ -1,3 +1,3 @@
1
1
  module TryUntil
2
- VERSION = "0.4.0"
2
+ VERSION = '0.4.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: try-until
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Markus Krogemann
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-22 00:00:00.000000000 Z
11
+ date: 2013-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec