Module: Try

Defined in:
lib/try/eager.rb,
lib/try/eager/version.rb

Defined Under Namespace

Modules: Eager

Class Method Summary collapse

Class Method Details

.call(enumerator, params = {}, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/try/eager.rb', line 19

def self.call enumerator, params = {}, &block
  options = {
    until: ->(args) { true },
    interval: 0.1, # in sec
  }.update(params)
  enumerator.each do
    result = block.call
    break result if options[:until].call(result)
    sleep options[:interval]
  end
end

.eager(params = {}, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/try/eager.rb', line 4

def self.eager params = {}, &block
  options = {
    until: ->(args) { true },
    timeout: 10, # in sec
    interval: 0.1, # in sec
  }.update(params)
  timeout(options[:timeout]) do
    loop do
      result = block.call
      break result if options[:until].call(result)
      sleep options[:interval]
    end
  end
end