lib/convenient_service/examples/standard/factorial/services/calculate.rb in convenient_service-0.13.0 vs lib/convenient_service/examples/standard/factorial/services/calculate.rb in convenient_service-0.14.0
- old
+ new
@@ -1,25 +1,31 @@
# frozen_string_literal: true
+##
+# Usage example:
+#
+# result = ConvenientService::Examples::Standard::Factorial::Services::Calculate.result(number: 10)
+#
module ConvenientService
module Examples
module Standard
class Factorial
module Services
class Calculate
include ConvenientService::Standard::Config
+ include ConvenientService::AwesomePrintInspect::Config
attr_reader :number, :timeout_seconds
def initialize(number:, timeout_seconds: 10)
@number = number
@timeout_seconds = timeout_seconds
end
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("is `nil`") if number.nil?
+ return error("is NOT an integer") unless number.instance_of?(::Integer)
+ return error("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