Class: Maze::Wait

Inherits:
Object
  • Object
show all
Defined in:
lib/maze/wait.rb

Overview

Allows repeated attempts at something, until it is successful or the timeout is exceed

Instance Method Summary collapse

Constructor Details

#initialize(interval: 0.1, timeout:) ⇒ Wait



9
10
11
12
13
14
15
# File 'lib/maze/wait.rb', line 9

def initialize(interval: 0.1, timeout:)
  raise "Interval must be greater than zero, got '#{interval}'" unless interval > 0
  raise "Timeout (#{timeout}) must be greater than interval (#{interval})" unless timeout > interval

  @interval = interval
  @max_attempts = timeout / interval
end

Instance Method Details

#until(&block) ⇒ Object

Wait until the given block succeeds (returns a truthy value) or the timeout is exceeded



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/maze/wait.rb', line 21

def until(&block)
  success = false
  attempts = 0

  until success || attempts >= @max_attempts do
    attempts += 1
    success = block.call

    sleep @interval unless success
  end

  success
end