diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6ae1324b27..a7d84d9655 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -6,11 +6,6 @@ # Note that changes in the inspected code, or installation of new # versions of RuboCop, may require this file to be generated again. -# This cop supports safe autocorrection (--autocorrect). -Layout/MultilineBlockLayout: - Exclude: - - 'spec/money_spec.rb' - # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: AllowSafeAssignment. Lint/AssignmentInCondition: @@ -99,50 +94,6 @@ Metrics/ModuleLength: Metrics/PerceivedComplexity: Max: 16 -# This cop supports safe autocorrection (--autocorrect). -Naming/BinaryOperatorParameterName: - Exclude: - - 'lib/money/currency.rb' - - 'lib/money/money/arithmetic.rb' - -# This cop supports unsafe autocorrection (--autocorrect-all). -# Configuration parameters: EnforcedStyleForLeadingUnderscores. -# SupportedStylesForLeadingUnderscores: disallowed, required, optional -Naming/MemoizedInstanceVariableName: - Exclude: - - 'lib/money/bank/base.rb' - -# Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames. -# AllowedNames: as, at, by, cc, db, id, if, in, io, ip, of, on, os, pp, to -Naming/MethodParameterName: - Exclude: - - 'lib/money/bank/variable_exchange.rb' - -# Configuration parameters: Mode, AllowedMethods, AllowedPatterns, AllowBangMethods, WaywardPredicates. -# AllowedMethods: call -# WaywardPredicates: nonzero? -Naming/PredicateMethod: - Exclude: - - 'lib/money/currency.rb' - - 'lib/money/money/arithmetic.rb' - -# Configuration parameters: NamePrefix, ForbiddenPrefixes, AllowedMethods, MethodDefinitionMacros, UseSorbetSigs. -# NamePrefix: is_, has_, have_, does_ -# ForbiddenPrefixes: is_, has_, have_, does_ -# AllowedMethods: is_a? -# MethodDefinitionMacros: define_method, define_singleton_method -Naming/PredicatePrefix: - Exclude: - - 'spec/**/*' - - 'lib/money/money/formatting_rules.rb' - -# Configuration parameters: EnforcedStyle, CheckMethodNames, CheckSymbols, AllowedIdentifiers, AllowedPatterns. -# SupportedStyles: snake_case, normalcase, non_integer -# AllowedIdentifiers: TLS1_1, TLS1_2, capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339, x86_64 -Naming/VariableNumber: - Exclude: - - 'spec/money_spec.rb' - # Configuration parameters: Prefixes, AllowedPatterns. # Prefixes: when, with, without RSpec/ContextWording: diff --git a/lib/money/bank/base.rb b/lib/money/bank/base.rb index e5f735bffc..e0095b207c 100644 --- a/lib/money/bank/base.rb +++ b/lib/money/bank/base.rb @@ -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. diff --git a/lib/money/bank/variable_exchange.rb b/lib/money/bank/variable_exchange.rb index ea96d46a16..b142092f92 100644 --- a/lib/money/bank/variable_exchange.rb +++ b/lib/money/bank/variable_exchange.rb @@ -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 @@ -240,12 +240,12 @@ 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] @@ -253,13 +253,13 @@ def rates # @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 @@ -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) diff --git a/lib/money/currency.rb b/lib/money/currency.rb index 66e9a8a7ec..378b3de3f0 100644 --- a/lib/money/currency.rb +++ b/lib/money/currency.rb @@ -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) @@ -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) } @@ -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 @@ -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. # @@ -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] diff --git a/lib/money/money/arithmetic.rb b/lib/money/money/arithmetic.rb index ee722b04cc..d99086fc40 100644 --- a/lib/money/money/arithmetic.rb +++ b/lib/money/money/arithmetic.rb @@ -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 @@ -176,12 +176,12 @@ def negative? # @example # Money.new(100) * 2 #=> # # - 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 @@ -200,19 +200,19 @@ def *(value) # Money.new(100) / 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 @@ -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)+ @@ -338,9 +338,11 @@ def zero? # @example # Money.new(100).nonzero? #=> # # 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] diff --git a/lib/money/money/formatting_rules.rb b/lib/money/money/formatting_rules.rb index 416d3b5a1a..33ac2ccb19 100644 --- a/lib/money/money/formatting_rules.rb +++ b/lib/money/money/formatting_rules.rb @@ -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 diff --git a/sig/lib/money/bank/variable_exchange.rbs b/sig/lib/money/bank/variable_exchange.rbs index ebd24a058b..62c46843c2 100644 --- a/sig/lib/money/bank/variable_exchange.rbs +++ b/sig/lib/money/bank/variable_exchange.rbs @@ -173,12 +173,12 @@ 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] @@ -186,13 +186,13 @@ class Money # @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 \ No newline at end of file +end diff --git a/spec/money_spec.rb b/spec/money_spec.rb index 8d76c47eb4..5fb46ba2aa 100644 --- a/spec/money_spec.rb +++ b/spec/money_spec.rb @@ -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