Skip to content

Delegate Money#clamp to Comparable#clamp - #481

Open
seanpdoyle wants to merge 1 commit into
Shopify:mainfrom
seanpdoyle:numeric-clamp
Open

seanpdoyle wants to merge 1 commit into
Shopify:mainfrom
seanpdoyle:numeric-clamp

Conversation

@seanpdoyle

Copy link
Copy Markdown

The Comparable#clamp interface supports two additional styles of invocation that Money#clamp does not:

  • a Range instance
  • a nil maximum

To support those invocations, this commit uses the ... argument forwarding syntax to pass along any arguments to the underlying #clamp implementation.

To ensure that the ArgumentError is still raised, this commit introduces test coverage. The prior error's message (min cannot be greater than max) is replaced with the message provided by the standard library (min argument must be less than or equal to max argument).

The [Comparable#clamp][] interface supports two additional styles of
invocation that `Money#clamp` does not:

* a [Range][] instance
* a `nil` maximum

To support those invocations, this commit uses the `...` argument
forwarding syntax to pass along any arguments to the underlying `#clamp`
implementation.

To ensure that the `ArgumentError` is still raised, this commit
introduces test coverage. The prior error's message (`min cannot be
greater than max`) is replaced with the message provided by the standard
library (`min argument must be less than or equal to max argument`).

[Comparable#clamp]: https://docs.ruby-lang.org/en/master/Comparable.html#method-i-clamp
[Range]: https://docs.ruby-lang.org/en/master/Range.html

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Delegates Money#clamp to Ruby’s Comparable#clamp, adding support for ranges and unbounded maxima.

Changes:

  • Forward clamp arguments to the underlying value.
  • Add range, nil maximum, and error tests.
  • Update clamp examples.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lib/money/money.rb Implements delegation and updates documentation.
spec/money_spec.rb Covers newly supported invocation styles.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread lib/money/money.rb
# @example
# Money.new(50, "CAD").clamp(1, 100) #=> Money.new(50, "CAD")
# Money.new(50, "CAD").clamp(1..100) #=> Money.new(50, "CAD")
# Money.new(50, "CAD").clamp(1, nil) #=> Money.new(1, "CAD")
Comment thread spec/money_spec.rb

it 'raises an Argument error if the max value is less than the min value' do
money = Money.new(-9001, 'EUR').clamp(min, nil)
expect { money.clamp(max, min) }.to raise_error(ArgumentError, 'min argument must be less than or equal to max argument')
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.

2 participants