Skip to content

Commit 5c71447

Browse files
sirwolfgangsunny
andcommitted
Allow nil to be used as a default_currency
Co-authored-by: Sunny Ripert <sunny@sunfox.org>
1 parent bb12d44 commit 5c71447

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
@@ -13,6 +13,7 @@
1313
- Update thousands_separator for CHF
1414
- Add Caribbean Guilder (XCG) as replacement for Netherlands Antillean Gulden (ANG)
1515
- Add `Currency#cents_based?` to check if currency is cents-based
16+
- Allow `nil` to be used as a default_currency
1617

1718
## 6.19.0
1819

lib/money/currency.rb

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

34+
# Thrown when currency is not provided.
35+
class NoCurrency < ArgumentError; end
36+
3437
class << self
3538
def new(id)
3639
id = id.to_s.downcase

lib/money/money.rb

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

158-
if @default_currency.respond_to?(:call)
158+
if @default_currency.nil?
159+
nil
160+
elsif @default_currency.respond_to?(:call)
159161
Money::Currency.new(@default_currency.call)
160162
else
161163
Money::Currency.new(@default_currency)
@@ -307,6 +309,8 @@ def self.from_amount(amount, currency = default_currency, options = {})
307309
raise ArgumentError, "'amount' must be numeric" unless Numeric === amount
308310

309311
currency = Currency.wrap(currency) || Money.default_currency
312+
raise Currency::NoCurrency, 'must provide a currency' if currency.nil?
313+
310314
value = amount.to_d * currency.subunit_to_unit
311315
new(value, currency, options)
312316
end
@@ -337,7 +341,7 @@ class << self
337341
# Money.new(100, "USD") #=> #<Money @fractional=100 @currency="USD">
338342
# Money.new(100, "EUR") #=> #<Money @fractional=100 @currency="EUR">
339343
#
340-
def initialize( obj, currency = Money.default_currency, options = {})
344+
def initialize(obj, currency = Money.default_currency, options = {})
341345
# For backwards compatibility, if options is not a Hash, treat it as a bank parameter
342346
unless options.is_a?(Hash)
343347
options = { bank: options }
@@ -351,6 +355,7 @@ def initialize( obj, currency = Money.default_currency, options = {})
351355

352356
# BigDecimal can be Infinity and NaN, money of that amount does not make sense
353357
raise ArgumentError, 'must be initialized with a finite value' unless @fractional.finite?
358+
raise Currency::NoCurrency, 'must provide a currency' if @currency.nil?
354359
end
355360

356361
# Assuming using a currency using dollars:

spec/currency_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ def unregister_foo
1919
end
2020
end
2121

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

spec/money_spec.rb

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

7388
context 'given a currency is provided' do

0 commit comments

Comments
 (0)