Skip to content

Commit

Permalink
feat(can_be_tried): inroduce CanBeTried for result
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Jun 9, 2023
1 parent 4f22767 commit 9e68538
Show file tree
Hide file tree
Showing 17 changed files with 362 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lib/convenient_service/configs/standard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,13 @@ class self::Result
use Plugins::Result::CanRecalculateResult::Concern

use Plugins::Result::HasStep::Concern
use Plugins::Result::CanBeTried::Concern
use Plugins::Result::CanHaveParentResult::Concern
end

middlewares :initialize do
use Plugins::Result::HasStep::Initialize::Middleware
use Plugins::Result::CanBeTried::Initialize::Middleware
use Plugins::Result::CanHaveParentResult::Initialize::Middleware
end

Expand Down Expand Up @@ -132,6 +134,7 @@ class self::Result

middlewares :to_kwargs do
use Plugins::Result::HasStep::ToKwargs::Middleware
use Plugins::Result::CanBeTried::ToKwargs::Middleware
use Plugins::Result::CanHaveParentResult::ToKwargs::Middleware
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def next(...)
raise Errors::ServiceTryReturnValueNotKindOfResult.new(service: entity, result: try_result) unless commands.is_result?(try_result)
raise Errors::ServiceTryReturnValueNotSuccess.new(service: entity, result: try_result) unless try_result.success?

try_result.copy
try_result.copy(overrides: {kwargs: {try_result: true}})
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require_relative "plugins/can_recalculate_result"
require_relative "plugins/can_be_tried"
require_relative "plugins/has_j_send_status_and_attributes"
require_relative "plugins/has_inspect"
require_relative "plugins/has_step"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

require_relative "can_be_tried/concern"
require_relative "can_be_tried/initialize"
require_relative "can_be_tried/to_kwargs"
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module ConvenientService
module Service
module Plugins
module HasResult
module Entities
class Result
module Plugins
module CanBeTried
module Concern
include Support::Concern

instance_methods do
##
# @return [Boolean]
#
def try_result?
Utils.to_bool(internals.cache[:try_result])
end
end
end
end
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require_relative "initialize/middleware"
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module ConvenientService
module Service
module Plugins
module HasResult
module Entities
class Result
module Plugins
module CanBeTried
module Initialize
class Middleware < MethodChainMiddleware
intended_for :initialize, entity: :result

##
# @param args [Array<Object>]
# @param kwargs [Hash{Symbol => Object}]
# @param block [Proc, nil]
# @return [void]
#
def next(*args, **kwargs, &block)
entity.internals.cache[:try_result] = kwargs[:try_result]

chain.next(*args, **kwargs, &block)
end
end
end
end
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require_relative "to_kwargs/middleware"
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module ConvenientService
module Service
module Plugins
module HasResult
module Entities
class Result
module Plugins
module CanBeTried
module ToKwargs
class Middleware < MethodChainMiddleware
intended_for :to_kwargs, entity: :result

##
# @return [Hash{Symbol => Object}]
#
def next(...)
chain.next(...).merge(try_result: entity.try_result?)
end
end
end
end
end
end
end
end
end
end
end
3 changes: 3 additions & 0 deletions spec/lib/convenient_service/configs/standard_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
ConvenientService::Common::Plugins::HasResultDuckShortSyntax::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanRecalculateResult::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasStep::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanBeTried::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanHaveParentResult::Concern
]
end
Expand All @@ -221,6 +222,7 @@
ConvenientService::Common::Plugins::NormalizesEnv::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasStep::Initialize::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanBeTried::Initialize::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanHaveParentResult::Initialize::Middleware
]
end
Expand Down Expand Up @@ -352,6 +354,7 @@
[
ConvenientService::Common::Plugins::NormalizesEnv::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasStep::ToKwargs::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanBeTried::ToKwargs::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanHaveParentResult::ToKwargs::Middleware
]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
ConvenientService::Common::Plugins::HasResultDuckShortSyntax::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanRecalculateResult::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasStep::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanBeTried::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanHaveParentResult::Concern
]
end
Expand All @@ -225,6 +226,7 @@
ConvenientService::Common::Plugins::NormalizesEnv::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasStep::Initialize::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanBeTried::Initialize::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanHaveParentResult::Initialize::Middleware
]
end
Expand Down Expand Up @@ -356,6 +358,7 @@
[
ConvenientService::Common::Plugins::NormalizesEnv::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasStep::ToKwargs::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanBeTried::ToKwargs::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanHaveParentResult::ToKwargs::Middleware
]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
ConvenientService::Common::Plugins::HasResultDuckShortSyntax::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanRecalculateResult::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasStep::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanBeTried::Concern,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanHaveParentResult::Concern
]
end
Expand All @@ -227,6 +228,7 @@
ConvenientService::Common::Plugins::NormalizesEnv::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasJSendStatusAndAttributes::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasStep::Initialize::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanBeTried::Initialize::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanHaveParentResult::Initialize::Middleware
]
end
Expand Down Expand Up @@ -358,6 +360,7 @@
[
ConvenientService::Common::Plugins::NormalizesEnv::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::HasStep::ToKwargs::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanBeTried::ToKwargs::Middleware,
ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanHaveParentResult::ToKwargs::Middleware
]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def try_result
specify do
expect { method_value }
.to delegate_to(try_result, :copy)
.without_arguments
.with_arguments(overrides: {kwargs: {try_result: true}})
.and_return_its_value
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

require "spec_helper"

require "convenient_service"

# rubocop:disable RSpec/NestedGroups, RSpec/MultipleMemoizedHelpers
RSpec.describe ConvenientService::Service::Plugins::HasResult::Entities::Result::Plugins::CanBeTried::Concern do
include ConvenientService::RSpec::Matchers::CacheItsValue

example_group "modules" do
include ConvenientService::RSpec::Matchers::IncludeModule

subject { described_class }

it { is_expected.to include_module(ConvenientService::Support::Concern) }

context "when included" do
subject { result_class }

let(:result_class) do
Class.new.tap do |klass|
klass.class_exec(described_class) do |mod|
include mod
end
end
end

it { is_expected.to include_module(described_class::InstanceMethods) }
end
end

example_group "instance methods" do
describe "#try_result?" do
let(:result) { service.result }

let(:service) do
Class.new do
include ConvenientService::Configs::Standard

def result
success
end

def try_result
success
end
end
end

context "when result is NOT from `try_result` method" do
let(:result) { service.result }

it "returns `false`" do
expect(result.try_result?).to eq(false)
end
end

context "when result is from `try_result` method" do
let(:result) { service.try_result }

it "returns `true`" do
expect(result.try_result?).to eq(true)
end
end
end
end
end
# rubocop:enable RSpec/NestedGroups, RSpec/MultipleMemoizedHelpers
Loading

0 comments on commit 9e68538

Please sign in to comment.