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

Inherits:
Command
  • Object
show all
Defined in:
lib/convenient_service/examples/standard/v1/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.



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

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

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



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

def block
  @block
end

#secondsObject (readonly)

Returns the value of attribute seconds.



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

def seconds
  @seconds
end

Instance Method Details

#callObject



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

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