Skip to content

Commit

Permalink
Raise warning on using Money.default_currency
Browse files Browse the repository at this point in the history
  • Loading branch information
antstorm committed Jul 14, 2019
1 parent 4293055 commit d4372da
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 15 deletions.
24 changes: 17 additions & 7 deletions lib/money/money.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,33 @@ class << self
:locale_backend

# @attr_writer rounding_mode Use this to specify the rounding mode
#
# @!attribute default_currency
# @return [Money::Currency] The default currency, which is used when
# +Money.new+ is called without an explicit currency argument. The
# default value is Currency.new("USD"). The value must be a valid
# +Money::Currency+ instance.
attr_writer :rounding_mode, :default_currency
attr_writer :rounding_mode

end

# @!attribute default_currency
# @return [Money::Currency] The default currency, which is used when
# +Money.new+ is called without an explicit currency argument. The
# default value is Currency.new("USD"). The value must be a valid
# +Money::Currency+ instance.
def self.default_currency
if @using_deprecated_default_currency
warn '[WARNING] The default currency will change to `nil` in the next major release. Make ' \
'sure to set it explicitly using `Money.default_currency=` to avoid potential issues'
end

if @default_currency.respond_to?(:call)
Money::Currency.new(@default_currency.call)
else
Money::Currency.new(@default_currency)
end
end

def self.default_currency=(currency)
@using_deprecated_default_currency = false
@default_currency = currency
end

def self.locale_backend=(value)
@locale_backend = value ? LocaleBackend.find(value) : nil
end
Expand All @@ -163,6 +172,7 @@ def self.setup_defaults

# Set the default currency for creating new +Money+ object.
self.default_currency = Currency.new("USD")
@using_deprecated_default_currency = true

# Default to using i18n
@use_i18n = true
Expand Down
31 changes: 23 additions & 8 deletions spec/money_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,16 @@
end

it 'warns about rounding_mode deprecation' do
expect(Money).to receive(:warn)
allow(Money).to receive(:warn)

expect(Money.from_amount(1.999).to_d).to eq 2
expect(Money.rounding_mode(BigDecimal::ROUND_DOWN) do
Money.from_amount(1.999).to_d
end).to eq 1.99
expect(Money)
.to have_received(:warn)
.with('[DEPRECATION] calling `rounding_mode` with a block is deprecated. ' \
'Please use `.with_rounding_mode` instead.')
end

it 'rounds using with_rounding_mode' do
Expand Down Expand Up @@ -881,13 +886,7 @@ def m.amount
end

describe ".default_currency" do
before do
@default_currency = Money.default_currency
end

after do
Money.default_currency = @default_currency
end
after { Money.setup_defaults }

it "accepts a lambda" do
Money.default_currency = lambda { :eur }
Expand All @@ -898,5 +897,21 @@ def m.amount
Money.default_currency = :eur
expect(Money.default_currency).to eq Money::Currency.new(:eur)
end

it 'warns about changing default_currency value' do
expect(Money)
.to receive(:warn)
.with('[WARNING] The default currency will change to `nil` in the next major release. Make ' \
'sure to set it explicitly using `Money.default_currency=` to avoid potential issues')

Money.default_currency
end

it 'does not warn if the default_currency has been changed' do
Money.default_currency = Money::Currency.new(:usd)

expect(Money).not_to receive(:warn)
Money.default_currency
end
end
end

0 comments on commit d4372da

Please sign in to comment.