class Money::Currency

Represents a specific currency unit.

@see en.wikipedia.org/wiki/Currency @see iso4217.net/

Attributes

decimal_mark[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
delimiter[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
disambiguate_symbol[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
html_entity[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
id[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
iso_code[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
iso_numeric[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
name[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
priority[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
separator[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
smallest_denomination[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
subunit[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
subunit_to_unit[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
symbol[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
symbol_first[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)
thousands_separator[R]

@!attribute [r] id

@return [Symbol] The symbol used to identify the currency, usually THE
  lowercase +iso_code+ attribute.

@!attribute [r] priority

@return [Integer] A numerical value you can use to sort/group the
  currency list.

@!attribute [r] #iso_code

@return [String] The international 3-letter code as defined by the ISO
  4217 standard.

@!attribute [r] #iso_numeric

@return [String] The international 3-numeric code as defined by the ISO
  4217 standard.

@!attribute [r] name

@return [String] The currency name.

@!attribute [r] symbol

@return [String] The currency symbol (UTF-8 encoded).

@!attribute [r] #disambiguate_symbol

@return [String] Alternative currency used if symbol is ambiguous

@!attribute [r] #html_entity

@return [String] The html entity for the currency symbol

@!attribute [r] subunit

@return [String] The name of the fractional monetary unit.

@!attribute [r] #subunit_to_unit

@return [Integer] The proportion between the unit and the subunit

@!attribute [r] #decimal_mark

@return [String] The decimal mark, or character used to separate the
  whole unit from the subunit.

@!attribute [r] #thousands_separator

@return [String] The character used to separate thousands grouping of
  the whole unit.

@!attribute [r] #symbol_first

@return [Boolean] Should the currency symbol precede the amount, or
  should it come after?

@!attribute [r] #smallest_denomination

@return [Integer] Smallest amount of cash possible (in the subunit of
  this currency)

Public Class Methods

_instances() click to toggle source
# File lib/money/currency.rb, line 46
def _instances
  @@instances
end
all() click to toggle source

List the currencies imported and registered @return [Array]

@example

Money::Currency.iso_codes()
[#<Currency ..USD>, 'CAD', 'EUR']...
# File lib/money/currency.rb, line 131
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
decimal_places_cache() click to toggle source

Cache decimal places for #subunit_to_unit values. Common ones pre-cached.

# File lib/money/currency.rb, line 402
def self.decimal_places_cache
  @decimal_places_cache ||= {1 => 0, 10 => 1, 100 => 2, 1000 => 3}
end
each() { |c| ... } click to toggle source
# File lib/money/currency.rb, line 195
def each
  all.each { |c| yield(c) }
end
find(id) click to toggle source

Lookup a currency with given id an returns a Currency instance on success, nil otherwise.

@param [String, Symbol, to_s] id Used to look into table and retrieve the applicable attributes.

@return [Money::Currency]

@example

Money::Currency.find(:eur) #=> #<Money::Currency id: eur ...>
Money::Currency.find(:foo) #=> nil
# File lib/money/currency.rb, line 61
def find(id)
  id = id.to_s.downcase.to_sym
  new(id)
rescue UnknownCurrency
  nil
end
find_by_iso_numeric(num) click to toggle source

Lookup a currency with given num as an ISO 4217 numeric and returns an Currency instance on success, nil otherwise.

@param [#to_s] num used to look into table in iso_numeric and find the right currency id.

@return [Money::Currency]

@example

Money::Currency.find_by_iso_numeric(978) #=> #<Money::Currency id: eur ...>
Money::Currency.find_by_iso_numeric('001') #=> nil
# File lib/money/currency.rb, line 79
def find_by_iso_numeric(num)
  num = num.to_s
  id, _ = self.table.find{|key, currency| currency[:iso_numeric] == num}
  new(id)
rescue UnknownCurrency
  nil
end
new(id) click to toggle source
Calls superclass method
# File lib/money/currency.rb, line 37
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
new(id) click to toggle source

Create a new Currency object.

@param [String, Symbol, to_s] id Used to look into table and retrieve

the applicable attributes.

@return [Money::Currency]

@example

Money::Currency.new(:usd) #=> #<Money::Currency id: usd ...>
# File lib/money/currency.rb, line 261
def initialize(id)
  @id = id.to_sym
  initialize_data!
end
register(curr) click to toggle source

Register a new currency

@param curr [Hash] information about the currency @option priority [Numeric] a numerical value you can use to sort/group

the currency list

@option #iso_code [String] the international 3-letter code as defined

by the ISO 4217 standard

@option #iso_numeric [Integer] the international 3-digit code as

defined by the ISO 4217 standard

@option name [String] the currency name @option symbol [String] the currency symbol (UTF-8 encoded) @option subunit [String] the name of the fractional monetary unit @option #subunit_to_unit [Numeric] the proportion between the unit and

the subunit

@option separator [String] character between the whole and fraction

amounts

@option delimiter [String] character between each thousands place

# File lib/money/currency.rb, line 168
def register(curr)
  key = curr.fetch(:iso_code).downcase.to_sym
  @@mutex.synchronize { _instances.delete(key.to_s) }
  @table[key] = curr
  @stringified_keys = stringify_keys
end
stringified_keys() click to toggle source

We need a string-based validator before creating an unbounded number of symbols. www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#11 github.com/RubyMoney/money/issues/132

@return [Set]

# File lib/money/currency.rb, line 147
def stringified_keys
  @stringified_keys ||= stringify_keys
end
table() click to toggle source

List of known currencies.

monetary unit

The standard unit of value of a currency, as the dollar in the United States or the peso in Mexico. www.answers.com/topic/monetary-unit

fractional monetary unit, subunit

A monetary unit that is valued at a fraction (usually one hundredth) of the basic monetary unit www.answers.com/topic/fractional-monetary-unit-subunit

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

# File lib/money/currency.rb, line 121
def table
  @table ||= load_currencies
end
unregister(curr) click to toggle source

Unregister a currency.

@param [Object] curr A Hash with the key `:iso_code`, or the ISO code

as a String or Symbol.

@return [Boolean] true if the currency previously existed, false

if it didn't.
# File lib/money/currency.rb, line 183
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 = stringify_keys if existed
  existed ? true : false
end
wrap(object) click to toggle source

Wraps the object in a Currency unless it's already a Currency object.

@param [Object] object The object to attempt and wrap as a Currency object.

@return [Money::Currency]

@example

c1 = Money::Currency.new(:usd)
Money::Currency.wrap(nil)   #=> nil
Money::Currency.wrap(c1)    #=> #<Money::Currency id: usd ...>
Money::Currency.wrap("usd") #=> #<Money::Currency id: usd ...>
# File lib/money/currency.rb, line 100
def wrap(object)
  if object.nil?
    nil
  elsif object.is_a?(Currency)
    object
  else
    Currency.new(object)
  end
end

Private Class Methods

stringify_keys() click to toggle source
# File lib/money/currency.rb, line 202
def stringify_keys
  table.keys.each_with_object(Set.new) { |k, set| set.add(k.to_s.downcase) }
end

Public Instance Methods

<=>(other_currency) click to toggle source

Compares self with other_currency against the value of priority attribute.

@param [Money::Currency] other_currency The currency to compare to.

@return [-1,0,1] -1 if less than, 0 is equal to, 1 if greater than

@example

c1 = Money::Currency.new(:usd)
c2 = Money::Currency.new(:jpy)
c1 <=> c2 #=> 1
c2 <=> c1 #=> -1
c1 <=> c1 #=> 0
# File lib/money/currency.rb, line 279
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
==(other_currency) click to toggle source

Compares self with other_currency and returns true if the are the same or if their id attributes match.

@param [Money::Currency] other_currency The currency to compare to.

@return [Boolean]

@example

c1 = Money::Currency.new(:usd)
c2 = Money::Currency.new(:jpy)
c1 == c1 #=> true
c1 == c2 #=> false
# File lib/money/currency.rb, line 302
def ==(other_currency)
  self.equal?(other_currency) || compare_ids(other_currency)
end
Also aliased as: eql?
code() click to toggle source

Returns currency symbol or iso code for currencies with no symbol.

@return [String]

# File lib/money/currency.rb, line 386
def code
  symbol || iso_code
end
decimal_places() click to toggle source

The number of decimal places needed.

@return [Integer]

# File lib/money/currency.rb, line 409
def decimal_places
  cache[subunit_to_unit] ||= calculate_decimal_places(subunit_to_unit)
end
eql?(other_currency)
Alias for: ==
exponent() click to toggle source

Returns the number of digits after the decimal separator.

@return [Float]

# File lib/money/currency.rb, line 397
def exponent
  Math.log10(@subunit_to_unit)
end
hash() click to toggle source

Returns a Fixnum hash value based on the id attribute in order to use functions like & (intersection), group_by, etc.

@return [Fixnum]

@example

Money::Currency.new(:usd).hash #=> 428936
# File lib/money/currency.rb, line 323
def hash
  id.hash
end
inspect() click to toggle source

Returns a human readable representation.

@return [String]

@example

Money::Currency.new(:usd) #=> #<Currency id: usd ...>
# File lib/money/currency.rb, line 333
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}>"
end
symbol_first?() click to toggle source
# File lib/money/currency.rb, line 390
def symbol_first?
  !!@symbol_first
end
to_currency() click to toggle source

Conversation to self.

@return [self]

# File lib/money/currency.rb, line 379
def to_currency
  self
end
to_s() click to toggle source

Returns a string representation corresponding to the upcase id attribute.

– DEV: id.to_s.upcase corresponds to #iso_code but don't use ISO_CODE for consistency.

@return [String]

@example

Money::Currency.new(:usd).to_s #=> "USD"
Money::Currency.new(:eur).to_s #=> "EUR"
# File lib/money/currency.rb, line 348
def to_s
  id.to_s.upcase
end
to_str() click to toggle source

Returns a string representation corresponding to the upcase id attribute. Useful in cases where only implicit conversions are made.

@return [String]

@example

Money::Currency.new(:usd).to_str #=> "USD"
Money::Currency.new(:eur).to_str #=> "EUR"
# File lib/money/currency.rb, line 360
def to_str
  id.to_s.upcase
end
to_sym() click to toggle source

Returns a symbol representation corresponding to the upcase id attribute.

@return [Symbol]

@example

Money::Currency.new(:usd).to_sym #=> :USD
Money::Currency.new(:eur).to_sym #=> :EUR
# File lib/money/currency.rb, line 372
def to_sym
  id.to_s.upcase.to_sym
end

Private Instance Methods

cache() click to toggle source
# File lib/money/currency.rb, line 415
def cache
  self.class.decimal_places_cache
end
calculate_decimal_places(num) click to toggle source

If we need to figure out how many decimal places we need we use repeated integer division.

# File lib/money/currency.rb, line 421
def calculate_decimal_places(num)
  i = 1
  while num >= 10
    num /= 10
    i += 1 if num >= 10
  end
  i
end
compare_ids(other_currency) click to toggle source
# File lib/money/currency.rb, line 306
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
initialize_data!() click to toggle source
# File lib/money/currency.rb, line 430
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]
end