Skip to content

Commit

Permalink
refactor(has_negated_j_send_result): move negated_result from service…
Browse files Browse the repository at this point in the history
… to result
  • Loading branch information
marian13 committed Jan 28, 2024
1 parent 0fe5488 commit 7968a2f
Show file tree
Hide file tree
Showing 13 changed files with 416 additions and 385 deletions.
2 changes: 1 addition & 1 deletion lib/convenient_service/service/configs/standard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class self::Result
use ConvenientService::Plugins::Common::HasJSendResultDuckShortSyntax::Concern
use ConvenientService::Plugins::Result::CanRecalculateResult::Concern

use ConvenientService::Plugins::Result::CanBeNegated::Concern
use ConvenientService::Plugins::Result::HasNegatedResult::Concern
use ConvenientService::Plugins::Result::CanBeOwnResult::Concern
use ConvenientService::Plugins::Result::CanHaveFallbacks::Concern
use ConvenientService::Plugins::Result::CanHaveParentResult::Concern
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

require_relative "plugins/can_recalculate_result"
require_relative "plugins/can_be_negated"
require_relative "plugins/has_negated_result"
require_relative "plugins/can_be_own_result"
require_relative "plugins/can_be_stubbed_result"
require_relative "plugins/can_have_fallbacks"
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require_relative "has_negated_result/concern"
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

module ConvenientService
module Service
module Plugins
module HasJSendResult
module Entities
class Result
module Plugins
module HasNegatedResult
module Concern
include Support::Concern

instance_methods do
##
# @return [Boolean]
#
def negated?
Utils.to_bool(extra_kwargs[:negated])
end

##
# @return [ConvenientService::Service::Plugins::HasJSendResult::Entities::Result]
#
# @internal
# NOTE: `original_result = result` is used to cache `result` independently, without relying on `CachesReturnValue` plugin.
#
# TODO: `Utils::String.concat_if`? How?
# - https://stackoverflow.com/a/28648594/12201472
#
def negated_result
case status.to_sym
when :success
service.failure(
data: unsafe_data,
message: "Original `result` is `success`#{" with `message` - #{unsafe_message}" unless unsafe_message.empty?}",
code: "negated_#{unsafe_code}"
)
when :failure
service.success(
data: unsafe_data,
message: "Original `result` is `failure`#{" with `message` - #{unsafe_message}" unless unsafe_message.empty?}",
code: "negated_#{unsafe_code}"
)
when :error
copy
end
end
end
end
end
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,10 @@ module Concern
instance_methods do
##
# @return [ConvenientService::Service::Plugins::HasJSendResult::Entities::Result]
#
# @internal
# NOTE: `original_result = result` is used to cache `result` independently, without relying on `CachesReturnValue` plugin.
#
# TODO: `Utils::String.concat_if`? How?
# - https://stackoverflow.com/a/28648594/12201472
# @note Users may override this method to provider custom `negataed_result` behavior.
#
def negated_result
original_result = result

case result.status.to_sym
when :success
failure(
data: original_result.unsafe_data,
message: "Original `result` is `success`#{" with `message` - #{original_result.unsafe_message}" unless original_result.unsafe_message.empty?}",
code: "negated_#{original_result.unsafe_code}"
)
when :failure
success(
data: original_result.unsafe_data,
message: "Original `result` is `failure`#{" with `message` - #{original_result.unsafe_message}" unless original_result.unsafe_message.empty?}",
code: "negated_#{original_result.unsafe_code}"
)
when :error
original_result.copy
end
result.negated_result
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanHaveStep::Concern,
ConvenientService::Common::Plugins::HasJSendResultDuckShortSyntax::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanRecalculateResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanBeNegated::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::HasNegatedResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanBeOwnResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanHaveFallbacks::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanHaveParentResult::Concern,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanHaveStep::Concern,
ConvenientService::Common::Plugins::HasJSendResultDuckShortSyntax::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanRecalculateResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanBeNegated::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::HasNegatedResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanBeOwnResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanHaveFallbacks::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanHaveParentResult::Concern,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanHaveStep::Concern,
ConvenientService::Common::Plugins::HasJSendResultDuckShortSyntax::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanRecalculateResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanBeNegated::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::HasNegatedResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanBeOwnResult::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanHaveFallbacks::Concern,
ConvenientService::Service::Plugins::HasJSendResult::Entities::Result::Plugins::CanHaveParentResult::Concern,
Expand Down

This file was deleted.

0 comments on commit 7968a2f

Please sign in to comment.