diff --git a/lib/convenient_service/service/plugins/has_j_send_result.rb b/lib/convenient_service/service/plugins/has_j_send_result.rb index d665c758d25..5d80a537702 100644 --- a/lib/convenient_service/service/plugins/has_j_send_result.rb +++ b/lib/convenient_service/service/plugins/has_j_send_result.rb @@ -6,3 +6,42 @@ require_relative "has_j_send_result/commands" require_relative "has_j_send_result/entities" require_relative "has_j_send_result/exceptions" + +module ConvenientService + module Service + module Plugins + module HasJSendResult + class << self + ## + # Checks whether an object is a result instance. + # + # @api public + # + # @param result [Object] Can be any type. + # @return [Boolean] + # + # @example Simple usage. + # class Service + # include ConvenientService::Standard::Config + # + # def result + # success + # end + # end + # + # result = Service.result + # + # ConvenientService::Plugins::Service::HasJSendResult.result?(result) + # # => true + # + # ConvenientService::Plugins::Service::HasJSendResult.result?(42) + # # => false + # + def result?(result) + Commands::IsResult[result: result] + end + end + end + end + end +end diff --git a/spec/lib/convenient_service/service/plugins/has_j_send_result_spec.rb b/spec/lib/convenient_service/service/plugins/has_j_send_result_spec.rb new file mode 100644 index 00000000000..a84450f5ff0 --- /dev/null +++ b/spec/lib/convenient_service/service/plugins/has_j_send_result_spec.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +require "spec_helper" + +require "convenient_service" + +# rubocop:disable RSpec/NestedGroups, RSpec/MultipleMemoizedHelpers +RSpec.describe ConvenientService::Service::Plugins::HasJSendResult do + include ConvenientService::RSpec::Matchers::DelegateTo + + example_group "class methods" do + describe ".result?" do + let(:service) do + Class.new do + include ConvenientService::Service::Configs::Minimal + end + end + + let(:result) { service.success } + + specify do + expect { described_class.result?(result) } + .to delegate_to(ConvenientService::Service::Plugins::HasJSendResult::Commands::IsResult, :call) + .with_arguments(result: result) + .and_return_its_value + end + end + end +end +# rubocop:enable RSpec/NestedGroups, RSpec/MultipleMemoizedHelpers