From a9677a03868aa124604151b706c027ec67efd472 Mon Sep 17 00:00:00 2001 From: Guilherme Andrade Date: Mon, 9 Jan 2023 18:43:02 +0100 Subject: [PATCH] Allow use cases to work with do notation --- Gemfile.lock | 2 +- lib/use_cases/module_optins/validated.rb | 1 + lib/use_cases/result.rb | 14 ++++++++-- lib/use_cases/version.rb | 2 +- .../operation_calling_use_case.rb | 27 +++++++++++++++++++ spec/use_cases/result_spec.rb | 16 +++++++++++ 6 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 spec/support/test_subjects/operation_calling_use_case.rb create mode 100644 spec/use_cases/result_spec.rb diff --git a/Gemfile.lock b/Gemfile.lock index c9450a5..b2b1051 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - use_cases (3.0.0) + use_cases (3.0.1) activesupport (>= 6.1.7) dry-matcher (>= 0.8.1) dry-monads (>= 1.0.0) diff --git a/lib/use_cases/module_optins/validated.rb b/lib/use_cases/module_optins/validated.rb index 3258c9b..b793ae7 100644 --- a/lib/use_cases/module_optins/validated.rb +++ b/lib/use_cases/module_optins/validated.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require "dry/validation" require "dry/validation/contract" module UseCases diff --git a/lib/use_cases/result.rb b/lib/use_cases/result.rb index 6fe1dde..e614d30 100644 --- a/lib/use_cases/result.rb +++ b/lib/use_cases/result.rb @@ -10,9 +10,19 @@ def initialize(step, result) @result = result end + def method_missing(method, *args, &block) + return result.send(method, *args, &block) if result.respond_to?(method) + + super + end + + def respond_to_missing?(method, include_private = false) + result.respond_to?(method, include_private) || super + end + def value return result if result_not_monad? - return nil if result_empty? + return if result_empty? result.success? ? result.value! : result.failure end @@ -39,7 +49,7 @@ def nil? end def to_result - self + result end def result_empty? diff --git a/lib/use_cases/version.rb b/lib/use_cases/version.rb index 0f5d439..76cb97a 100644 --- a/lib/use_cases/version.rb +++ b/lib/use_cases/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module UseCases - VERSION = "3.0.1" + VERSION = "3.0.2" end diff --git a/spec/support/test_subjects/operation_calling_use_case.rb b/spec/support/test_subjects/operation_calling_use_case.rb new file mode 100644 index 0000000..b197e71 --- /dev/null +++ b/spec/support/test_subjects/operation_calling_use_case.rb @@ -0,0 +1,27 @@ +class OperationCallingUseCase + include Dry::Monads[:result] + include Dry::Monads::Do.for(:call) + + def call + result = yield call_operation + Success(result) + end + + private + + def call_operation + operation = Operation.new + operation.call({}) + end +end + +# Path: spec/use_cases/steps/operation.rb +class Operation + include UseCase + + step :step_1 + + def step_1 + Success(:step_1) + end +end diff --git a/spec/use_cases/result_spec.rb b/spec/use_cases/result_spec.rb new file mode 100644 index 0000000..d9a6f5c --- /dev/null +++ b/spec/use_cases/result_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +RSpec.describe UseCases::Result do + describe 'self' do + context 'when used by a service using the do notation' do + let(:caller) do + require 'support/test_subjects/operation_calling_use_case' + OperationCallingUseCase.new + end + + it 'works' do + expect(caller.call).to be_success + end + end + end +end