Skip to content

Commit 392a5b7

Browse files
committed
Deprecate Money#dollars when the currency is not USD
1 parent 324bae2 commit 392a5b7

File tree

4 files changed

+37
-34
lines changed

4 files changed

+37
-34
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- Add Caribbean Guilder (XCG) as replacement for Netherlands Antillean Gulden (ANG)
2424
- Add `Money#to_nearest_cash_value` to return a rounded Money instance to the smallest denomination
2525
- Deprecate `Money#round_to_nearest_cash_value` in favor of calling `to_nearest_cash_value.fractional`
26+
- Deprecate `Money#dollars` when the currency is not USD, prefer calling `#amount`
2627
- Add `Money::Currency#cents_based?` to check if currency is cents-based
2728
- Add ability to nest `Money.with_rounding_mode` blocks
2829
- Allow `nil` to be used as a default_currency

lib/money/money.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -365,34 +365,37 @@ def initialize(obj, currency = nil, options = {})
365365
end
366366

367367
# Assuming using a currency using dollars:
368-
# Returns the value of the money in dollars,
369-
# instead of in the fractional unit cents.
368+
# Returns the value of the money in dollars, instead of in the fractional unit
369+
# cents.
370370
#
371371
# Synonym of #amount
372372
#
373373
# @return [BigDecimal]
374374
#
375375
# @example
376-
# Money.new(1_00, "USD").dollars # => BigDecimal("1.00")
376+
# Money.new(1_00, "USD").dollars # => BigDecimal("1.00")
377377
#
378378
# @see #amount
379379
# @see #to_d
380380
# @see #cents
381-
#
382381
def dollars
382+
unless currency == "USD"
383+
warn "[DEPRECATION] `#dollars` was called on an instance of Money in " \
384+
"#{currency}. Prefer calling `#amount` instead."
385+
end
386+
383387
amount
384388
end
385389

386-
# Returns the numerical value of the money
390+
# Returns the numerical value of the money.
387391
#
388392
# @return [BigDecimal]
389393
#
390394
# @example
391-
# Money.new(1_00, "USD").amount # => BigDecimal("1.00")
395+
# Money.new(1_00, "USD").amount # => BigDecimal("1.00")
392396
#
393397
# @see #to_d
394398
# @see #fractional
395-
#
396399
def amount
397400
to_d
398401
end
@@ -595,9 +598,7 @@ def allocate(parts)
595598
# @example
596599
# Money.new(10.1, 'USD').round #=> Money.new(10, 'USD')
597600
#
598-
# @see
599-
# Money.default_infinite_precision
600-
#
601+
# @see Money.default_infinite_precision
601602
def round(rounding_mode = self.class.rounding_mode, rounding_precision = 0)
602603
rounded_amount = as_d(@fractional).round(rounding_precision, rounding_mode)
603604
dup_with(fractional: rounded_amount)

sig/lib/money/money.rbs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -318,32 +318,30 @@ class Money
318318
def initialize: (Object obj, ?(Money::Currency | string | Symbol) currency, ?::Hash[untyped, untyped] options) -> Money
319319

320320
# Assuming using a currency using dollars:
321-
# Returns the value of the money in dollars,
322-
# instead of in the fractional unit cents.
321+
# Returns the value of the money in dollars, instead of in the fractional unit
322+
# cents.
323323
#
324324
# Synonym of #amount
325325
#
326326
# @return [BigDecimal]
327327
#
328328
# @example
329-
# Money.new(1_00, "USD").dollars # => BigDecimal("1.00")
329+
# Money.new(1_00, "USD").dollars # => BigDecimal("1.00")
330330
#
331331
# @see #amount
332332
# @see #to_d
333333
# @see #cents
334-
#
335334
def dollars: () -> BigDecimal
336335

337-
# Returns the numerical value of the money
336+
# Returns the numerical value of the money.
338337
#
339338
# @return [BigDecimal]
340339
#
341340
# @example
342-
# Money.new(1_00, "USD").amount # => BigDecimal("1.00")
341+
# Money.new(1_00, "USD").amount # => BigDecimal("1.00")
343342
#
344343
# @see #to_d
345344
# @see #fractional
346-
#
347345
def amount: () -> BigDecimal
348346

349347
# Returns a Integer hash value based on the +fractional+ and +currency+ attributes
@@ -498,9 +496,7 @@ class Money
498496
# @example
499497
# Money.new(10.1, 'USD').round #=> Money.new(10, 'USD')
500498
#
501-
# @see
502-
# Money.default_infinite_precision
503-
#
499+
# @see Money.default_infinite_precision
504500
def round: (?untyped rounding_mode, ?::Integer rounding_precision) -> Money
505501

506502
# Creates a formatted price string according to several rules.

spec/money_spec.rb

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -614,15 +614,18 @@ def expectation.fractional
614614
end
615615

616616
describe "#dollars" do
617-
it "is synonym of #amount" do
618-
m = Money.new(0)
617+
it "is a synonym of #amount" do
618+
expect(Money.new(1_23, "USD").dollars).to eq(1.23)
619+
end
619620

620-
# Make a small expectation
621-
def m.amount
622-
5
623-
end
621+
context "when using another currency" do
622+
it "warns" do
623+
amount = Money.new(1_23, "EUR")
624624

625-
expect(m.dollars).to eq 5
625+
allow(amount).to receive(:warn)
626+
expect(amount.dollars).to eq(1.23)
627+
expect(amount).to have_received(:warn).with(/DEPRECATION/)
628+
end
626629
end
627630
end
628631

@@ -648,14 +651,16 @@ def m.amount
648651
end
649652

650653
describe "#symbol" do
651-
it "works as documented" do
652-
currency = Money::Currency.new("EUR")
653-
expect(currency).to receive(:symbol).and_return("€")
654-
expect(Money.new(0, currency).symbol).to eq "€"
654+
it "returns the currency’s symbol" do
655+
expect(Money.new(0, "EUR").symbol).to eq("€")
656+
end
655657

656-
currency = Money::Currency.new("EUR")
657-
expect(currency).to receive(:symbol).and_return(nil)
658-
expect(Money.new(0, currency).symbol).to eq "¤"
658+
context "when the currency has no symbol" do
659+
it "returns a generic currency symbol" do
660+
currency = Money::Currency.new("EUR")
661+
expect(currency).to receive(:symbol).and_return(nil)
662+
expect(Money.new(0, currency).symbol).to eq "¤"
663+
end
659664
end
660665
end
661666

0 commit comments

Comments
 (0)