Module: Mint::Registry

Extended by:
Registry
Included in:
Registry
Defined in:
lib/minting/mint/registry/zeros.rb,
lib/minting/mint/registry/crypto.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

.crypto_currenciesArray<Currency>

Returns the list of built-in crypto currency definitions.

These are not registered by default — call register_crypto to opt in.

Returns:

  • (Array<Currency>)

    frozen array of crypto currency definitions



17
18
19
20
21
22
23
24
# File 'lib/minting/mint/registry/crypto.rb', line 17

def self.crypto_currencies
  @crypto_currencies || CRYPTO_MUTEX.synchronize do
    @crypto_currencies ||= begin
      path = File.join(File.expand_path('../../data', __dir__), 'crypto-currencies.yaml')
      YAML.load_file(path).map { |entry| Currency.new(**entry.transform_keys(&:to_sym)) }.freeze
    end
  end
end

.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:



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

def self.currencies = @currencies

.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
# 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
    @symbols_list = nil
    currency
  end
end

.register_all_cryptoArray<Currency>

Registers all built-in crypto currencies at once.

Raises on the first duplicate — call rescue if idempotency is needed.

Returns:

  • (Array<Currency>)

    the newly registered Currency objects

Raises:

  • (KeyError)

    if any currency code is already registered



53
54
55
56
57
# File 'lib/minting/mint/registry/crypto.rb', line 53

def self.register_all_crypto
  crypto_currencies.map do |c|
    Currency.register(code: c.code, subunit: c.subunit, symbol: c.symbol, priority: c.priority)
  end
end

.register_crypto(*codes) ⇒ Array<Currency>

Registers one or more crypto currencies into the shared currency registry.

Raises on duplicate registration or unknown code — use rescue if idempotent bulk registration is needed.

Parameters:

  • codes (Array<String>)

    one or more crypto currency codes

Returns:

  • (Array<Currency>)

    the newly registered Currency objects

Raises:

  • (ArgumentError)

    if a code is not a known crypto currency

  • (KeyError)

    if the currency code is already registered



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/minting/mint/registry/crypto.rb', line 35

def self.register_crypto(*codes)
  entries = crypto_currencies
  index = entries.each_with_index.to_h { |currency, index| [currency.code, index] }
  missing = codes.reject { |code| index.key?(code) }
  raise ArgumentError, "Unknown crypto code(s): #{missing.join(', ')}" unless missing.empty?

  codes.map do |code|
    c = entries[index[code]]
    Currency.register(code:, subunit: c.subunit, symbol: c.symbol, priority: c.priority)
  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



28
# File 'lib/minting/mint/registry/registry.rb', line 28

def self.world_currencies = @world_currencies

.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

Raises:

  • (ArgumentError)


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

def self.zero_for(currency)
  raise ArgumentError, "Expect a Currency param. (#{currency})" unless currency.is_a?(Currency)

  return @zeros[currency] if @zeros&.key?(currency)

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

Instance Method Details

#currency_for_symbol(symbol) ⇒ Object



8
9
10
11
# File 'lib/minting/mint/registry/symbols.rb', line 8

def currency_for_symbol(symbol)
  sync_symbols
  @symbols_map[symbol]
end

#detect_currency(input) ⇒ Object



18
19
20
21
# File 'lib/minting/mint/registry/symbols.rb', line 18

def detect_currency(input)
  sync_symbols
  input.match(@symbols_regex) { |m| @symbols_map[m[0]] }
end

#symbol_shared?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


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

def symbol_shared?(symbol)
  sync_symbols
  @shared_symbols.include?(symbol)
end