Class: ConvenientService::Examples::Standard::Factorial::Utils::Timeout::WithTimeout
- Inherits:
-
Command
- Object
- ConvenientService::Examples::Standard::Factorial::Utils::Timeout::WithTimeout
- Defined in:
- lib/convenient_service/examples/standard/factorial/utils/timeout/with_timeout.rb
Defined Under Namespace
Classes: ReturnStruct
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#seconds ⇒ Object
readonly
Returns the value of attribute seconds.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(seconds, &block) ⇒ WithTimeout
constructor
A new instance of WithTimeout.
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
#block ⇒ Object (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 |
#seconds ⇒ Object (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
#call ⇒ Object
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 |