Module: HoldOn
- Defined in:
- lib/holdon.rb
Defined Under Namespace
Classes: Timeout
Class Method Summary collapse
- .breaker(options = {}) ⇒ Object
-
.delay_until(options = {}, &block) ⇒ Object
This method just introduces a delay into the code that can be exited prematurely.
-
.delay_while(options = {}, &block) ⇒ Object
This method just introduces a delay into the code that can be exited prematurely.
- .until(options = {}, &block) ⇒ Object
- .while(options = {}, &block) ⇒ Object
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( = {}) timeout = .fetch(:timeout, 30) interval = .fetch(:interval, 1) = [: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, 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( = {}, &block) begin self.until(, &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( = {}, &block) begin self.while(, &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( = {}, &block) self.breaker() 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( = {}, &block) self.breaker() do result = yield break result unless result end end |