Class: ConvenientService::Examples::Standard::Factorial::Utils::Timeout::WithTimeout

Inherits:
Command
  • Object
show all
Defined in:
lib/convenient_service/examples/standard/factorial/utils/timeout/with_timeout.rb

Defined Under Namespace

Classes: ReturnStruct

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(seconds, &block) ⇒ WithTimeout

Returns a new instance of WithTimeout.



14
15
16
17
# File 'lib/convenient_service/examples/standard/factorial/utils/timeout/with_timeout.rb', line 14

def initialize(seconds, &block)
  @seconds = seconds
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



12
13
14
# File 'lib/convenient_service/examples/standard/factorial/utils/timeout/with_timeout.rb', line 12

def block
  @block
end

#secondsObject (readonly)

Returns the value of attribute seconds.



12
13
14
# File 'lib/convenient_service/examples/standard/factorial/utils/timeout/with_timeout.rb', line 12

def seconds
  @seconds
end

Instance Method Details

#callObject



28
29
30
31
32
33
34
35
36
37
# File 'lib/convenient_service/examples/standard/factorial/utils/timeout/with_timeout.rb', line 28

def call
  return struct(timeout?: false, value: block.call) if seconds.nil?
  return struct(timeout?: true, value: nil) if seconds.zero?

  begin
    struct(timeout?: false, value: ::Timeout.timeout(seconds, &block))
  rescue ::Timeout::Error
    struct(timeout?: true, value: nil)
  end
end