Skip to content

Commit 3bfb4a2

Browse files
authored
Merge pull request #1153 from sunny/deprecate-calling-dollars-on-non-dollars
Deprecate `Money#dollars` and `Money.from_dollars`
2 parents 956ece2 + 80eb00c commit 3bfb4a2

File tree

4 files changed

+51
-59
lines changed

4 files changed

+51
-59
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
```
3838
- Add `Money#to_nearest_cash_value` to return a rounded Money instance to the smallest denomination
3939
- Deprecate `Money#round_to_nearest_cash_value` in favor of calling `to_nearest_cash_value.fractional`
40+
- Deprecate `Money#dollars` in favor of `Money#amount`.
41+
- Deprecate `Money.from_dollars` in favor of `Money.from_amount`.
4042
- Add `Money::Currency#cents_based?` to check if currency is cents-based
4143
- Add ability to nest `Money.with_rounding_mode` blocks
4244
- Allow `nil` to be used as a default_currency

lib/money/money.rb

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,18 @@ def self.from_amount(amount, currency = default_currency, options = {})
345345
new(value, currency, options)
346346
end
347347

348+
# DEPRECATED.
349+
#
350+
# @see Money.from_amount
351+
def self.from_dollars(amount, currency = default_currency, options = {})
352+
warn "[DEPRECATION] `Money.from_dollars` is deprecated in favor of " \
353+
"`Money.from_amount`."
354+
355+
from_amount(amount, currency, options)
356+
end
357+
348358
class << self
349359
alias_method :from_cents, :new
350-
alias_method :from_dollars, :from_amount
351360
end
352361

353362
# Creates a new Money object of value given in the
@@ -388,35 +397,25 @@ def initialize(obj, currency = nil, options = {})
388397
raise Currency::NoCurrency, 'must provide a currency' if @currency.nil?
389398
end
390399

391-
# Assuming using a currency using dollars:
392-
# Returns the value of the money in dollars,
393-
# instead of in the fractional unit cents.
394-
#
395-
# Synonym of #amount
396-
#
397-
# @return [BigDecimal]
398-
#
399-
# @example
400-
# Money.new(1_00, "USD").dollars # => BigDecimal("1.00")
400+
# DEPRECATED.
401401
#
402402
# @see #amount
403-
# @see #to_d
404-
# @see #cents
405-
#
406403
def dollars
404+
warn "[DEPRECATION] `Money#dollars` is deprecated in favor of " \
405+
"`Money#amount`."
406+
407407
amount
408408
end
409409

410-
# Returns the numerical value of the money
410+
# Returns the numerical value of the money.
411411
#
412412
# @return [BigDecimal]
413413
#
414414
# @example
415-
# Money.new(1_00, "USD").amount # => BigDecimal("1.00")
415+
# Money.new(1_00, "USD").amount # => BigDecimal("1.00")
416416
#
417417
# @see #to_d
418418
# @see #fractional
419-
#
420419
def amount
421420
to_d
422421
end
@@ -619,9 +618,7 @@ def allocate(parts)
619618
# @example
620619
# Money.new(10.1, 'USD').round #=> Money.new(10, 'USD')
621620
#
622-
# @see
623-
# Money.default_infinite_precision
624-
#
621+
# @see Money.default_infinite_precision
625622
def round(rounding_mode = self.class.rounding_mode, rounding_precision = 0)
626623
rounded_amount = as_d(@fractional).round(rounding_precision, rounding_mode)
627624
dup_with(fractional: rounded_amount)

sig/lib/money/money.rbs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,12 @@ class Money
308308
# @see #initialize
309309
def self.from_amount: (Numeric amount, ?(Money::Currency | string | Symbol) currency, ?::Hash[untyped, untyped] options) -> Money
310310

311-
alias self.from_cents self.new
311+
# DEPRECATED.
312+
#
313+
# @see Money.from_amount
314+
def self.from_amount: (Numeric amount, ?(Money::Currency | string | Symbol) currency, ?::Hash[untyped, untyped] options) -> Money
312315

313-
alias self.from_dollars self.from_amount
316+
alias self.from_cents self.new
314317

315318
# Creates a new Money object of value given in the
316319
# +fractional unit+ of the given +currency+.
@@ -335,33 +338,20 @@ class Money
335338
#
336339
def initialize: (Object obj, ?(Money::Currency | string | Symbol) currency, ?::Hash[untyped, untyped] options) -> Money
337340

338-
# Assuming using a currency using dollars:
339-
# Returns the value of the money in dollars,
340-
# instead of in the fractional unit cents.
341-
#
342-
# Synonym of #amount
343-
#
344-
# @return [BigDecimal]
345-
#
346-
# @example
347-
# Money.new(1_00, "USD").dollars # => BigDecimal("1.00")
341+
# DEPRECATED.
348342
#
349343
# @see #amount
350-
# @see #to_d
351-
# @see #cents
352-
#
353344
def dollars: () -> BigDecimal
354345

355-
# Returns the numerical value of the money
346+
# Returns the numerical value of the money.
356347
#
357348
# @return [BigDecimal]
358349
#
359350
# @example
360-
# Money.new(1_00, "USD").amount # => BigDecimal("1.00")
351+
# Money.new(1_00, "USD").amount # => BigDecimal("1.00")
361352
#
362353
# @see #to_d
363354
# @see #fractional
364-
#
365355
def amount: () -> BigDecimal
366356

367357
# Returns a Integer hash value based on the +fractional+ and +currency+ attributes
@@ -516,9 +506,7 @@ class Money
516506
# @example
517507
# Money.new(10.1, 'USD').round #=> Money.new(10, 'USD')
518508
#
519-
# @see
520-
# Money.default_infinite_precision
521-
#
509+
# @see Money.default_infinite_precision
522510
def round: (?untyped rounding_mode, ?::Integer rounding_precision) -> Money
523511

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

spec/money_spec.rb

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@
135135
context 'initializing with .from_dollars' do
136136
subject(:money) { Money.from_dollars(initializing_value) }
137137

138-
it 'works just as with .from_amount' do
139-
expect(money.dollars).to eq initializing_value
138+
it "is a deprecated synonym of .from_amount" do
139+
allow(Money).to receive(:warn)
140+
expect(money.amount).to eq initializing_value
141+
expect(Money).to have_received(:warn).with(/DEPRECATION/)
140142
end
141143
end
142144
end
@@ -587,7 +589,7 @@
587589
end
588590

589591
describe "#amount" do
590-
it "returns the amount of cents as dollars" do
592+
it "returns the amount of cents as the full unit" do
591593
expect(Money.new(1_00).amount).to eq 1
592594
end
593595

@@ -608,15 +610,12 @@
608610
end
609611

610612
describe "#dollars" do
611-
it "is synonym of #amount" do
612-
m = Money.new(0)
613+
it "is a deprecated synonym of #amount" do
614+
money = Money.new(1_23)
613615

614-
# Make a small expectation
615-
def m.amount
616-
5
617-
end
618-
619-
expect(m.dollars).to eq 5
616+
allow(money).to receive(:warn)
617+
expect(money.dollars).to eq(1.23)
618+
expect(money).to have_received(:warn).with(/DEPRECATION/)
620619
end
621620
end
622621

@@ -642,14 +641,20 @@ def m.amount
642641
end
643642

644643
describe "#symbol" do
645-
it "works as documented" do
646-
currency = Money::Currency.new("EUR")
647-
expect(currency).to receive(:symbol).and_return("€")
648-
expect(Money.new(0, currency).symbol).to eq "€"
644+
it "returns the currency’s symbol" do
645+
expect(Money.new(0, "EUR").symbol).to eq("€")
646+
end
649647

650-
currency = Money::Currency.new("EUR")
651-
expect(currency).to receive(:symbol).and_return(nil)
652-
expect(Money.new(0, currency).symbol).to eq "¤"
648+
context "when the currency has no symbol" do
649+
let(:currency) { Money::Currency.new("EUR") }
650+
651+
before do
652+
expect(currency).to receive(:symbol).and_return(nil)
653+
end
654+
655+
it "returns a generic currency symbol" do
656+
expect(Money.new(0, currency).symbol).to eq "¤"
657+
end
653658
end
654659
end
655660

0 commit comments

Comments
 (0)