Skip to content

Commit

Permalink
Raise warning on using default Money.rounding_mode (RubyMoney#883)
Browse files Browse the repository at this point in the history
  • Loading branch information
antstorm authored Jul 14, 2019
1 parent 6be1cfb commit 8754d9a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
26 changes: 19 additions & 7 deletions lib/money/money.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ class << self
attr_accessor :default_bank, :default_formatting_rules,
:use_i18n, :infinite_precision, :conversion_precision,
:locale_backend

# @attr_writer rounding_mode Use this to specify the rounding mode
attr_writer :rounding_mode

end

# @!attribute default_currency
Expand Down Expand Up @@ -156,6 +152,12 @@ def self.locale_backend=(value)
@locale_backend = value ? LocaleBackend.find(value) : nil
end

# @attr_writer rounding_mode Use this to specify the rounding mode
def self.rounding_mode=(new_rounding_mode)
@using_deprecated_default_rounding_mode = false
@rounding_mode = new_rounding_mode
end

def self.use_i18n=(value)
if value
warn '[DEPRECATION] `use_i18n` is deprecated - use `Money.locale_backend = :i18n` instead for locale based formatting'
Expand Down Expand Up @@ -185,6 +187,7 @@ def self.setup_defaults

# Default to bankers rounding
self.rounding_mode = BigDecimal::ROUND_HALF_EVEN
@using_deprecated_default_rounding_mode = true

# Default the conversion of Rationals precision to 16
self.conversion_precision = 16
Expand All @@ -202,10 +205,19 @@ def self.inherited(base)
#
# @return [BigDecimal::ROUND_MODE] rounding mode
def self.rounding_mode(mode = nil)
return Thread.current[:money_rounding_mode] || @rounding_mode unless mode
if mode
warn "[DEPRECATION] calling `rounding_mode` with a block is deprecated. Please use `.with_rounding_mode` instead."
return with_rounding_mode(mode) { yield }
end

return Thread.current[:money_rounding_mode] if Thread.current[:money_rounding_mode]

if @using_deprecated_default_rounding_mode
warn '[WARNING] The default rounding mode will change to `ROUND_HALF_UP` in the next major ' \
'release. Set it explicitly using `Money.rounding_mode=` to avoid potential problems.'
end

warn "[DEPRECATION] calling `rounding_mode` with a block is deprecated. Please use `.with_rounding_mode` instead."
with_rounding_mode(mode) { yield }
@rounding_mode
end

# This method temporarily changes the rounding mode. It will then return the
Expand Down
24 changes: 21 additions & 3 deletions spec/money_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,7 @@ def expectation.fractional
end

context "user changes rounding_mode" do
after do
Money.rounding_mode = BigDecimal::ROUND_HALF_EVEN
end
after { Money.setup_defaults }

context "with the setter" do
it "respects the rounding_mode" do
Expand Down Expand Up @@ -914,4 +912,24 @@ def m.amount
Money.default_currency
end
end

describe ".rounding_mode" do
after { Money.setup_defaults }

it 'warns about changing default rounding_mode value' do
expect(Money)
.to receive(:warn)
.with('[WARNING] The default rounding mode will change to `ROUND_HALF_UP` in the next major ' \
'release. Set it explicitly using `Money.rounding_mode=` to avoid potential problems.')

Money.rounding_mode
end

it 'does not warn if the default rounding_mode has been changed' do
Money.rounding_mode = BigDecimal::ROUND_HALF_UP

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

0 comments on commit 8754d9a

Please sign in to comment.