try-until 0.3.0 → 0.4.0

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OTgyNWMzYmMwZjI0MWQ1OGJkZjRlYjJmNjVhMmM4NjViZjNmZTI3Mw==
4
+ ZTQ5NDkxMzE4ZDc2MGMzMjQ1OWYzM2NjNTE0NGI4MmUwMGE3M2ZlZA==
5
5
  data.tar.gz: !binary |-
6
- YzliYTUyOGIwOTExNmUwNjZiZGVlMTMwNmRmZjY1NDc0Y2Y5ZGMxMQ==
6
+ ODZiMjJjYTllY2U4M2RjYjIyZDJhMjJkZWRlYWVhNjZiMzM2MTc4OA==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZjRkOWU5NWI5M2I3OWI4OTJlMzNlNDc3ZTM3Y2I4ZmExNDNkNjU4ZGYyN2U0
10
- MjUxNzY0NDhiOWY1YmQxZGEyMTQ4MDFkODFkODI5ZGUwZmRmZDk5YzE2NTlh
11
- ZjcyNjZlZGQ0NTRiYzJjNzQ5YTlmZmEzODQ0YmNiYjZhMGNmNmU=
9
+ MjEwNjg4MDQ5NWFkZWE5MTdmNmFlZjU1MWQ2ZWRlM2I0ODA3NDI2YzFmMjY1
10
+ YWFiMDg5NTllMjNiY2QxMjYxZjNmODQ1ODA1MGVjNWU2Zjg4YjQxOTNhODBl
11
+ OWFkNTk5MTIxZWZhMjk0OTA4NDJjN2ZjYzU0ZWJjY2QyNjcwNWQ=
12
12
  data.tar.gz: !binary |-
13
- ODY3MDVhMzAxNjlkZWRhMDE2NGFlNWE0ZDJhNDk4YzlmZjYxMWNkNTdmNDNi
14
- MjgxZGI2NzU2ZjAwY2VlZTJmZTQ2Yzk4NDMxNTk0Y2YxYjRlMjdlMmYzODQ4
15
- YWQ3MTRjNzUyZjIyMmRiYTgxOWY1ZWFjZGYyMGIwN2Y2ZWRiNGM=
13
+ MjI0MGExMjU4Yjc3ZjE1MzcyOWVkMjM5YWI4YjM3MjlhM2Y1ZDE3MzczYTNk
14
+ OTAwZDA1ZmE1NjkwMmVkYzI3MWQxMmRjZDhkOTI3YmEzY2JkNjY1YWVlYzkz
15
+ ODlkYjU4YmFlOThhYjM5MDFhMjlhMTdmNTgwYjhiNThkMWRmN2E=
@@ -0,0 +1,7 @@
1
+ module TryUntil
2
+ # Null-Object: Will be used unless user specifies another target for
3
+ # informational messages printed while Repeatedly does its work
4
+ class NullPrinter
5
+ def printf(*args); end
6
+ end
7
+ end
@@ -11,12 +11,21 @@ module TryUntil
11
11
  end
12
12
 
13
13
  def sample
14
- @target.send(@method, *@args)
14
+ if args_is_one_hash?
15
+ @target.send(@method, @args)
16
+ else
17
+ @target.send(@method, *@args)
18
+ end
15
19
  end
16
20
 
17
21
  def to_s
18
22
  "Probe: #{@target.class}##{@method}(#{@args})"
19
23
  end
24
+
25
+ private
26
+ def args_is_one_hash?
27
+ @args.class == Hash
28
+ end
20
29
  end
21
30
  end
22
31
 
@@ -5,15 +5,19 @@ module TryUntil
5
5
  # result = Repeatedly.new(Probe.new(Object.new, :to_s))
6
6
  # .attempts(5)
7
7
  # .interval(10)
8
+ # .delay(120)
8
9
  # .rescues([ ArgumentError, IOError ])
9
10
  # .stop_when(lambda { |response| JSON.parse(response.body)['id'] == 'some_id' })
11
+ # .log_to($stdout)
10
12
  # .execute
11
13
  #
12
14
  # Not all of the above settings are required. These are the default values:
13
- # attempt = 3
15
+ # attempts = 3
14
16
  # interval = 0
17
+ # delay = 0
15
18
  # rescues = []
16
19
  # stop_when = lambda { |response| false }
20
+ # log_to = TryUntil::NullPrinter.new
17
21
  #
18
22
  class Repeatedly
19
23
 
@@ -31,6 +35,11 @@ module TryUntil
31
35
  self
32
36
  end
33
37
 
38
+ def delay(seconds)
39
+ @delay = seconds
40
+ self
41
+ end
42
+
34
43
  def rescues(errors)
35
44
  @rescues = errors
36
45
  self
@@ -41,6 +50,11 @@ module TryUntil
41
50
  self
42
51
  end
43
52
 
53
+ def log_to(io)
54
+ @log_to = io
55
+ self
56
+ end
57
+
44
58
  # The heart of this gem: This method will repeatedly call '#sample' on the
45
59
  # subject and evaluate if the expectated result is returned.
46
60
  # In case of errors it will rescue those and continue, provided the type
@@ -48,14 +62,23 @@ module TryUntil
48
62
  def execute
49
63
  @attempts = 3 unless @attempts
50
64
  @interval = 0 unless @interval
51
- @stop_when = lambda { |response| false } unless @stop_when
65
+ @delay = 0 unless @delay
52
66
  @rescues = [] unless @rescues
67
+ @stop_when = lambda { |response| false } unless @stop_when
68
+ @log_to = NullPrinter.new unless @log_to
69
+
70
+ Kernel.sleep(@delay) if @delay > 0
53
71
  count = 0
54
72
  while count < @attempts
55
73
  begin
56
74
  result = @probe.sample
57
- return result if @stop_when.call(result)
75
+ if @stop_when.call(result)
76
+ log_outcome(count, 'CONDITION_MET')
77
+ return result
78
+ end
79
+ log_outcome(count, 'CONDITION_NOT_MET')
58
80
  rescue *@rescues => exception
81
+ log_outcome(count, exception.class)
59
82
  raise exception, "During final attempt (#{@attempts} configured) target returned #{exception}" if count + 1 == @attempts
60
83
  ensure
61
84
  count += 1
@@ -69,5 +92,10 @@ module TryUntil
69
92
  { :probe => @probe.to_s, :attempts => @attempts, :interval => @interval,
70
93
  :rescues => @rescues, :stop_when => @stop_when }
71
94
  end
95
+
96
+ private
97
+ def log_outcome(count, outcome)
98
+ @log_to.printf("#{Time.new}|attempt ##{count + 1}|outcome: #{outcome}|#{@attempts - count - 1} attempts left\n")
99
+ end
72
100
  end
73
101
  end
@@ -1,3 +1,3 @@
1
1
  module TryUntil
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/lib/try_until.rb CHANGED
@@ -1,4 +1,3 @@
1
1
  require 'try_until/probe'
2
2
  require 'try_until/repeatedly'
3
-
4
-
3
+ require 'try_until/null_printer'
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.3.0
4
+ version: 0.4.0
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-10 00:00:00.000000000 Z
11
+ date: 2013-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -62,6 +62,7 @@ extensions: []
62
62
  extra_rdoc_files: []
63
63
  files:
64
64
  - lib/try-until.rb
65
+ - lib/try_until/null_printer.rb
65
66
  - lib/try_until/probe.rb
66
67
  - lib/try_until/repeatedly.rb
67
68
  - lib/try_until/version.rb