Skip to content

Block-form pattern-match handler at the call site #152

Open
serradura wants to merge 1 commit into
mainfrom
feature/wrapper-pattern-match-block-form
Open

Block-form pattern-match handler at the call site #152
serradura wants to merge 1 commit into
mainfrom
feature/wrapper-pattern-match-block-form

Conversation

@serradura

@serradura serradura commented May 27, 2026

Copy link
Copy Markdown
Member

Summary

  • Micro::Case::Result::Wrapper#success / #failure now accept *types (splat) — a strict superset of the prior single-optional-type form, so one branch can match multiple result types (on.success(:ok, :created) { ... }).
  • #success / #failure / #unknown dispatch on block.arity: 2-arity blocks receive (data, type); 1-arity (and 0/-1/-2) keep receiving the full Result. Every existing call site is unchanged byte-for-byte.
  • Unlocks the motivating call site:
    MyCase.call(input) do |on|
      on.success(:ok, :created) { |data, type| ... }
      on.failure                { |data, type| ... }
      on.unknown                { |result|     ... }
    end
  • No new public constants; no new exceptions on previously-silent paths. Exhaustiveness (raise when no branch matched and no unknown was declared) is intentionally deferred to a follow-up gated by an opt-in, so the current silent-Kind::Undefined fallback stays observable.
  • CHANGELOG.md records the change under ## [Unreleased] — version bump intentionally skipped here (per the author's instruction); the release commit can roll this into the next minor.
  • README.md and README.pt-BR.md get a new Block-form handler at the call site / Handler em forma de bloco no local da chamada subsection under "Working with results", in lockstep.

Test plan

  • bundle exec rake test — 814 runs, 8229 assertions, 0 failures
  • ENABLE_TRANSITIONS=true bundle exec rake test — green
  • ENABLE_TRANSITIONS=false bundle exec rake test — green
  • New test/micro/case/result/wrapper_test.rb — 19 cases covering:
    • backward compat: 1-arity / no-type / single-symbol-type variants of success, failure, unknown
    • new *types splat matching on success and failure (matches when any listed type matches; skips when none do)
    • new 2-arity (data, type) blocks on success, failure, unknown
    • mixed-arity in the same handler (motivating shape)
    • type-filter miss falls through so unknown still fires
    • one-branch-wins (first matching success wins; later ones skipped)
    • Micro::Case.call block-form returns wrapper.output; non-block returns the Result
    • unknown does not run when a success/failure already matched
  • CI: full Ruby × Rails matrix + the ENABLE_TRANSITIONS=true|false axis

🤖 Generated with Claude Code

Micro::Case::Result::Wrapper#success / #failure now accept *types so a
single branch can match multiple result types (a strict superset of the
prior single-optional-type form). #success / #failure / #unknown also
dispatch on block.arity: a 2-arity block (|data, type|) receives the
result's data hash and type symbol; 1-arity (and 0/-1/-2) keep
receiving the full Result, so every existing call site is unchanged.

Together these unlock the motivating call site:

  MyCase.call(input) do |on|
    on.success(:ok, :created) { |data, type| ... }
    on.failure                { |data, type| ... }
    on.unknown                { |result| ... }
  end

No new public constants and no new exceptions on previously-silent
paths. Exhaustiveness (raise when no branch matched and no `unknown`
was declared) is intentionally deferred to a follow-up gated by an
opt-in, to preserve the silent-Kind::Undefined fallback.

Closes #4.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@serradura serradura changed the title Block-form pattern-match handler at the call site (closes #4) Block-form pattern-match handler at the call site May 27, 2026
@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