Module: Mint::Registry

Extended by:
Registry
Included in:
Registry
Defined in:
lib/minting/mint/registry/zeros.rb,
lib/minting/mint/registry/symbols.rb,
lib/minting/mint/registry/registry.rb,
lib/minting/mint/registry/registration.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.currenciesHash{String => Currency}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the frozen hash of all registered currencies (world + custom).

Returns:

  • (Hash{String => Currency})

    registered currencies mapped by code



34
35
36
# File 'lib/minting/mint/registry/registry.rb', line 34

def self.currencies
  @currencies || MUTEX.synchronize { @currencies = world_currencies.dup.freeze }
end

.register(code:, subunit: 0, symbol: '', priority: 0) ⇒ Currency

Registers a new currency, raising a KeyError if already registered.

Parameters:

  • code (String)

    the unique currency code

  • subunit (Integer) (defaults to: 0)

    the decimal subunit precision, defaults to 0

  • symbol (String) (defaults to: '')

    the display symbol

  • priority (Integer) (defaults to: 0)

    parser precedence priority

Returns:

  • (Currency)

    the newly registered Currency instance

Raises:

  • (ArgumentError)

    if the code contains invalid characters

  • (KeyError)

    if the currency code is already registered



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/minting/mint/registry/registration.rb', line 15

def self.register(code:, subunit: 0, symbol: '', priority: 0)
  raise ArgumentError, 'Currency code must be String' unless code.is_a? String
  unless code.match?(/^[A-Z_]+$/)
    raise ArgumentError,
          "Currency code must have only letters or '_' ('USD',, 'MY_COIN')"
  end

  MUTEX.synchronize do
    raise KeyError, "Currency: #{code} already registered" if currencies[code]

    currency = Currency.new(code:, subunit:, symbol:, priority:)
    @currencies = @currencies.merge(code => currency).freeze
    @currency_symbols = nil
    @currency_symbol_map = nil
    currency
  end
end

.world_currenciesHash{String => Currency}

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Loads ISO world currencies from YAML file.

Returns:

  • (Hash{String => Currency})

    ISO-4217 world currencies mapped by code



21
22
23
24
25
26
27
28
# File 'lib/minting/mint/registry/registry.rb', line 21

def self.world_currencies
  @world_currencies || MUTEX.synchronize do
    @world_currencies = begin
      path = File.join(File.expand_path('../../data', __dir__), 'world-currencies.yaml')
      YAML.load_file(path).to_h { |entry| [entry['code'], Currency.new(**entry.transform_keys(&:to_sym))] }
    end.freeze
  end
end

.zero_for(currency) ⇒ Money

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the cached zero-Money for a currency, creating it if needed.

Parameters:

  • currency (Currency)

    the currency object

Returns:

  • (Money)

    a frozen zero-Money



11
12
13
14
15
16
# File 'lib/minting/mint/registry/zeros.rb', line 11

def self.zero_for(currency)
  MUTEX.synchronize do
    @zeros ||= {}
    @zeros[currency] ||= Mint::Money.send(:new, 0, currency)
  end
end

Instance Method Details

#currency_for_symbol(symbol) ⇒ Currency?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Looks up a currency by its display symbol.

Parameters:

  • symbol (String)

    the display symbol (e.g. “$”, “R$”)

Returns:

  • (Currency, nil)

    the highest-priority currency for the symbol



13
14
15
16
# File 'lib/minting/mint/registry/symbols.rb', line 13

def currency_for_symbol(symbol)
  @currency_symbol_map || MUTEX.synchronize { @currency_symbol_map = currency_symbols.to_h.freeze }
  @currency_symbol_map[symbol]
end

#detect_currency(input) ⇒ Currency?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Scans input for registered currency symbols and returns the first match.

Parameters:

  • input (String)

    the string to scan

Returns:



23
24
25
26
27
28
# File 'lib/minting/mint/registry/symbols.rb', line 23

def detect_currency(input)
  currency_symbols.each do |symbol, currency|
    return currency if input.include?(symbol)
  end
  nil
end