From a6389d00b5c9f2cd36327cdffe0945fe94318375 Mon Sep 17 00:00:00 2001 From: Zane Wolfgang Pickett Date: Wed, 24 Apr 2024 20:57:02 +0200 Subject: [PATCH 1/2] Allow nil to be used as a default_currency --- lib/money/currency.rb | 3 +++ lib/money/money.rb | 9 +++++++-- spec/currency_spec.rb | 6 ++++++ spec/money_spec.rb | 15 +++++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/money/currency.rb b/lib/money/currency.rb index b4e682b55b..d745fa6380 100644 --- a/lib/money/currency.rb +++ b/lib/money/currency.rb @@ -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 diff --git a/lib/money/money.rb b/lib/money/money.rb index faa0d269db..6bd2ad6e38 100644 --- a/lib/money/money.rb +++ b/lib/money/money.rb @@ -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) @@ -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 be provide a currency' if currency.nil? + value = amount.to_d * currency.subunit_to_unit new(value, currency, options) end @@ -338,7 +342,7 @@ class << self # Money.new(100, "USD") #=> # # Money.new(100, "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 } @@ -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 be provide a currency' if @currency.nil? end # Assuming using a currency using dollars: diff --git a/spec/currency_spec.rb b/spec/currency_spec.rb index 2e01018bbc..d0633d1ee4 100644 --- a/spec/currency_spec.rb +++ b/spec/currency_spec.rb @@ -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 } diff --git a/spec/money_spec.rb b/spec/money_spec.rb index bfba7b5255..1c0edd98ac 100644 --- a/spec/money_spec.rb +++ b/spec/money_spec.rb @@ -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 From c0ecce8a2de4d1e7494fe226e7958d749297fb41 Mon Sep 17 00:00:00 2001 From: Zane Pickett Date: Thu, 16 May 2024 10:08:03 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Sunny Ripert --- CHANGELOG.md | 1 + lib/money/money.rb | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58cdb538da..11e7c53052 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/money/money.rb b/lib/money/money.rb index 6bd2ad6e38..f0ec3552c5 100644 --- a/lib/money/money.rb +++ b/lib/money/money.rb @@ -310,7 +310,7 @@ 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 be provide a currency' if currency.nil? + raise Currency::NoCurrency, 'must provide a currency' if currency.nil? value = amount.to_d * currency.subunit_to_unit new(value, currency, options) @@ -356,7 +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 be provide a currency' if @currency.nil? + raise Currency::NoCurrency, 'must provide a currency' if @currency.nil? end # Assuming using a currency using dollars: