-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(can_be_tried): inroduce CanBeTried for result
- Loading branch information
Showing
17 changed files
with
362 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
lib/convenient_service/service/plugins/has_result/entities/result/plugins.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
lib/convenient_service/service/plugins/has_result/entities/result/plugins/can_be_tried.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
30 changes: 30 additions & 0 deletions
30
...enient_service/service/plugins/has_result/entities/result/plugins/can_be_tried/concern.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
3 changes: 3 additions & 0 deletions
3
...ent_service/service/plugins/has_result/entities/result/plugins/can_be_tried/initialize.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "initialize/middleware" |
35 changes: 35 additions & 0 deletions
35
.../service/plugins/has_result/entities/result/plugins/can_be_tried/initialize/middleware.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
3 changes: 3 additions & 0 deletions
3
...ient_service/service/plugins/has_result/entities/result/plugins/can_be_tried/to_kwargs.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "to_kwargs/middleware" |
30 changes: 30 additions & 0 deletions
30
...e/service/plugins/has_result/entities/result/plugins/can_be_tried/to_kwargs/middleware.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...t_service/service/plugins/has_result/entities/result/plugins/can_be_tried/concern_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.