Skip to content
Merged
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
49 changes: 0 additions & 49 deletions .rubocop_todo.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/money/bank/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Base
#
# @return [Money::Bank::Base]
def self.instance
@singleton ||= self.new
@instance ||= self.new
end

# The rounding method to use when exchanging rates.
Expand Down
18 changes: 9 additions & 9 deletions lib/money/bank/variable_exchange.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ class VariableExchange < Base
# @param [RateStore] st An exchange rate store, used to persist exchange rate pairs.
# @yield [n] Optional block to use when rounding after exchanging one
# currency for another. See +Money::bank::base+
def initialize(st = Money::RatesStore::Memory.new, &)
@store = st
def initialize(store = Money::RatesStore::Memory.new, &)
@store = store
super(&)
end

Expand Down Expand Up @@ -240,26 +240,26 @@ def rates
end
end

# Loads rates provided in +s+ given the specified format. Available
# Loads rates provided in +string+ given the specified format. Available
# formats are +:json+, +:ruby+ and +:yaml+.
# Delegates to +Money::RatesStore::Memory+
#
# @param [Symbol] format The format of +s+.
# @param [String] s The rates string.
# @param [Symbol] format The format of +string+.
# @param [String] string The rates string.
# @param [Hash] opts Options hash to set special parameters. Backwards compatibility only.
#
# @return [self]
#
# @raise +Money::Bank::UnknownRateFormat+ if format is unknown.
#
# @example
# s = "{\"USD_TO_CAD\":1.24515,\"CAD_TO_USD\":0.803115}"
# string = "{\"USD_TO_CAD\":1.24515,\"CAD_TO_USD\":0.803115}"
# bank = Money::Bank::VariableExchange.new
# bank.import_rates(:json, s)
# bank.import_rates(:json, string)
#
# bank.get_rate("USD", "CAD") #=> 1.24515
# bank.get_rate("CAD", "USD") #=> 0.803115
def import_rates(format, s, opts = {})
def import_rates(format, string, opts = {})
raise Money::Bank::UnknownRateFormat unless RATE_FORMATS.include?(format)

if format == :ruby
Expand All @@ -269,7 +269,7 @@ def import_rates(format, s, opts = {})
end

store.transaction do
data = FORMAT_SERIALIZERS[format].load(s)
data = FORMAT_SERIALIZERS[format].load(string)

data.each do |key, rate|
from, to = key.split(SERIALIZER_SEPARATOR)
Expand Down
34 changes: 19 additions & 15 deletions lib/money/currency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ def inherit(parent_iso_code, curr)
#
# @return [Boolean] true if the currency previously existed, false
# if it didn't.
# rubocop:disable Naming/PredicateMethod
def unregister(curr)
key =
if curr.is_a?(Hash)
Expand All @@ -207,6 +208,7 @@ def unregister(curr)
@stringified_keys = nil if existed
existed ? true : false
end
# rubocop:enable Naming/PredicateMethod

def each
all.each { |c| yield(c) }
Expand Down Expand Up @@ -307,12 +309,12 @@ def initialize(id)
# c1 <=> c2 #=> 1
# c2 <=> c1 #=> -1
# c1 <=> c1 #=> 0
def <=>(other_currency)
def <=>(other)
# <=> returns nil when one of the values is nil
comparison = self.priority <=> other_currency.priority || 0
comparison = self.priority <=> other.priority || 0

if comparison == 0
self.id <=> other_currency.id
self.id <=> other.id
else
comparison
end
Expand All @@ -330,20 +332,10 @@ def <=>(other_currency)
# c2 = Money::Currency.new(:jpy)
# c1 == c1 #=> true
# c1 == c2 #=> false
def ==(other_currency)
self.equal?(other_currency) || compare_ids(other_currency)
def ==(other)
self.equal?(other) || compare_ids(other)
end

def compare_ids(other_currency)
other_currency_id = if other_currency.is_a?(Currency)
other_currency.id.to_s.downcase
else
other_currency.to_s.downcase
end
self.id.to_s.downcase == other_currency_id
end
private :compare_ids

# Returns a Integer hash value based on the +id+ attribute in order to use
# functions like & (intersection), group_by, etc.
#
Expand Down Expand Up @@ -457,6 +449,18 @@ def exponent

private

# rubocop:disable Naming/PredicateMethod
def compare_ids(other_currency)
other_currency_id =
if other_currency.is_a?(Currency)
other_currency.id.to_s.downcase
else
other_currency.to_s.downcase
end
id.to_s.downcase == other_currency_id
end
# rubocop:enable Naming/PredicateMethod

def initialize_data!
data = self.class.table[@id]
@alternate_symbols = data[:alternate_symbols]
Expand Down
40 changes: 21 additions & 19 deletions lib/money/money/arithmetic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ def -@
# Money.new(1_00).eql?("1.00") #=> false
#
# @see Money.strict_eql_compare
def eql?(other_money)
if other_money.is_a?(Money)
if !Money.strict_eql_compare && fractional == 0 && other_money.fractional == 0
def eql?(other)
if other.is_a?(Money)
if !Money.strict_eql_compare && fractional == 0 && other.fractional == 0
warn "[DEPRECATION] Comparing 0 #{currency} with 0 " \
"#{other_money.currency} using `#eql?` will return false in " \
"#{other.currency} using `#eql?` will return false in " \
"future versions of Money. Opt-in to the new behavior by " \
"setting `Money.strict_eql_compare = true`."
return true
end

fractional == other_money.fractional && currency == other_money.currency
fractional == other.fractional && currency == other.currency
else
false
end
Expand Down Expand Up @@ -176,12 +176,12 @@ def negative?
# @example
# Money.new(100) * 2 #=> #<Money @fractional=200>
#
def *(value)
value = value.value if value.is_a?(CoercedNumeric)
if value.is_a? Numeric
dup_with(fractional: fractional * value)
def *(other)
other = other.value if other.is_a?(CoercedNumeric)
if other.is_a? Numeric
dup_with(fractional: fractional * other)
else
raise TypeError, "Can't multiply a #{self.class.name} by a #{value.class.name}'s value"
raise TypeError, "Can't multiply a #{self.class.name} by a #{other.class.name}'s value"
end
end

Expand All @@ -200,19 +200,19 @@ def *(value)
# Money.new(100) / 10 #=> #<Money @fractional=10>
# Money.new(100) / Money.new(10) #=> 10.0
#
def /(value)
if value.is_a?(self.class)
exchanged = value.exchange_to(currency)
def /(other)
if other.is_a?(self.class)
exchanged = other.exchange_to(currency)
raise ZeroDivisionError, "divided by Money(0)" if exchanged.zero?

fractional / as_d(exchanged.fractional).to_f
else
raise TypeError, 'Can not divide by Money' if value.is_a?(CoercedNumeric)
raise TypeError, 'Can not divide by Money' if other.is_a?(CoercedNumeric)

value = as_d(value)
raise ZeroDivisionError, "divided by zero" if value.zero?
other = as_d(other)
raise ZeroDivisionError, "divided by zero" if other.zero?

dup_with(fractional: fractional / value)
dup_with(fractional: fractional / other)
end
end

Expand Down Expand Up @@ -285,8 +285,8 @@ def modulo(val)
# @return [Money]
#
# @see #modulo
def %(val)
modulo(val)
def %(other)
modulo(other)
end

# If different signs +self.modulo(val) - val+ otherwise +self.modulo(val)+
Expand Down Expand Up @@ -338,9 +338,11 @@ def zero?
# @example
# Money.new(100).nonzero? #=> #<Money @fractional=100>
# Money.new(0).nonzero? #=> nil
# rubocop:disable Naming/PredicateMethod
def nonzero?
fractional != 0 ? self : nil
end
# rubocop:enable Naming/PredicateMethod

# Used to make Money instance handle the operations when arguments order is reversed
# @return [Array]
Expand Down
2 changes: 2 additions & 0 deletions lib/money/money/formatting_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ def [](key)
@rules[key]
end

# rubocop:disable Naming/PredicatePrefix
def has_key?(key)
@rules.has_key? key
end
# rubocop:enable Naming/PredicatePrefix

private

Expand Down
14 changes: 7 additions & 7 deletions sig/lib/money/bank/variable_exchange.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -173,26 +173,26 @@ class Money
# This should be deprecated.
def rates: () -> untyped

# Loads rates provided in +s+ given the specified format. Available
# Loads rates provided in +string+ given the specified format. Available
# formats are +:json+, +:ruby+ and +:yaml+.
# Delegates to +Money::RatesStore::Memory+
#
# @param [Symbol] format The format of +s+.
# @param [String] s The rates string.
# @param [Symbol] format The format of +string+.
# @param [String] string The rates string.
# @param [Hash] opts Options hash to set special parameters. Backwards compatibility only.
#
# @return [self]
#
# @raise +Money::Bank::UnknownRateFormat+ if format is unknown.
#
# @example
# s = "{\"USD_TO_CAD\":1.24515,\"CAD_TO_USD\":0.803115}"
# string = "{\"USD_TO_CAD\":1.24515,\"CAD_TO_USD\":0.803115}"
# bank = Money::Bank::VariableExchange.new
# bank.import_rates(:json, s)
# bank.import_rates(:json, string)
#
# bank.get_rate("USD", "CAD") #=> 1.24515
# bank.get_rate("CAD", "USD") #=> 0.803115
def import_rates: (Symbol format, string s, ?::Hash[untyped, untyped] opts) -> Money::Bank::VariableExchange
def import_rates: (Symbol format, string string, ?::Hash[untyped, untyped] opts) -> Money::Bank::VariableExchange
end
end
end
end
6 changes: 3 additions & 3 deletions spec/money_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -982,10 +982,10 @@
end

it "does not accumulate rounding error" do
money_1 = Money.new(10.9).round(BigDecimal::ROUND_DOWN)
money_2 = Money.new(10.9).round(BigDecimal::ROUND_DOWN)
money1 = Money.new(10.9).round(BigDecimal::ROUND_DOWN)
money2 = Money.new(10.9).round(BigDecimal::ROUND_DOWN)

expect(money_1 + money_2).to eq(Money.new(20))
expect(money1 + money2).to eq(Money.new(20))
end
end

Expand Down