Class: ConvenientService::Examples::Standard::V1::Factorial::Services::Calculate

Inherits:
Object
  • Object
show all
Includes:
Service::Configs::Standard::V1
Defined in:
lib/convenient_service/examples/standard/v1/factorial/services/calculate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Config

included

Constructor Details

#initialize(number:, timeout_seconds: 10) ⇒ Calculate

Returns a new instance of Calculate.



19
20
21
22
# File 'lib/convenient_service/examples/standard/v1/factorial/services/calculate.rb', line 19

def initialize(number:, timeout_seconds: 10)
  @number = number
  @timeout_seconds = timeout_seconds
end

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



17
18
19
# File 'lib/convenient_service/examples/standard/v1/factorial/services/calculate.rb', line 17

def number
  @number
end

#timeout_secondsObject (readonly)

Returns the value of attribute timeout_seconds.



17
18
19
# File 'lib/convenient_service/examples/standard/v1/factorial/services/calculate.rb', line 17

def timeout_seconds
  @timeout_seconds
end

Instance Method Details

#resultObject



24
25
26
27
28
29
30
31
32
# File 'lib/convenient_service/examples/standard/v1/factorial/services/calculate.rb', line 24

def result
  return failure(number: "is `nil`") if number.nil?
  return failure(number: "is NOT an integer") unless number.instance_of?(::Integer)
  return failure(number: "is lower than `0`") if number < 0

  return error("Timeout (`#{timeout_seconds}` seconds) is exceeded for `#{number}`") if factorial.timeout?

  success(factorial: factorial.value)
end