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

Constructor Details

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

Returns a new instance of Calculate.



14
15
16
17
# File 'lib/convenient_service/examples/standard/v1/factorial/services/calculate.rb', line 14

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

Instance Attribute Details

#numberObject (readonly)

Returns the value of attribute number.



12
13
14
# File 'lib/convenient_service/examples/standard/v1/factorial/services/calculate.rb', line 12

def number
  @number
end

#timeout_secondsObject (readonly)

Returns the value of attribute timeout_seconds.



12
13
14
# File 'lib/convenient_service/examples/standard/v1/factorial/services/calculate.rb', line 12

def timeout_seconds
  @timeout_seconds
end

Instance Method Details

#resultObject



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

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