Skip to content

Commit

Permalink
Resolve Rubocop layout offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
tweymuth committed Aug 16, 2019
1 parent 00a4845 commit 2e61d95
Show file tree
Hide file tree
Showing 21 changed files with 182 additions and 210 deletions.
3 changes: 0 additions & 3 deletions lib/money/bank/base.rb
Expand Up @@ -2,7 +2,6 @@ class Money
# Provides classes that aid in the ability of exchange one currency with
# another.
module Bank

# The lowest Money::Bank error class.
# All Money::Bank errors should inherit from it.
class Error < StandardError
Expand All @@ -13,7 +12,6 @@ class Error < StandardError
class UnknownRate < Error
end


# Money::Bank::Base is the basic interface for creating a money exchange
# object, also called Bank.
#
Expand All @@ -38,7 +36,6 @@ class UnknownRate < Error
# +Money::Bank+ class. You can also override +#setup+ instead of
# +#initialize+ to setup initial variables, etc.
class Base

# Returns the singleton instance of the Base bank.
#
# @return [Money::Bank::Base]
Expand Down
1 change: 0 additions & 1 deletion lib/money/bank/single_currency.rb
Expand Up @@ -13,7 +13,6 @@ class DifferentCurrencyError < Error; end
# where exchanges happen are erroneous. Using this as the default bank
# means that that these mistakes don't silently do the wrong thing.
class SingleCurrency < Base

# Raises a DifferentCurrencyError to remove possibility of accidentally
# exchanging currencies
def exchange_with(from, to_currency, &block)
Expand Down
7 changes: 3 additions & 4 deletions lib/money/bank/variable_exchange.rb
Expand Up @@ -41,13 +41,12 @@ class UnknownRateFormat < StandardError; end
# # Get rate from redis
# bank.get_rate 'USD', 'CAD'
class VariableExchange < Base

attr_reader :mutex, :store

# Available formats for importing/exporting rates.
RATE_FORMATS = [:json, :ruby, :yaml].freeze
SERIALIZER_SEPARATOR = '_TO_'.freeze
FORMAT_SERIALIZERS = {json: JSON, ruby: Marshal, yaml: YAML}.freeze
FORMAT_SERIALIZERS = { json: JSON, ruby: Marshal, yaml: YAML }.freeze

# Initializes a new +Money::Bank::VariableExchange+ object.
# It defaults to using an in-memory, thread safe store instance for
Expand Down Expand Up @@ -220,7 +219,7 @@ def export_rates(format, file = nil, opts = {})
s = FORMAT_SERIALIZERS[format].dump(rates)

unless file.nil?
File.open(file, "w") {|f| f.write(s) }
File.open(file, "w") { |f| f.write(s) }
end

s
Expand All @@ -229,7 +228,7 @@ def export_rates(format, file = nil, opts = {})

# This should be deprecated.
def rates
store.each_rate.each_with_object({}) do |(from,to,rate),hash|
store.each_rate.each_with_object({}) do |(from, to, rate), hash|
hash[[from, to].join(SERIALIZER_SEPARATOR)] = rate
end
end
Expand Down
24 changes: 12 additions & 12 deletions lib/money/currency.rb
Expand Up @@ -5,7 +5,6 @@
require "money/currency/heuristics"

class Money

# Represents a specific currency unit.
#
# @see https://en.wikipedia.org/wiki/Currency
Expand Down Expand Up @@ -80,6 +79,7 @@ def find(id)
def find_by_iso_numeric(num)
num = num.to_s.rjust(3, '0')
return if num.empty?

id, _ = self.table.find { |key, currency| currency[:iso_numeric] == num }
new(id)
rescue UnknownCurrency
Expand Down Expand Up @@ -136,6 +136,7 @@ def all
if c.priority.nil?
raise MissingAttributeError.new(:all, c.id, :priority)
end

c
end.sort_by(&:priority)
end
Expand Down Expand Up @@ -206,7 +207,6 @@ def each
all.each { |c| yield(c) }
end


private

def stringify_keys
Expand Down Expand Up @@ -252,8 +252,8 @@ def stringify_keys
# this currency)

attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol,
:disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark,
:thousands_separator, :symbol_first, :smallest_denomination
:disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark,
:thousands_separator, :symbol_first, :smallest_denomination

alias_method :separator, :decimal_mark
alias_method :delimiter, :thousands_separator
Expand Down Expand Up @@ -427,14 +427,14 @@ def exponent

def initialize_data!
data = self.class.table[@id]
@alternate_symbols = data[:alternate_symbols]
@decimal_mark = data[:decimal_mark]
@disambiguate_symbol = data[:disambiguate_symbol]
@html_entity = data[:html_entity]
@iso_code = data[:iso_code]
@iso_numeric = data[:iso_numeric]
@name = data[:name]
@priority = data[:priority]
@alternate_symbols = data[:alternate_symbols]
@decimal_mark = data[:decimal_mark]
@disambiguate_symbol = data[:disambiguate_symbol]
@html_entity = data[:html_entity]
@iso_code = data[:iso_code]
@iso_numeric = data[:iso_numeric]
@name = data[:name]
@priority = data[:priority]
@smallest_denomination = data[:smallest_denomination]
@subunit = data[:subunit]
@subunit_to_unit = data[:subunit_to_unit]
Expand Down
6 changes: 3 additions & 3 deletions lib/money/money.rb
@@ -1,4 +1,5 @@
# encoding: utf-8

require "money/bank/variable_exchange"
require "money/bank/single_currency"
require "money/money/arithmetic"
Expand Down Expand Up @@ -89,7 +90,6 @@ def round_to_nearest_cash_value

# Class Methods
class << self

# @!attribute [rw] default_bank
# @return [Money::Bank::Base] Each Money object is associated to a bank
# object, which is responsible for currency exchange. This property
Expand Down Expand Up @@ -121,8 +121,8 @@ class << self
# @return [Integer] Use this to specify precision for converting Rational
# to BigDecimal
attr_accessor :default_bank, :default_formatting_rules,
:use_i18n, :infinite_precision, :conversion_precision,
:locale_backend
:use_i18n, :infinite_precision, :conversion_precision,
:locale_backend
end

# @!attribute default_currency
Expand Down
6 changes: 6 additions & 0 deletions lib/money/money/arithmetic.rb
Expand Up @@ -55,9 +55,11 @@ def eql?(other_money)
def <=>(other)
unless other.is_a?(Money)
return unless other.respond_to?(:zero?) && other.zero?

return other.is_a?(CoercedNumeric) ? 0 <=> fractional : fractional <=> 0
end
return 0 if zero? && other.zero?

other = other.exchange_to(currency)
fractional <=> other.fractional
rescue Money::Bank::UnknownRate
Expand All @@ -69,6 +71,7 @@ def ==(other)
if other.is_a?(Numeric) && !other.zero?
raise ArgumentError, 'Money#== supports only zero numerics'
end

super
end

Expand Down Expand Up @@ -135,9 +138,11 @@ def negative?
self.class.new(new_fractional, currency, bank)
when CoercedNumeric
raise TypeError, non_zero_message.call(other.value) unless other.zero?

self.class.new(other.value.public_send(op, fractional), currency)
when Numeric
raise TypeError, non_zero_message.call(other) unless other.zero?

self
else
raise TypeError, "Unsupported argument type: #{other.class.name}"
Expand Down Expand Up @@ -188,6 +193,7 @@ def /(value)
fractional / as_d(value.exchange_to(currency).fractional).to_f
else
raise TypeError, 'Can not divide by Money' if value.is_a?(CoercedNumeric)

self.class.new(fractional / as_d(value), currency, bank)
end
end
Expand Down
6 changes: 0 additions & 6 deletions lib/money/money/constructors.rb
@@ -1,6 +1,5 @@
class Money
module Constructors

# Create a new money object with value 0.
#
# @param [Currency, String, Symbol] currency The currency to use.
Expand All @@ -14,7 +13,6 @@ def empty(currency = default_currency)
end
alias_method :zero, :empty


# Creates a new Money object of the given value, using the Canadian
# dollar currency.
#
Expand All @@ -31,7 +29,6 @@ def ca_dollar(cents)
end
alias_method :cad, :ca_dollar


# Creates a new Money object of the given value, using the American dollar
# currency.
#
Expand All @@ -48,7 +45,6 @@ def us_dollar(cents)
end
alias_method :usd, :us_dollar


# Creates a new Money object of the given value, using the Euro currency.
#
# @param [Integer] cents The cents value.
Expand All @@ -64,7 +60,6 @@ def euro(cents)
end
alias_method :eur, :euro


# Creates a new Money object of the given value, in British pounds.
#
# @param [Integer] pence The pence value.
Expand All @@ -79,6 +74,5 @@ def pound_sterling(pence)
new(pence, "GBP")
end
alias_method :gbp, :pound_sterling

end
end
2 changes: 2 additions & 0 deletions lib/money/money/formatter.rb
@@ -1,4 +1,5 @@
# encoding: UTF-8

require 'money/money/formatting_rules'

class Money
Expand Down Expand Up @@ -229,6 +230,7 @@ def initialize(money, *rules)

def to_s
return free_text if show_free_text?

result = format_number
formatted = append_sign(result)
append_currency_symbol(formatted)
Expand Down
3 changes: 1 addition & 2 deletions lib/money/rates_store/memory.rb
@@ -1,6 +1,5 @@
class Money
module RatesStore

# Class for thread-safe storage of exchange rate pairs.
# Used by instances of +Money::Bank::VariableExchange+.
#
Expand Down Expand Up @@ -91,7 +90,7 @@ def each_rate(&block)
enum = Enumerator.new do |yielder|
index.each do |key, rate|
iso_from, iso_to = key.split(INDEX_KEY_SEPARATOR)
yielder.yield iso_from, iso_to, rate
yielder.yield iso_from, iso_to, rate
end
end

Expand Down
1 change: 1 addition & 0 deletions money.gemspec
@@ -1,4 +1,5 @@
# -*- encoding: utf-8 -*-

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "money/version"
Expand Down
1 change: 0 additions & 1 deletion spec/bank/base_spec.rb
@@ -1,5 +1,4 @@
describe Money::Bank::Base do

describe ".instance" do
it "is local to one class" do
subclass = Class.new(described_class)
Expand Down
15 changes: 6 additions & 9 deletions spec/bank/variable_exchange_spec.rb
Expand Up @@ -2,7 +2,6 @@
require 'yaml'

describe Money::Bank::VariableExchange do

describe "#initialize" do
context "without &block" do
let(:bank) {
Expand Down Expand Up @@ -52,12 +51,12 @@
expect { bank.exchange_with(Money.new(100, 'USD'), 'JPY') }.to raise_exception(Money::Bank::UnknownRate)
end

#it "rounds the exchanged result down" do
# it "rounds the exchanged result down" do
# bank.add_rate("USD", "EUR", 0.788332676)
# bank.add_rate("EUR", "YEN", 122.631477)
# expect(bank.exchange_with(Money.new(10_00, "USD"), "EUR")).to eq Money.new(788, "EUR")
# expect(bank.exchange_with(Money.new(500_00, "EUR"), "YEN")).to eq Money.new(6131573, "YEN")
#end
# end

it "accepts a custom truncation method" do
proc = Proc.new { |n| n.ceil }
Expand Down Expand Up @@ -183,7 +182,7 @@

context "with unknown format" do
it "raises Money::Bank::UnknownRateFormat" do
expect { subject.export_rates(:foo)}.to raise_error Money::Bank::UnknownRateFormat
expect { subject.export_rates(:foo) }.to raise_error Money::Bank::UnknownRateFormat
end
end

Expand All @@ -201,7 +200,6 @@
expect(subject.store).to receive(:transaction)
subject.export_rates(:yaml, nil, foo: 1)
end

end

describe "#import_rates" do
Expand All @@ -216,7 +214,7 @@

context "with format == :ruby" do
it "loads the rates provided" do
s = Marshal.dump({"USD_TO_EUR"=>1.25,"USD_TO_JPY"=>2.55})
s = Marshal.dump({ "USD_TO_EUR" => 1.25, "USD_TO_JPY" => 2.55 })
subject.import_rates(:ruby, s)
expect(subject.get_rate('USD', 'EUR')).to eq 1.25
expect(subject.get_rate('USD', 'JPY')).to eq 2.55
Expand All @@ -234,7 +232,7 @@

context "with unknown format" do
it "raises Money::Bank::UnknownRateFormat" do
expect { subject.import_rates(:foo, "")}.to raise_error Money::Bank::UnknownRateFormat
expect { subject.import_rates(:foo, "") }.to raise_error Money::Bank::UnknownRateFormat
end
end

Expand All @@ -243,12 +241,11 @@
s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n"
subject.import_rates(:yaml, s, foo: 1)
end

end

describe "#marshal_dump" do
it "does not raise an error" do
expect { Marshal.dump(subject) }.to_not raise_error
expect { Marshal.dump(subject) }.to_not raise_error
end

it "works with Marshal.load" do
Expand Down
2 changes: 1 addition & 1 deletion spec/currency/heuristics_spec.rb
Expand Up @@ -5,7 +5,7 @@
let(:it) { Money::Currency }

it "it raises deprecation error" do
expect{ it.analyze('123') }.to raise_error(StandardError, 'Heuristics deprecated, add `gem "money-heuristics"` to Gemfile')
expect { it.analyze('123') }.to raise_error(StandardError, 'Heuristics deprecated, add `gem "money-heuristics"` to Gemfile')
end
end
end

0 comments on commit 2e61d95

Please sign in to comment.