Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise error when trying to expose an invalid key #28

Merged
merged 2 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 18 additions & 2 deletions lib/bcdd/result/context/success.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,35 @@
# 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'
end

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
9 changes: 9 additions & 0 deletions sig/bcdd/result/context.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 37 additions & 0 deletions test/bcdd/result/context/and_expose/invalid_keys_test.rb
Original file line number Diff line number Diff line change
@@ -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
Loading