Skip to content

Commit

Permalink
Use Currency::Loader directly without extending (tonsky#845)
Browse files Browse the repository at this point in the history
  • Loading branch information
antstorm authored Jan 20, 2019
1 parent 4340099 commit 422172a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 24 deletions.
3 changes: 1 addition & 2 deletions lib/money/currency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Money
class Currency
include Comparable
extend Enumerable
extend Money::Currency::Loader
extend Money::Currency::Heuristics

# Keeping cached instances in sync between threads
Expand Down Expand Up @@ -121,7 +120,7 @@ def wrap(object)
# See https://en.wikipedia.org/wiki/List_of_circulating_currencies and
# http://search.cpan.org/~tnguyen/Locale-Currency-Format-1.28/Format.pm
def table
@table ||= load_currencies
@table ||= Loader.load_currencies
end

# List the currencies imported and registered
Expand Down
28 changes: 15 additions & 13 deletions lib/money/currency/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ class Currency
module Loader
DATA_PATH = File.expand_path("../../../../config", __FILE__)

# Loads and returns the currencies stored in JSON files in the config directory.
#
# @return [Hash]
def load_currencies
currencies = parse_currency_file("currency_iso.json")
currencies.merge! parse_currency_file("currency_non_iso.json")
currencies.merge! parse_currency_file("currency_backwards_compatible.json")
end
class << self
# Loads and returns the currencies stored in JSON files in the config directory.
#
# @return [Hash]
def load_currencies
currencies = parse_currency_file("currency_iso.json")
currencies.merge! parse_currency_file("currency_non_iso.json")
currencies.merge! parse_currency_file("currency_backwards_compatible.json")
end

private
private

def parse_currency_file(filename)
json = File.read("#{DATA_PATH}/#{filename}")
json.force_encoding(::Encoding::UTF_8) if defined?(::Encoding)
JSON.parse(json, symbolize_names: true)
def parse_currency_file(filename)
json = File.read("#{DATA_PATH}/#{filename}")
json.force_encoding(::Encoding::UTF_8) if defined?(::Encoding)
JSON.parse(json, symbolize_names: true)
end
end
end
end
Expand Down
12 changes: 3 additions & 9 deletions spec/currency/loader_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
# encoding: utf-8

describe Money::Currency::Loader do
class CurrencyLoader
include Money::Currency::Loader
end

let(:loader) { CurrencyLoader.new }

it "returns a currency table hash" do
expect(loader.load_currencies).to be_a Hash
expect(subject.load_currencies).to be_a Hash
end

it "parse currency_iso.json & currency_non_iso.json & currency_backwards_compatible.json" do
expect(loader).to receive(:parse_currency_file).exactly(3).times.and_return({})
expect(subject).to receive(:parse_currency_file).exactly(3).times.and_return({})

loader.load_currencies
subject.load_currencies
end
end

0 comments on commit 422172a

Please sign in to comment.