Add Micro::Case::Result#then_expose for boundary projection + re-tagging#153
Open
serradura wants to merge 1 commit into
Open
Add Micro::Case::Result#then_expose for boundary projection + re-tagging#153serradura wants to merge 1 commit into
serradura wants to merge 1 commit into
Conversation
Adds a `Result#then_expose` (alias `#then_return`) primitive that lets
callers project a successful result's accumulated data down to a named
subset of keys and re-tag it with a domain-meaningful `type` in one
call — useful at the boundary of a flow where you hand the result back
to a controller, worker, or outer flow. On a `Failure` result the call
is a no-op (returns `self` unchanged); on `Success`, lookup runs against
the merged map `accessible_attributes + accumulated_data` (accumulated
wins on collisions) and the exposed slice is merged back into
accumulated data so subsequent `.then`-chained use cases still see it.
Inspired by `solid-result`'s `Solid::Output::Success#and_expose`,
adapted to `u-case`'s `Micro::Case::Result` state-transition primitive.
Strictly additive — no existing public API behavior changes (per the
gem's no-breaking-changes Golden rule).
Three call shapes:
result.then_expose(:user_created, [:user, :token])
result.then_expose(:user_created, :user)
result.then_expose([:user, :token]) # type defaults to :data_exposed
A bare Symbol (`then_expose(:user)`) is rejected with `ArgumentError`
because a single Symbol is ambiguous between "type with no keys" and
"key with no type". Empty / non-Symbol keys and non-Symbol types in
the two-arg form likewise raise `ArgumentError` up-front.
Missing keys raise `Micro::Case::Error::InvalidResultExposure`, a
subclass of `::KeyError` so existing `rescue KeyError` clauses keep
working. The message lists the keys that *were* available, e.g.
`key not found: :foo. Available to expose: :a, :b, :c`.
`then_expose` deliberately bypasses the producing use case's
`results { ... }` contract — the exposed shape is the seam's decision,
not the producer's declaration. Documented as such in both READMEs.
Argument validation stays inline in `__resolve_then_expose_args`,
matching the existing "local validity check stays local" pattern (cf.
the `then` method's inline `INVALID_INVOCATION_OF_THE_THEN_METHOD`).
Tests cover all three call shapes; the `ArgumentError` rejection
cases; failure short-circuit; key sourcing from accessible-attributes
only; collision precedence (accumulated wins); the missing-key error
and `::KeyError` rescue compatibility; the `then_return` alias parity;
transitions enabled vs disabled; accumulated-data threading into
subsequent `.then(...)`; `disable_runtime_checks = true` path; and
the producer-side contract bypass (custom and default type).
CHANGELOG gets an `[Unreleased]` Added section; both READMEs get a
new "Projecting and re-tagging with `Result#then_expose`" section
under "Working with results" (in lockstep, per CLAUDE.md). No version
bump.
Closes a follow-up tracked in issue-3-then-expose-from-solid-result.md.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Micro::Case::Result#then_expose(alias#then_return) — projects a successful result's accumulated data down to a named subset of keys and re-tags it with a domain-meaningfultype, in one call. Useful at the boundary of a flow where you hand a result back to a controller / worker / outer flow. On aFailureresult it's a no-op (returnsselfunchanged); onSuccess, lookup runs against the mergedaccessible_attributes + accumulated_datamap (accumulated wins on collisions) and the exposed slice is merged back into accumulated data so subsequent.then-chained use cases still see it.Micro::Case::Error::InvalidResultExposure(subclass of::KeyError) for missing-key errors, with a message that lists the keys that were available.CLAUDE.md). No version bump.Design notes
Inspired by
solid-result'sSolid::Output::Success#and_expose, adapted tou-case'sMicro::Case::Resultstate-transition primitive (__set__).Three call shapes:
A bare Symbol (
then_expose(:user)) is rejected withArgumentErrorbecause a lone Symbol is ambiguous between "type with no keys" and "key with no type". Empty / non-Symbol keys and non-Symbol types in the two-arg form likewise raiseArgumentErrorup-front.then_exposedeliberately bypasses the producing use case'sresults { ... }contract — the exposed shape is the seam's decision, not the producer's declaration. The contract check runs only when the use case callsSuccess(...)/Failure(...)insidecall!. This is implementation-evident (calling__set__directly) and documented in both READMEs.Argument validation stays inline in
__resolve_then_expose_argsrather than routing throughMicro::Case::Check, matching the existing "local validity check stays local" pattern (cf. thethenmethod's inlineINVALID_INVOCATION_OF_THE_THEN_METHOD).Docs
CHANGELOG.md— new[Unreleased]Added section.README.md+README.pt-BR.md— new Projecting and re-tagging withResult#then_exposesection under Working with results, plus a TOC entry. Both READMEs updated in lockstep (perCLAUDE.md). The new section covers all three call shapes, thethen_returnalias, the failure short-circuit, the missing-key error, and an explicit note about the producer-side contract bypass.Test plan
bundle exec rake test TEST=test/micro/case/result/then_expose_test.rb→ 25 tests, 117 assertions, 0 failures (1 skip is the transitions-disabled branch when env defaults to enabled).bundle exec rake test(default suite) → 820 runs, 8309 assertions, 0 failures, 0 errors.bundle exec rake matrixon Ruby 4.0.1 → green across all 4 runs:rails-8-1andrails-edge, each withENABLE_TRANSITIONS=trueand=false. 0 failures total.Micro::Case::Error::InvalidResultExposure < ::KeyErrorso existingrescue KeyErrorclauses keep working unchanged.then_returnproduces identical behavior across all three call shapes.Files changed
lib/micro/case/result.rb— public#then_expose,alias then_return then_expose, private helpers__resolve_then_expose_argsand__fetch_values_to_expose.lib/micro/case/error.rb— newInvalidResultExposureclass.test/micro/case/result/then_expose_test.rb— new test file covering all behaviors above (call shapes, argument validation, failure short-circuit, source/precedence semantics, missing-key error and::KeyErrorrescue,then_returnalias parity, transitions enabled/disabled, accumulated-data threading,disable_runtime_checkspath, contract bypass).CHANGELOG.md,README.md,README.pt-BR.md— Added entry + new docs section in lockstep.🤖 Generated with Claude Code