Skip to content

Commit

Permalink
feat(can_have_try_result): introduce Service.try_result
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Apr 14, 2023
1 parent c0b2617 commit 92914dc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ module Concern

instance_methods do
##
# Returns `ConvenientService::Service::Plugins::HasResult::Entities::Result` when overridden.
#
# @raise [ConvenientService::Service::Plugins::CanHaveTryResult::Errors::TryResultIsNotOverridden]
#
# @internal
# NOTE: name is inspired by Ruby's `try_convert` methods.
# - https://blog.saeloun.com/2021/08/03/ruby-adds-integer-try-convert
#
# TODO: A plugin that checks that a `success` is returned from `try_result`.
#
def try_result
raise Errors::TryResultIsNotOverridden.new(service: self)
end
end

class_methods do
##
# Returns `ConvenientService::Service::Plugins::HasResult::Entities::Result` when `#try_result` is overridden.
#
# @raise [ConvenientService::Service::Plugins::CanHaveTryResult::Errors::TryResultIsNotOverridden]
#
# @example `try_result` method MUST always return `success` with reasonable "null" data.
Expand Down Expand Up @@ -60,14 +79,8 @@ module Concern
# @see https://thoughtbot.com/blog/rails-refactoring-example-introduce-null-object
# @see https://en.wikipedia.org/wiki/Null_object_pattern
#
# @internal
# NOTE: name is inspired by Ruby's `try_convert` methods.
# - https://blog.saeloun.com/2021/08/03/ruby-adds-integer-try-convert
#
# TODO: A plugin that checks that a `success` is returned from `try_result`.
#
def try_result
raise Errors::TryResultIsNotOverridden.new(service: self)
def try_result(...)
new(...).try_result
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@
require "convenient_service"

RSpec.describe ConvenientService::Service::Plugins::CanHaveTryResult::Concern do
include ConvenientService::RSpec::Matchers::DelegateTo

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

##
# TODO: Remove once `CanHaveTryResult` becomes included into `Standard` config.
#
concerns do
use ConvenientService::Service::Plugins::CanHaveTryResult::Concern
end
end
end

let(:service_instance) { service_class.new }

let(:args) { [:foo] }
let(:kwargs) { {foo: :bar} }
let(:block) { proc { :foo } }

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

Expand All @@ -28,33 +49,38 @@
end

example_group "instance methods" do
let(:service_class) do
Class.new do
include ConvenientService::Configs::Standard

##
# TODO: Remove once `CanHaveTryResult` becomes included into `Standard` config.
#
concerns do
use ConvenientService::Service::Plugins::CanHaveTryResult::Concern
end
describe "#try_result" do
let(:error_message) do
<<~TEXT
Try result method (#try_result) of `#{service_class}` is NOT overridden.
NOTE: Make sure overridden `try_result` returns `success` with reasonable "null" data.
TEXT
end
end

let(:service_instance) { service_class.new }
it "raises `ConvenientService::Service::Plugins::CanHaveTryResult::Errors::TryResultIsNotOverridden`" do
expect { service_instance.try_result }
.to raise_error(ConvenientService::Service::Plugins::CanHaveTryResult::Errors::TryResultIsNotOverridden)
.with_message(error_message)
end
end
end

let(:error_message) do
<<~TEXT
Try result method (#try_result) of `#{service_class}` is NOT overridden.
example_group "class methods" do
describe ".try_result" do
specify do
expect { ignoring_error(ConvenientService::Service::Plugins::CanHaveTryResult::Errors::TryResultIsNotOverridden) { service_class.try_result(*args, **kwargs, &block) } }
.to delegate_to(service_class, :new)
.with_arguments(*args, **kwargs, &block)
end

NOTE: Make sure overridden `try_result` returns `success` with reasonable "null" data.
TEXT
end
specify do
allow(service_class).to receive(:new).and_return(service_instance)

it "raises `ConvenientService::Service::Plugins::CanHaveTryResult::Errors::TryResultIsNotOverridden`" do
expect { service_instance.try_result }
.to raise_error(ConvenientService::Service::Plugins::CanHaveTryResult::Errors::TryResultIsNotOverridden)
.with_message(error_message)
expect { ignoring_error(ConvenientService::Service::Plugins::CanHaveTryResult::Errors::TryResultIsNotOverridden) { service_class.try_result(*args, **kwargs, &block) } }
.to delegate_to(service_instance, :try_result)
.and_return_its_value
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def result
end

let(:service_instance) { service_class.new(*args, **kwargs, &block) }

let(:args) { [:foo] }
let(:kwargs) { {foo: :bar} }
let(:block) { proc { :foo } }
Expand Down

0 comments on commit 92914dc

Please sign in to comment.