Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nil to be used as a default_currency #1095

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Upcoming 7.0.0.alpha

- **Potential breaking change**: Fix USDC decimals places from 2 to 6
- Allow `nil` to be used as a default_currency

## 6.19.0

Expand Down
3 changes: 3 additions & 0 deletions lib/money/currency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def initialize(method, currency, attribute)
# Thrown when an unknown currency is requested.
class UnknownCurrency < ArgumentError; end

# Thrown when currency is not provided.
class NoCurrency < ArgumentError; end

class << self
def new(id)
id = id.to_s.downcase
Expand Down
9 changes: 7 additions & 2 deletions lib/money/money.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ def self.default_currency
@using_deprecated_default_currency = false
end

if @default_currency.respond_to?(:call)
if @default_currency.nil?
nil
elsif @default_currency.respond_to?(:call)
Money::Currency.new(@default_currency.call)
else
Money::Currency.new(@default_currency)
Expand Down Expand Up @@ -308,6 +310,8 @@ def self.from_amount(amount, currency = default_currency, options = {})
raise ArgumentError, "'amount' must be numeric" unless Numeric === amount

currency = Currency.wrap(currency) || Money.default_currency
raise Currency::NoCurrency, 'must provide a currency' if currency.nil?

value = amount.to_d * currency.subunit_to_unit
new(value, currency, options)
end
Expand Down Expand Up @@ -338,7 +342,7 @@ class << self
# Money.new(100, "USD") #=> #<Money @fractional=100 @currency="USD">
# Money.new(100, "EUR") #=> #<Money @fractional=100 @currency="EUR">
#
def initialize( obj, currency = Money.default_currency, options = {})
def initialize(obj, currency = Money.default_currency, options = {})
# For backwards compatability, if options is not a Hash, treat it as a bank parameter
unless options.is_a?(Hash)
options = { bank: options }
Expand All @@ -352,6 +356,7 @@ def initialize( obj, currency = Money.default_currency, options = {})

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

# Assuming using a currency using dollars:
Expand Down
6 changes: 6 additions & 0 deletions spec/currency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ def unregister_foo
end
end

describe "NoCurrency" do
it "is a subclass of ArgumentError" do
expect(described_class::NoCurrency < ArgumentError).to be true
end
end

describe ".find" do
before { register_foo }
after { unregister_foo }
Expand Down
15 changes: 15 additions & 0 deletions spec/money_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@
it "should have the default currency" do
expect(money.currency).to eq Money.default_currency
end

context 'without a default' do
around do |example|
default_currency = Money.default_currency
Money.default_currency = nil

example.run

Money.default_currency = default_currency
end

it 'should throw an NoCurrency Error' do
expect { money }.to raise_error(Money::Currency::NoCurrency)
end
end
end

context 'given a currency is provided' do
Expand Down