Skip to content

Commit d2e0005

Browse files
committed
stop setting USD as default currency
1 parent c5a365d commit d2e0005

File tree

8 files changed

+13
-28
lines changed

8 files changed

+13
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- **Breaking change**: Remove deprecated methods:
77
- `Money.infinite_precision`
88
- `Money.infinite_precision=`
9+
- **Breaking change**: Default currency is now `nil` instead of `USD`. If you want to keep the previous behavior, set `Money.default_currency = Money::Currency.find("USD")` in your initializer. Initializing a Money object will raise a `Currency::NoCurrency` if no currency is set.
910
- **Potential breaking change**: Fix RSD (Serbian Dinar) formatting to be like `12.345,42 RSD`
1011
- **Potential breaking change**: Fix USDC decimals places from 2 to 6
1112
- **Potential breaking change**: Fix MGA (Malagasy Ariary) to be a zero-decimal currency (changing subunit_to_unit from 5 to 1)

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ all_currencies(Money::Currency.table)
192192

193193
### Default Currency
194194

195-
By default `Money` defaults to USD as its currency. This can be overwritten
196-
using:
195+
A default currency is not set by default. If a default currency is not set, it will raise an error when you try to initialize a `Money` object without explicitly passing a currency or parse a string that does not contain a currency. You can set a default currency for your application by using:
197196

198197
``` ruby
199198
Money.default_currency = Money::Currency.new("CAD")

lib/money/money.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,6 @@ class << self
141141
# default value is Currency.new("USD"). The value must be a valid
142142
# +Money::Currency+ instance.
143143
def self.default_currency
144-
if @using_deprecated_default_currency
145-
warn '[WARNING] The default currency will change from `USD` to `nil` in the next major release. Make ' \
146-
'sure to set it explicitly using `Money.default_currency=` to avoid potential issues'
147-
@using_deprecated_default_currency = false
148-
end
149-
150144
if @default_currency.nil?
151145
nil
152146
elsif @default_currency.respond_to?(:call)
@@ -157,7 +151,6 @@ def self.default_currency
157151
end
158152

159153
def self.default_currency=(currency)
160-
@using_deprecated_default_currency = false
161154
@default_currency = currency
162155
end
163156

@@ -193,10 +186,6 @@ def self.setup_defaults
193186
# Set the default bank for creating new +Money+ objects.
194187
self.default_bank = Bank::VariableExchange.instance
195188

196-
# Set the default currency for creating new +Money+ object.
197-
self.default_currency = Currency.new("USD")
198-
@using_deprecated_default_currency = true
199-
200189
# Default to using i18n
201190
@use_i18n = true
202191

@@ -333,7 +322,7 @@ class << self
333322
# Money.new(100, "USD") #=> #<Money @fractional=100 @currency="USD">
334323
# Money.new(100, "EUR") #=> #<Money @fractional=100 @currency="EUR">
335324
#
336-
def initialize(obj, currency = Money.default_currency, options = {})
325+
def initialize(obj, currency = nil, options = {})
337326
# For backwards compatibility, if options is not a Hash, treat it as a bank parameter
338327
unless options.is_a?(Hash)
339328
options = { bank: options }

sig/lib/money/money.rbs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class Money
2020

2121
@default_currency: Money::Currency
2222
@fractional: BigDecimal
23-
@using_deprecated_default_currency: bool
2423

2524
# Convenience method for fractional part of the amount. Synonym of #fractional
2625
#

spec/money/arithmetic_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@
239239
expect { Money.new(10_00) + nil }.to raise_error(TypeError, "Unsupported argument type: NilClass")
240240
end
241241

242-
it_behaves_like 'instance with custom bank', :+, Money.new(1)
242+
it_behaves_like 'instance with custom bank', :+, -> { Money.new(1) }
243243
end
244244

245245
describe "#-" do
@@ -270,7 +270,7 @@
270270
expect { Money.new(10_00) - nil }.to raise_error(TypeError, "Unsupported argument type: NilClass")
271271
end
272272

273-
it_behaves_like 'instance with custom bank', :-, Money.new(1)
273+
it_behaves_like 'instance with custom bank', :-, -> { Money.new(1) }
274274
end
275275

276276
describe "#*" do
@@ -519,7 +519,7 @@
519519
expect(special_money_class.new(10_00, "USD").divmod(special_money_class.new(4_00)).last).to be_a special_money_class
520520
end
521521

522-
it_behaves_like 'instance with custom bank', :divmod, Money.new(1)
522+
it_behaves_like 'instance with custom bank', :divmod, -> { Money.new(1) }
523523
it_behaves_like 'instance with custom bank', :divmod, 1
524524
end
525525

spec/money_spec.rb

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -952,15 +952,6 @@ def m.amount
952952
expect(Money.default_currency).to eq Money::Currency.new(:eur)
953953
end
954954

955-
it 'warns about changing default_currency value' do
956-
expect(Money)
957-
.to receive(:warn)
958-
.with('[WARNING] The default currency will change from `USD` to `nil` in the next major release. ' \
959-
'Make sure to set it explicitly using `Money.default_currency=` to avoid potential issues')
960-
961-
Money.default_currency
962-
end
963-
964955
it 'does not warn if the default_currency has been changed' do
965956
Money.default_currency = Money::Currency.new(:usd)
966957

spec/spec_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@
112112
# test failures related to randomization by passing the same `--seed` value
113113
# as the one that triggered the failure.
114114
Kernel.srand config.seed
115+
116+
# Code to run once before the entire test suite
117+
config.before(:suite) do
118+
Money.default_currency = Money::Currency.new("USD")
119+
end
115120
end
116121

117122
def reset_i18n

spec/support/shared_examples/money_examples.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
RSpec.shared_examples 'instance with custom bank' do |operation, value|
44
let(:custom_bank) { Money::Bank::VariableExchange.new }
55
let(:instance) { Money.new(1, :usd, custom_bank) }
6+
let(:evaluated_value) { value.respond_to?(:call) ? value.call : value }
67

7-
subject { value ? instance.send(operation, value) : instance.send(operation) }
8+
subject { evaluated_value ? instance.send(operation, evaluated_value) : instance.send(operation) }
89

910
it "returns custom bank from new instance" do
1011
new_money_instances = Array(subject).select { |el| el.is_a?(Money) }

0 commit comments

Comments
 (0)