Module: HoldOn

Defined in:
lib/holdon.rb

Defined Under Namespace

Classes: Timeout

Class Method Summary collapse

Class Method Details

.breaker(options = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/holdon.rb', line 52

def self.breaker(options = {})
  timeout  = options.fetch(:timeout, 30)
  interval = options.fetch(:interval, 1)
  message  = options[:message]

  start = Time.now
  loop do
    yield

    # If the time remaining is less than the interval,
    # then we'll only sleep for the time remaining.
    inv = [interval, (timeout - (Time.now - start))].min
    if inv > 0
      sleep inv
    else
      raise Timeout, message
    end

  end
end

.delay_until(options = {}, &block) ⇒ Object

This method just introduces a delay into the code that can be exited prematurely. Useful when you are waiting for something that might happen.



27
28
29
30
31
32
33
34
35
# File 'lib/holdon.rb', line 27

def self.delay_until(options = {}, &block)
  begin
    self.until(options, &block)
  rescue HoldOn::Timeout
    return false
  end

  return true
end

.delay_while(options = {}, &block) ⇒ Object

This method just introduces a delay into the code that can be exited prematurely. Useful when you are waiting for something that might happen.



41
42
43
44
45
46
47
48
49
# File 'lib/holdon.rb', line 41

def self.delay_while(options = {}, &block)
  begin
    self.while(options, &block)
  rescue HoldOn::Timeout
    return false
  end

  return true
end

.until(options = {}, &block) ⇒ Object



8
9
10
11
12
13
# File 'lib/holdon.rb', line 8

def self.until(options = {}, &block)
  self.breaker(options) do
    result = yield
    break result if result
  end
end

.while(options = {}, &block) ⇒ Object



16
17
18
19
20
21
# File 'lib/holdon.rb', line 16

def self.while(options = {}, &block)
  self.breaker(options) do
    result = yield
    break result unless result
  end
end