Skip to content

Commit 3d37755

Browse files
committed
Allow nil to be used as a default_currency
1 parent 37e183a commit 3d37755

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

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: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ def self.default_currency
156156
@using_deprecated_default_currency = false
157157
end
158158

159-
if @default_currency.respond_to?(:call)
159+
if @default_currency.nil?
160+
nil
161+
elsif @default_currency.respond_to?(:call)
160162
Money::Currency.new(@default_currency.call)
161163
else
162164
Money::Currency.new(@default_currency)
@@ -338,7 +340,7 @@ class << self
338340
# Money.new(100, "USD") #=> #<Money @fractional=100 @currency="USD">
339341
# Money.new(100, "EUR") #=> #<Money @fractional=100 @currency="EUR">
340342
#
341-
def initialize( obj, currency = Money.default_currency, options = {})
343+
def initialize(obj, currency = Money.default_currency, options = {})
342344
# For backwards compatability, if options is not a Hash, treat it as a bank parameter
343345
unless options.is_a?(Hash)
344346
options = { bank: options }
@@ -352,6 +354,7 @@ def initialize( obj, currency = Money.default_currency, options = {})
352354

353355
# BigDecimal can be Infinity and NaN, money of that amount does not make sense
354356
raise ArgumentError, 'must be initialized with a finite value' unless @fractional.finite?
357+
raise Currency::NoCurrency, 'must be provide a currency' if @currency.nil?
355358
end
356359

357360
# 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)