Skip to content

Add Micro::Case::Result#then_expose for boundary projection + re-tagging#153

Open
serradura wants to merge 1 commit into
mainfrom
feature/result-then-expose
Open

Add Micro::Case::Result#then_expose for boundary projection + re-tagging#153
serradura wants to merge 1 commit into
mainfrom
feature/result-then-expose

Conversation

@serradura

Copy link
Copy Markdown
Member

Summary

  • Adds a new public 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-meaningful type, in one call. Useful at the boundary of a flow where you hand a result back to a controller / worker / outer flow. On a Failure result it's a no-op (returns self unchanged); on Success, lookup runs against the merged accessible_attributes + accumulated_data map (accumulated wins on collisions) and the exposed slice is merged back into accumulated data so subsequent .then-chained use cases still see it.
  • Adds Micro::Case::Error::InvalidResultExposure (subclass of ::KeyError) for missing-key errors, with a message that lists the keys that were available.
  • Strictly additive — no existing public API behavior changes (per the gem's no-breaking-changes Golden rule in CLAUDE.md). No version bump.

Design notes

Inspired by solid-result's Solid::Output::Success#and_expose, adapted to u-case's Micro::Case::Result state-transition primitive (__set__).

Three call shapes:

result.then_expose(:user_created, [:user, :token])  # type + Array of keys
result.then_expose(:user_created, :user)            # type + single Symbol key (normalized to [:user])
result.then_expose([:user, :token])                 # Array-only — type defaults to :data_exposed

A bare Symbol (then_expose(:user)) is rejected with ArgumentError because 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 raise ArgumentError up-front.

then_expose deliberately bypasses the producing use case's results { ... } contract — the exposed shape is the seam's decision, not the producer's declaration. The contract check runs only when the use case calls Success(...) / Failure(...) inside call!. This is implementation-evident (calling __set__ directly) and documented in both READMEs.

Argument validation stays inline in __resolve_then_expose_args rather than routing through Micro::Case::Check, matching the existing "local validity check stays local" pattern (cf. the then method's inline INVALID_INVOCATION_OF_THE_THEN_METHOD).

Docs

  • CHANGELOG.md — new [Unreleased] Added section.
  • README.md + README.pt-BR.md — new Projecting and re-tagging with Result#then_expose section under Working with results, plus a TOC entry. Both READMEs updated in lockstep (per CLAUDE.md). The new section covers all three call shapes, the then_return alias, 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 matrix on Ruby 4.0.1 → green across all 4 runs: rails-8-1 and rails-edge, each with ENABLE_TRANSITIONS=true and =false. 0 failures total.
  • Verified Micro::Case::Error::InvalidResultExposure < ::KeyError so existing rescue KeyError clauses keep working unchanged.
  • Verified the alias then_return produces 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_args and __fetch_values_to_expose.
  • lib/micro/case/error.rb — new InvalidResultExposure class.
  • 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 ::KeyError rescue, then_return alias parity, transitions enabled/disabled, accumulated-data threading, disable_runtime_checks path, contract bypass).
  • CHANGELOG.md, README.md, README.pt-BR.md — Added entry + new docs section in lockstep.

🤖 Generated with Claude Code

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>
@serradura serradura self-assigned this May 27, 2026
@serradura serradura added this to the 5.x milestone May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant