Skip to content

Commit

Permalink
apply or remove legacy_deprecations to a block of code
Browse files Browse the repository at this point in the history
this allows being explicit about if a code block is ready for the next gem update or not
  • Loading branch information
elfassy committed Feb 3, 2025
1 parent 51619fd commit 61c4e49
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
34 changes: 26 additions & 8 deletions lib/money/money.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,34 @@ def with_currency(new_currency, &block)
Money.current_currency = old_currency
end

def current_cross_currency_deprecation
Thread.current[:money_gem_cross_currency_deprecation]
def legacy_deprecations
Thread.current[:money_gem_legacy_deprecations] || reset_legacy_deprecations
end

def cross_currency_deprecation(&block)
# Get cross-currency deprecation instead of exception
Thread.current[:money_gem_cross_currency_deprecation] = true
def disable_legacy_deprecations
Thread.current[:money_gem_legacy_deprecations] = false
end

def enable_legacy_deprecations
Thread.current[:money_gem_legacy_deprecations] = true
end

def reset_legacy_deprecations
Thread.current[:money_gem_legacy_deprecations] = Money.config.legacy_deprecations
end

def without_legacy_deprecations(&block)
disable_legacy_deprecations
yield
ensure
reset_legacy_deprecations
end

def with_legacy_deprecations(&block)
enable_legacy_deprecations
yield
ensure
Thread.current[:money_gem_cross_currency_deprecation] = false
reset_legacy_deprecations
end

private
Expand All @@ -143,7 +161,7 @@ def new_from_money(amount, currency)
msg = "Money.new(Money.new(amount, #{amount.currency}), #{currency}) " \
"is changing the currency of an existing money object"

if Money.current_cross_currency_deprecation
if Money.legacy_deprecations
Money.deprecate("#{msg}. A Money::IncompatibleCurrencyError will raise in the next major release")
Money.new(amount.value, currency)
else
Expand Down Expand Up @@ -413,7 +431,7 @@ def arithmetic(other)
def ensure_compatible_currency(other_currency, msg)
return if currency.compatible?(other_currency)

if Money.current_cross_currency_deprecation
if Money.legacy_deprecations
Money.deprecate("#{msg}. A Money::IncompatibleCurrencyError will raise in the next major release")
else
raise Money::IncompatibleCurrencyError, msg
Expand Down
20 changes: 13 additions & 7 deletions spec/money_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,18 @@
end

it "legacy_deprecations #to_money doesn't overwrite the money object's currency" do
Money.cross_currency_deprecation do
Money.with_legacy_deprecations do
expect(Money).to receive(:deprecate).with(match(/to_money is attempting to change currency of an existing money object/)).once
expect(Money.new(1, 'USD').to_money('CAD')).to eq(Money.new(1, 'USD'))
end
end

it "legacy_deprecations #to_money doesn't overwrite the money object's currency" do
Money.without_legacy_deprecations do
expect{ Money.new(1, 'USD').to_money('CAD') }.to raise_error(Money::IncompatibleCurrencyError)
end
end

it "#to_money raises when changing currency" do
expect{ Money.new(1, 'USD').to_money('CAD') }.to raise_error(Money::IncompatibleCurrencyError)
end
Expand Down Expand Up @@ -102,7 +108,7 @@
end

it "legacy_deprecations constructor with money used the constructor currency" do
Money.cross_currency_deprecation do
Money.with_legacy_deprecations do
expect(Money).to receive(:deprecate).with(match(/Money.new\(Money.new\(amount, USD\), CAD\) is changing the currency of an existing money object/)).once
expect(Money.new(Money.new(1, 'USD'), 'CAD')).to eq(Money.new(1, 'CAD'))
end
Expand Down Expand Up @@ -214,7 +220,7 @@
end

it "legacy_deprecations adds inconsistent currencies" do
Money.cross_currency_deprecation do
Money.with_legacy_deprecations do
expect(Money).to receive(:deprecate).once
expect(Money.new(5, 'USD') + Money.new(1, 'CAD')).to eq(Money.new(6, 'USD'))
end
Expand All @@ -237,7 +243,7 @@
end

it "logs a deprecation warning when adding across currencies" do
Money.cross_currency_deprecation do
Money.with_legacy_deprecations do
expect(Money).to receive(:deprecate).with(match(/mathematical operation not permitted for Money objects with different currencies/))
expect(Money.new(10, 'USD') - Money.new(1, 'JPY')).to eq(Money.new(9, 'USD'))
end
Expand Down Expand Up @@ -476,7 +482,7 @@
it "generates a true rational" do
expect(Money.rational(Money.new(10.0, 'USD'), Money.new(15.0, 'USD'))).to eq(Rational(2,3))

Money.cross_currency_deprecation do
Money.with_legacy_deprecations do
expect(Money).to receive(:deprecate).once
expect(Money.rational(Money.new(10.0, 'USD'), Money.new(15.0, 'JPY'))).to eq(Rational(2,3))
end
Expand Down Expand Up @@ -562,7 +568,7 @@
end

it "<=> issues deprecation warning when comparing incompatible currency" do
Money.cross_currency_deprecation do
Money.with_legacy_deprecations do
expect(Money).to receive(:deprecate).twice
expect(Money.new(1000, 'USD') <=> Money.new(2000, 'JPY')).to eq(-1)
expect(Money.new(2000, 'JPY') <=> Money.new(1000, 'USD')).to eq(1)
Expand Down Expand Up @@ -590,7 +596,7 @@

describe('legacy different currencies') do
around(:each) do |test|
Money.cross_currency_deprecation { test.run }
Money.with_legacy_deprecations { test.run }
end

it { expect(Money).to(receive(:deprecate).once); expect(cad_10 <=> usd_10).to(eq(0)) }
Expand Down

0 comments on commit 61c4e49

Please sign in to comment.