Skip to content

Commit 1d583ef

Browse files
authored
Merge pull request #1095 from quiltt/main
Allow nil to be used as a default_currency
2 parents b03e6d8 + 5c71447 commit 1d583ef

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- Update thousands_separator for CHF
1515
- Add Caribbean Guilder (XCG) as replacement for Netherlands Antillean Gulden (ANG)
1616
- Add `Currency#cents_based?` to check if currency is cents-based
17+
- Allow `nil` to be used as a default_currency
1718

1819
## 6.19.0
1920

lib/money/currency.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ def initialize(method, currency, attribute)
3333
# Thrown when an unknown currency is requested.
3434
class UnknownCurrency < ArgumentError; end
3535

36+
# Thrown when currency is not provided.
37+
class NoCurrency < ArgumentError; end
38+
3639
class << self
3740
def new(id)
3841
id = id.to_s.downcase

lib/money/money.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ def self.default_currency
157157
@using_deprecated_default_currency = false
158158
end
159159

160-
if @default_currency.respond_to?(:call)
160+
if @default_currency.nil?
161+
nil
162+
elsif @default_currency.respond_to?(:call)
161163
Money::Currency.new(@default_currency.call)
162164
else
163165
Money::Currency.new(@default_currency)
@@ -309,6 +311,8 @@ def self.from_amount(amount, currency = default_currency, options = {})
309311
raise ArgumentError, "'amount' must be numeric" unless Numeric === amount
310312

311313
currency = Currency.wrap(currency) || Money.default_currency
314+
raise Currency::NoCurrency, 'must provide a currency' if currency.nil?
315+
312316
value = amount.to_d * currency.subunit_to_unit
313317
new(value, currency, options)
314318
end
@@ -339,7 +343,7 @@ class << self
339343
# Money.new(100, "USD") #=> #<Money @fractional=100 @currency="USD">
340344
# Money.new(100, "EUR") #=> #<Money @fractional=100 @currency="EUR">
341345
#
342-
def initialize( obj, currency = Money.default_currency, options = {})
346+
def initialize(obj, currency = Money.default_currency, options = {})
343347
# For backwards compatibility, if options is not a Hash, treat it as a bank parameter
344348
unless options.is_a?(Hash)
345349
options = { bank: options }
@@ -353,6 +357,7 @@ def initialize( obj, currency = Money.default_currency, options = {})
353357

354358
# BigDecimal can be Infinity and NaN, money of that amount does not make sense
355359
raise ArgumentError, 'must be initialized with a finite value' unless @fractional.finite?
360+
raise Currency::NoCurrency, 'must provide a currency' if @currency.nil?
356361
end
357362

358363
# Assuming using a currency using dollars:

spec/currency_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def unregister_foo
2121
end
2222
end
2323

24+
describe "NoCurrency" do
25+
it "is a subclass of ArgumentError" do
26+
expect(described_class::NoCurrency < ArgumentError).to be true
27+
end
28+
end
29+
2430
describe ".find" do
2531
before { register_foo }
2632
after { unregister_foo }

spec/money_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,21 @@
7070
it "should have the default currency" do
7171
expect(money.currency).to eq Money.default_currency
7272
end
73+
74+
context 'without a default' do
75+
around do |example|
76+
default_currency = Money.default_currency
77+
Money.default_currency = nil
78+
79+
example.run
80+
81+
Money.default_currency = default_currency
82+
end
83+
84+
it 'should throw an NoCurrency Error' do
85+
expect { money }.to raise_error(Money::Currency::NoCurrency)
86+
end
87+
end
7388
end
7489

7590
context 'given a currency is provided' do

0 commit comments

Comments
 (0)