class Money::Currency

Experimental RBS support (using type sampling data from the type_fusion project).

# sig/money/currency.rbs

class Money::Currency
  def _instances: () -> untyped
end

@see iso4217.net/
@see en.wikipedia.org/wiki/Currency<br><br>Represents a specific currency unit.

def <=>(other_currency)

Returns:
  • (-1, 0, 1) - -1 if less than, 0 is equal to, 1 if greater than

Parameters:
  • other_currency (Money::Currency) -- The currency to compare to.
def <=>(other_currency)
  # <=> returns nil when one of the values is nil
  comparison = self.priority <=> other_currency.priority || 0
  if comparison == 0
    self.id <=> other_currency.id
  else
    comparison
  end
end

def ==(other_currency)

Returns:
  • (Boolean) -

Parameters:
  • other_currency (Money::Currency) -- The currency to compare to.
def ==(other_currency)
  self.equal?(other_currency) || compare_ids(other_currency)
end

def _instances

Experimental RBS support (using type sampling data from the type_fusion project).

def _instances: () -> untyped

This signature was generated using 1 sample from 1 application.

def _instances
  @@instances
end

def all

Returns:
  • (Array) -
def all
  table.keys.map do |curr|
    c = Currency.new(curr)
    if c.priority.nil?
      raise MissingAttributeError.new(:all, c.id, :priority)
    end
    c
  end.sort_by(&:priority)
end

def code

Returns:
  • (String) -
def code
  symbol || iso_code
end

def compare_ids(other_currency)

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

def each

def each
  all.each { |c| yield(c) }
end

def exponent

Returns:
  • (Integer) -

Other tags:
    See: https://en.wikipedia.org/wiki/ISO_4217#Active_codes -
def exponent
  Math.log10(subunit_to_unit).round
end

def find(id)

Returns:
  • (Money::Currency) -

Parameters:
  • id (String, Symbol, #to_s) -- Used to look into +table+ and
def find(id)
  id = id.to_s.downcase.to_sym
  new(id)
rescue UnknownCurrency
  nil
end

def find_by_iso_numeric(num)

Returns:
  • (Money::Currency) -

Parameters:
  • num (#to_s) -- used to look into +table+ in +iso_numeric+ and find
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
  nil
end

def hash

Returns:
  • (Integer) -
def hash
  id.hash
end

def inherit(parent_iso_code, curr)

Parameters:
  • curr (Hash) -- See {register} method for hash structure
  • parent_iso_code (String) -- the international 3-letter code as defined
def inherit(parent_iso_code, curr)
  parent_iso_code = parent_iso_code.downcase.to_sym
  curr = @table.fetch(parent_iso_code, {}).merge(curr)
  register(curr)
end

def initialize(id)

Returns:
  • (Money::Currency) -

Parameters:
  • id (String, Symbol, #to_s) -- Used to look into +table+ and retrieve
def initialize(id)
  @id = id.to_sym
  initialize_data!
end

def initialize_data!

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]
  @smallest_denomination = data[:smallest_denomination]
  @subunit               = data[:subunit]
  @subunit_to_unit       = data[:subunit_to_unit]
  @symbol                = data[:symbol]
  @symbol_first          = data[:symbol_first]
  @thousands_separator   = data[:thousands_separator]
  @format                = data[:format]
end

def inspect

Returns:
  • (String) -
def inspect
  "#<#{self.class.name} id: #{id}, priority: #{priority}, symbol_first: #{symbol_first}, thousands_separator: #{thousands_separator}, html_entity: #{html_entity}, decimal_mark: #{decimal_mark}, name: #{name}, symbol: #{symbol}, subunit_to_unit: #{subunit_to_unit}, exponent: #{exponent}, iso_code: #{iso_code}, iso_numeric: #{iso_numeric}, subunit: #{subunit}, smallest_denomination: #{smallest_denomination}, format: #{format}>"
end

def iso?

Returns:
  • (Boolean) -
def iso?
  iso_numeric && iso_numeric != ''
end

def new(id)

def new(id)
  id = id.to_s.downcase
  unless stringified_keys.include?(id)
    raise UnknownCurrency, "Unknown currency '#{id}'"
  end
  _instances[id] || @@mutex.synchronize { _instances[id] ||= super }
end

def register(curr)

Options Hash: (**delimiter)
  • character (String) -- between each thousands place
  • character (String) -- between the whole and fraction
  • the (Numeric) -- proportion between the unit and
  • the (String) -- name of the fractional monetary unit
  • the (String) -- currency symbol (UTF-8 encoded)
  • the (String) -- currency name
  • the (Integer) -- international 3-digit code as
  • the (String) -- international 3-letter code as defined
  • a (Numeric) -- numerical value you can use to sort/group

Parameters:
  • curr (Hash) -- information about the currency
def register(curr)
  key = curr.fetch(:iso_code).downcase.to_sym
  @@mutex.synchronize { _instances.delete(key.to_s) }
  @table[key] = curr
  @stringified_keys = nil
end

def reset!

def reset!
  @@instances = {}
  @table = Loader.load_currencies
end

def stringified_keys

Returns:
  • (Set) -
def stringified_keys
  @stringified_keys ||= stringify_keys
end

def stringify_keys

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

def symbol_first?

def symbol_first?
  !!@symbol_first
end

def table

http://search.cpan.org/~tnguyen/Locale-Currency-Format-1.28/Format.pm
See https://en.wikipedia.org/wiki/List_of_circulating_currencies and

https://www.answers.com/topic/fractional-monetary-unit-subunit
A monetary unit that is valued at a fraction (usually one hundredth) of the basic monetary unit
== fractional monetary unit, subunit
https://www.answers.com/topic/monetary-unit
The standard unit of value of a currency, as the dollar in the United States or the peso in Mexico.
== monetary unit

List of known currencies.
def table
  @table ||= Loader.load_currencies
end

def to_currency

Returns:
  • (self) -
def to_currency
  self
end

def to_s

Returns:
  • (String) -
def to_s
  id.to_s.upcase
end

def to_str

Returns:
  • (String) -
def to_str
  id.to_s.upcase
end

def to_sym

Returns:
  • (Symbol) -
def to_sym
  id.to_s.upcase.to_sym
end

def unregister(curr)

Returns:
  • (Boolean) - true if the currency previously existed, false

Parameters:
  • curr (Object) -- A Hash with the key `:iso_code`, or the ISO code
def unregister(curr)
  if curr.is_a?(Hash)
    key = curr.fetch(:iso_code).downcase.to_sym
  else
    key = curr.downcase.to_sym
  end
  existed = @table.delete(key)
  @stringified_keys = nil if existed
  existed ? true : false
end

def wrap(object)

Returns:
  • (Money::Currency) -

Parameters:
  • object (Object) -- The object to attempt and wrap as a +Currency+
def wrap(object)
  if object.nil?
    nil
  elsif object.is_a?(Currency)
    object
  else
    Currency.new(object)
  end
end