From e9546fd66f5a8321da39648419d714d89b39c299 Mon Sep 17 00:00:00 2001 From: Rodrigo Serradura Date: Wed, 24 Jan 2024 23:21:27 -0300 Subject: [PATCH 1/2] Raise error when trying to expose an invalid key --- lib/bcdd/result/context/success.rb | 20 +++++++++- sig/bcdd/result/context.rbs | 9 +++++ .../context/and_expose/invalid_keys_test.rb | 37 +++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 test/bcdd/result/context/and_expose/invalid_keys_test.rb diff --git a/lib/bcdd/result/context/success.rb b/lib/bcdd/result/context/success.rb index ce74df5e..1abc71b4 100644 --- a/lib/bcdd/result/context/success.rb +++ b/lib/bcdd/result/context/success.rb @@ -1,9 +1,23 @@ # frozen_string_literal: true class BCDD::Result + class Context::Error < BCDD::Result::Error + InvalidExposure = ::Class.new(self) + end + class Context::Success < Context include ::BCDD::Result::Success::Methods + FetchValues = ->(acc_values, keys) do + fetched_values = acc_values.fetch_values(*keys) + + keys.zip(fetched_values).to_h + rescue ::KeyError => e + message = "#{e.message}. Available to expose: #{acc_values.keys.map(&:inspect).join(', ')}" + + raise Context::Error::InvalidExposure, message + end + def and_expose(type, keys, terminal: true) unless keys.is_a?(::Array) && !keys.empty? && keys.all?(::Symbol) raise ::ArgumentError, 'keys must be an Array of Symbols' @@ -11,9 +25,11 @@ def and_expose(type, keys, terminal: true) Transitions.tracking.reset_and_then! - exposed_value = acc.merge(value).slice(*keys) + acc_values = acc.merge(value) + + value_to_expose = FetchValues.call(acc_values, keys) - self.class.new(type: type, value: exposed_value, source: source, terminal: terminal) + self.class.new(type: type, value: value_to_expose, source: source, terminal: terminal) end end end diff --git a/sig/bcdd/result/context.rbs b/sig/bcdd/result/context.rbs index 49321630..1cbb0f29 100644 --- a/sig/bcdd/result/context.rbs +++ b/sig/bcdd/result/context.rbs @@ -28,10 +28,19 @@ class BCDD::Result::Context < BCDD::Result def raise_unexpected_outcome_error: (BCDD::Result::Context | untyped, Symbol) -> void end +class BCDD::Result::Context + class Error < BCDD::Result::Error + class InvalidExposure < BCDD::Result::Context::Error + end + end +end + class BCDD::Result::Context class Success < BCDD::Result::Context include BCDD::Result::Success::Methods + FetchValues: Proc + def and_expose: (Symbol, Array[Symbol], terminal: bool) -> BCDD::Result::Context::Success end diff --git a/test/bcdd/result/context/and_expose/invalid_keys_test.rb b/test/bcdd/result/context/and_expose/invalid_keys_test.rb new file mode 100644 index 00000000..49bd35c3 --- /dev/null +++ b/test/bcdd/result/context/and_expose/invalid_keys_test.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'test_helper' + +class BCDD::Result + class ContextAndExposeInvalidKeysTest < Minitest::Test + class Divide + include BCDD::Result::Context.mixin + + def call(arg1, arg2) + validate_numbers(arg1, arg2) + .and_then(:divide, extra_division: 2) + .and_expose(:division_completed, %i[final_numbers extra_division number1 number2]) + end + + private + + def validate_numbers(arg1, arg2) + arg1.is_a?(Numeric) or return Failure(:invalid_arg, message: 'arg1 must be numeric') + arg2.is_a?(Numeric) or return Failure(:invalid_arg, message: 'arg2 must be numeric') + + Success(:ok, number1: arg1, number2: arg2) + end + + def divide(number1:, number2:, extra_division:) + Success(:division_completed, final_number: (number1 / number2) / extra_division) + end + end + + test '#and_expose receive an invalid key' do + err = assert_raises(BCDD::Result::Context::Error::InvalidExposure) { Divide.new.call(12, 2) } + + assert err.message.start_with?('key not found: :final_numbers') + assert err.message.end_with?('. Available to expose: :number1, :number2, :extra_division, :final_number') + end + end +end From 595879bf344c69b95c29242757ae407ac656df88 Mon Sep 17 00:00:00 2001 From: Rodrigo Serradura Date: Wed, 24 Jan 2024 23:27:24 -0300 Subject: [PATCH 2/2] Add CHANGELOG.md --- CHANGELOG.md | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dd0630c..1194b218 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,44 +1,49 @@ - [\[Unreleased\]](#unreleased) -- [\[0.12.0\] - 2024-01-07](#0120---2024-01-07) - [Added](#added) +- [\[0.12.0\] - 2024-01-07](#0120---2024-01-07) + - [Added](#added-1) - [Changed](#changed) - [\[0.11.0\] - 2024-01-02](#0110---2024-01-02) - - [Added](#added-1) + - [Added](#added-2) - [Changed](#changed-1) - [\[0.10.0\] - 2023-12-31](#0100---2023-12-31) - - [Added](#added-2) + - [Added](#added-3) - [\[0.9.1\] - 2023-12-12](#091---2023-12-12) - [Changed](#changed-2) - [Fixed](#fixed) - [\[0.9.0\] - 2023-12-12](#090---2023-12-12) - - [Added](#added-3) + - [Added](#added-4) - [Changed](#changed-3) - [\[0.8.0\] - 2023-12-11](#080---2023-12-11) - - [Added](#added-4) + - [Added](#added-5) - [Changed](#changed-4) - [Removed](#removed) - [\[0.7.0\] - 2023-10-27](#070---2023-10-27) - - [Added](#added-5) + - [Added](#added-6) - [Changed](#changed-5) - [\[0.6.0\] - 2023-10-11](#060---2023-10-11) - - [Added](#added-6) + - [Added](#added-7) - [Changed](#changed-6) - [\[0.5.0\] - 2023-10-09](#050---2023-10-09) - - [Added](#added-7) -- [\[0.4.0\] - 2023-09-28](#040---2023-09-28) - [Added](#added-8) +- [\[0.4.0\] - 2023-09-28](#040---2023-09-28) + - [Added](#added-9) - [Changed](#changed-7) - [Removed](#removed-1) - [\[0.3.0\] - 2023-09-26](#030---2023-09-26) - - [Added](#added-9) -- [\[0.2.0\] - 2023-09-26](#020---2023-09-26) - [Added](#added-10) +- [\[0.2.0\] - 2023-09-26](#020---2023-09-26) + - [Added](#added-11) - [Removed](#removed-2) - [\[0.1.0\] - 2023-09-25](#010---2023-09-25) - - [Added](#added-11) + - [Added](#added-12) ## [Unreleased] +### Added + +- `BCDD::Result::Context` - Raise error when trying to expose an invalid key. + ## [0.12.0] - 2024-01-07 ### Added