Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Document ServiceResult with YARD

Alexander Brandon Coles2b704315762d
diff --git a/app/services/service_result.rb b/app/services/service_result.rb
index c3ce3f46e4c..b1ca981a5ce 100644
--- a/app/services/service_result.rb
+++ b/app/services/service_result.rb
@@ -28,28 +28,75 @@
# See COPYRIGHT and LICENSE files for more details.
#++
+##
+# Represents the result of a call to a service.
+#
+# @example
+# result = Projects::UpdateService
+# .new(user: current_user, model: @project)
+# .call(permitted_params.project)
+# result.success? # => true if the service call was successful.
+# result.result # => #<Project id: 1011>
+# result.errors # => #<ActiveModel::Errors []>
+#
class ServiceResult
SUCCESS = true
FAILURE = false
+ private_constant :SUCCESS, :FAILURE
+
+ # @return [Boolean] whether the service call was successful.
+ attr_accessor :success
+
+ ##
+ # Returns the result of the service call. In typical usage this will be a
+ # model, i.e. an {ActiveRecord::Base} descendant.
+ #
+ # @return [Object, nil] the service call result object. This can also be nil.
+ attr_accessor :result
- attr_accessor :success,
- :result,
- :errors,
- :dependent_results
+ # @return [ActiveModel::Errors] errors resulting from the service call.
+ attr_accessor :errors
+
+ # @return [Array<ServiceResult>] all dependent ServiceResults - by virtue of
+ # the service calling other services.
+ attr_accessor :dependent_results
attr_writer :message,
:state
# Creates a successful ServiceResult.
+ #
+ # @param (see #initialize)
+ # @return (see #initialize)
def self.success(**)
new(**, success: SUCCESS)
end
+ ##
# Creates a failed ServiceResult.
+ #
+ # @param (see #initialize)
+ # @return (see #initialize)
def self.failure(**)
new(**, success: FAILURE)
end
+ ##
+ # @note
+ # Prefer using {.success} or {.failure} factory methods to calling
+ # `ServiceResult.new(success: true)` or `ServiceResult.new(success: false)`.
+ #
+ # @param errors [ActiveModel::Errors, nil] errors resulting from the service
+ # call.
+ # @param message [String, nil] an error message associated with the service
+ # call.
+ # @param message_type [#to_sym, nil] the type of error message when displayed
+ # as a Controller flash message.
+ # @param state [Shared::ServiceState, nil] the Service State object.
+ # @param dependent_results [Array<ServiceResult>] any dependent
+ # ServiceResults.
+ # @param result [Object, nil] the result of the service call.
+ # @return [ServiceResult] a new, successful instance of ServiceResult.
def initialize(success: FAILURE,
errors: nil,
message: nil,
@@ -68,15 +115,24 @@ class ServiceResult
self.dependent_results = dependent_results
end
+ # @see failure?
+ # @return [Boolean] whether the service call succeeded.
alias success? success
+ # @see success?
+ # @return [Boolean] whether the service call failed.
def failure?
!success?
end
##
- # Merge another service result into this instance
- # allowing optionally to skip updating its service
+ # Merges another ServiceResult into this instance, optionally allowing its
+ # {#success} to be ignored.
+ #
+ # @param other [ServiceResult] the other ServiceResult.
+ # @param without_success [Boolean] whether to ignore the {#success} of the
+ # other ServiceResult.
+ # @return [void]
def merge!(other, without_success: false)
merge_success!(other) unless without_success
merge_errors!(other)
@@ -84,26 +140,41 @@ class ServiceResult
end
##
- # Print messages to flash
+ # Prints messages to the Controller's flash.
+ #
+ # @param flash [ActionDispatch::Flash::FlashHash]
+ # @return [void]
def apply_flash_message!(flash)
if message
flash[message_type] = message
end
end
+ ##
+ # Returns all {#result}s, including from dependent ServiceResults.
+ #
+ # @return [Array<Object, nil>] all results.
def all_results
dependent_results.map(&:result).tap do |results|
results.unshift result unless result.nil?
end
end
+ ##
+ # Returns all {#errors}, including from dependent ServiceResults.
+ #
+ # @return [Array<ActiveModel::Errors>] all errors.
def all_errors
[errors] + dependent_results.map(&:errors)
end
##
- # Test whether the returned errors respond
- # to the search key
+ # Tests whether the returned errors, including from dependent ServiceResults,
+ # include the error key.
+ #
+ # @param attribute [:base, Symbol] the attribute.
+ # @param error_key [Symbol] the type of the error.
+ # @return [Boolean] whether the returned errors include the error key.
def includes_error?(attribute, error_key)
all_errors.any? do |error|
error.symbols_for(attribute).include?(error_key)
@@ -111,10 +182,11 @@ class ServiceResult
end
##
- # Collect all present errors for the given result
- # and dependent results.
+ # Returns dependent ServiceResults with errors, and optionally self, if self
+ # has errors.
#
- # Returns a map of the service result to the error object
+ # @param include_self [Boolean] whether to include self, if self has errors.
+ # @return [Array<ServiceResult>] all ServiceResults with errors.
def results_with_errors(include_self: true)
results =
if include_self
@@ -126,10 +198,15 @@ class ServiceResult
results.reject { |call| call.errors.empty? }
end
+ # @return [Array<ServiceResult>] self and dependent ServiceResults.
def self_and_dependent
[self] + dependent_results
end
+ ##
+ # Adds a dependent ServiceResult.
+ #
+ # @param [ServiceResult] dependent the dependent ServiceResult to add.
def add_dependent!(dependent)
merge_success!(dependent)
@@ -140,21 +217,47 @@ class ServiceResult
self.dependent_results += inner_results
end
+ ##
+ # Executes block argument if the service call succeeded.
+ #
+ # @yield block to be called on success.
+ # @return [self]
def on_success(&)
tap(&) if success?
self
end
+ ##
+ # Executes block argument if the service call failed.
+ #
+ # @yield block to be called on failure.
+ # @return [self]
def on_failure(&)
tap(&) if failure?
self
end
+ ##
+ # Iterates exactly once, passing the result to the block, if the service call
+ # succeeded.
+ #
+ # @see Enumerable#each
+ # @yield block to be called on success.
+ # @yieldparam result [Object, nil] the result of the service call.
+ # @return [self]
def each
yield result if success?
self
end
+ ##
+ # If the service call succeeded, returns a copy of the ServiceResult whose
+ # whose {#result} is the return value from the block. Iterates exactly once
+ # if the service call succeeded.
+ #
+ # @yield block to be called on success.
+ # @yieldparam result [Object, nil] the result of the service call.
+ # @return [ServiceResult] a new ServiceResult with the result.
def map
return self if failure?
@@ -163,6 +266,8 @@ class ServiceResult
end
end
+ # @return [Array<Object, nil>] the {#result} wrapped in an Array if the
+ # service call succeeded, or an empty Array if the service call failed.
def to_a
if success?
[result]
@@ -171,6 +276,12 @@ class ServiceResult
end
end
+ ##
+ # Allows ServiceResult to be used with pattern matching.
+ #
+ # @param [Array<:success, :failure, :result, :error>] keys the keys to match
+ # on.
+ # @return [Hash{Symbol=>Object}] the match result.
def deconstruct_keys(keys)
if keys
value = {}
@@ -189,6 +300,7 @@ class ServiceResult
end
end
+ # @return [String] error message associated with the service call.
def message
if @message
@message
@@ -201,13 +313,19 @@ class ServiceResult
end
end
+ # @return [Shared::ServiceState] the Service State object.
def state
@state ||= ::Shared::ServiceState.build
end
##
- # Required as we create an errors object bound to this ServiceResult.
- # calling `errors#full_messages` will call `human_attribute_name` here.
+ # @api private
+ # @note
+ # Required as we create an errors object bound to this ServiceResult.
+ # Calling `errors#full_messages` will call {.human_attribute_name} here.
+ #
+ # @see ApplicationRecord.human_attribute_name
+ # @return [String] the attribute name in a more human format
def self.human_attribute_name(*)
ApplicationRecord.human_attribute_name(*)
end
diff --git a/lib/open_project/patches/active_record_i18n.rb b/lib/open_project/patches/active_record_i18n.rb
index 301ee5a4901..6f1e56b1724 100644
--- a/lib/open_project/patches/active_record_i18n.rb
+++ b/lib/open_project/patches/active_record_i18n.rb
@@ -32,6 +32,7 @@ module ActiveRecord
class Base
include Redmine::I18n
+ # @return [String] the attribute name in a more human format
def self.human_attribute_name(attr, options = {})
attr = attr.to_s.delete_suffix("_id")