Skip to content

Commit

Permalink
use to_sym.downcase to avoid allocations, assume ids are downcased an…
Browse files Browse the repository at this point in the history
…d store them that way
  • Loading branch information
korbin committed Aug 16, 2024
1 parent 97690e9 commit da74036
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 38 deletions.
45 changes: 13 additions & 32 deletions lib/money/currency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ def _instances
# Money::Currency.find(:eur) #=> #<Money::Currency id: eur ...>
# Money::Currency.find(:foo) #=> nil
def find(id)
id = id.to_s.downcase.to_sym
new(id)
rescue UnknownCurrency
nil
Expand Down Expand Up @@ -141,16 +140,6 @@ def all
end.sort_by(&:priority)
end

# We need a string-based validator before creating an unbounded number of
# symbols.
# http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#11
# https://github.com/RubyMoney/money/issues/132
#
# @return [Set]
def stringified_keys
@stringified_keys ||= stringify_keys
end

# Register a new currency
#
# @param curr [Hash] information about the currency
Expand All @@ -169,18 +158,17 @@ def stringified_keys
# amounts
# @option delimiter [String] character between each thousands place
def register(curr)
key = curr.fetch(:iso_code).downcase.to_sym
@@mutex.synchronize { _instances.delete(key.to_s) }
key = curr.fetch(:iso_code).to_sym.downcase
@@mutex.synchronize { _instances.delete(key.to_sym) }
@table[key] = curr
@stringified_keys = nil
end

# Inherit a new currency from existing one
#
# @param parent_iso_code [String] the international 3-letter code as defined
# @param curr [Hash] See {register} method for hash structure
def inherit(parent_iso_code, curr)
parent_iso_code = parent_iso_code.downcase.to_sym
parent_iso_code = parent_iso_code.to_sym.downcase
curr = @table.fetch(parent_iso_code, {}).merge(curr)
register(curr)
end
Expand All @@ -194,13 +182,12 @@ def inherit(parent_iso_code, curr)
# if it didn't.
def unregister(curr)
if curr.is_a?(Hash)
key = curr.fetch(:iso_code).downcase.to_sym
key = curr.fetch(:iso_code).to_sym.downcase
else
key = curr.downcase.to_sym
key = curr.to_sym.downcase
end
existed = @table.delete(key)
@stringified_keys = nil if existed
existed ? true : false

@table.delete(key)
end

def each
Expand All @@ -211,12 +198,6 @@ def reset!
@@instances = {}
@table = Loader.load_currencies
end

private

def stringify_keys
table.keys.each_with_object(Set.new) { |k, set| set.add(k.to_s.downcase) }
end
end

# @!attribute [r] id
Expand Down Expand Up @@ -320,11 +301,11 @@ def ==(other_currency)

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

Expand Down Expand Up @@ -361,7 +342,7 @@ def inspect
# Money::Currency.new(:usd).to_s #=> "USD"
# Money::Currency.new(:eur).to_s #=> "EUR"
def to_s
id.to_s.upcase
id.upcase.to_s
end

# Returns a string representation corresponding to the upcase +id+
Expand All @@ -373,7 +354,7 @@ def to_s
# Money::Currency.new(:usd).to_str #=> "USD"
# Money::Currency.new(:eur).to_str #=> "EUR"
def to_str
id.to_s.upcase
id.upcase.to_s
end

# Returns a symbol representation corresponding to the upcase +id+
Expand All @@ -385,7 +366,7 @@ def to_str
# Money::Currency.new(:usd).to_sym #=> :USD
# Money::Currency.new(:eur).to_sym #=> :EUR
def to_sym
id.to_s.upcase.to_sym
id.upcase
end

# Conversion to +self+.
Expand Down
7 changes: 1 addition & 6 deletions spec/currency_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def to_s
currency = described_class.new("USD")

expect(described_class._instances.length).to eq 1
expect(described_class._instances["usd"].object_id).to eq currency.object_id
expect(described_class._instances[:usd].object_id).to eq currency.object_id
end

it "raises UnknownCurrency with unknown currency" do
Expand Down Expand Up @@ -365,11 +365,6 @@ def to_s
usd = described_class.new(:usd)
expect(usd.to_currency).to eq usd
end

it "doesn't create new symbols indefinitely" do
expect { described_class.new("bogus") }.to raise_error(described_class::UnknownCurrency)
expect(Symbol.all_symbols.map{|s| s.to_s}).not_to include("bogus")
end
end

describe "#code" do
Expand Down

0 comments on commit da74036

Please sign in to comment.