Class: Money::Currency
- Inherits:
-
Object
- Object
- Money::Currency
- Extended by:
- Enumerable, Heuristics
- Includes:
- Comparable
- Defined in:
- lib/money/currency.rb,
lib/money/currency/loader.rb,
lib/money/currency/heuristics.rb
Overview
Represents a specific currency unit.
Defined Under Namespace
Modules: Heuristics, Loader Classes: MissingAttributeError, NoCurrency, UnknownCurrency
Constant Summary collapse
- @@mutex =
Keeping cached instances in sync between threads
Mutex.new
- @@instances =
{}
Instance Attribute Summary collapse
-
#decimal_mark ⇒ String
(also: #separator)
readonly
The decimal mark, or character used to separate the whole unit from the subunit.
-
#disambiguate_symbol ⇒ String
readonly
Alternative currency used if symbol is ambiguous.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#html_entity ⇒ String
readonly
The html entity for the currency symbol.
-
#id ⇒ Symbol
readonly
The symbol used to identify the currency, usually THE lowercase +iso_code+ attribute.
-
#iso_code ⇒ String
readonly
The international 3-letter code as defined by the ISO 4217 standard.
-
#iso_numeric ⇒ String
readonly
The international 3-numeric code as defined by the ISO 4217 standard.
-
#name ⇒ String
readonly
The currency name.
-
#priority ⇒ Integer
readonly
A numerical value you can use to sort/group the currency list.
-
#smallest_denomination ⇒ Integer
readonly
Smallest amount of cash possible (in the subunit of this currency).
-
#subunit ⇒ String
readonly
The name of the fractional monetary unit.
-
#subunit_to_unit ⇒ Integer
readonly
The proportion between the unit and the subunit.
-
#symbol ⇒ String
readonly
The currency symbol (UTF-8 encoded).
-
#symbol_first ⇒ Boolean
readonly
Should the currency symbol precede the amount, or should it come after?.
-
#thousands_separator ⇒ String
(also: #delimiter)
readonly
The character used to separate thousands grouping of the whole unit.
Class Method Summary collapse
- ._instances ⇒ Object
-
.all ⇒ Array
List the currencies imported and registered.
- .each ⇒ Object
-
.find(id) ⇒ Money::Currency
Lookup a currency with given +id+ an returns a +Currency+ instance on success, +nil+ otherwise.
-
.find_by_iso_numeric(num) ⇒ Money::Currency
Lookup a currency with given +num+ as an ISO 4217 numeric and returns an +Currency+ instance on success, +nil+ otherwise.
-
.inherit(parent_iso_code, curr) ⇒ Object
Inherit a new currency from existing one.
- .new(id) ⇒ Object
-
.register(curr) ⇒ Object
Register a new currency.
- .reset! ⇒ Object
-
.stringified_keys ⇒ Set
We need a string-based validator before creating an unbounded number of symbols.
-
.table ⇒ Object
List of known currencies.
-
.unregister(curr) ⇒ Boolean
Unregister a currency.
-
.wrap(object) ⇒ Money::Currency
Wraps the object in a +Currency+ unless it's already a +Currency+ object.
Instance Method Summary collapse
-
#<=>(other_currency) ⇒ -1, ...
Compares +self+ with +other_currency+ against the value of +priority+ attribute.
-
#==(other_currency) ⇒ Boolean
Compares +self+ with +other_currency+ and returns +true+ if the are the same or if their +id+ attributes match.
-
#cents_based? ⇒ Boolean
Returns true if a subunit is cents-based.
-
#code ⇒ String
Returns currency symbol or iso code for currencies with no symbol.
- #eql? ⇒ Object
-
#exponent ⇒ Integer
(also: #decimal_places)
Returns the relation between subunit and unit as a base 10 exponent.
-
#hash ⇒ Integer
Returns a Integer hash value based on the +id+ attribute in order to use functions like & (intersection), group_by, etc.
-
#initialize(id) ⇒ Money::Currency
constructor
Create a new +Currency+ object.
-
#inspect ⇒ String
Returns a human readable representation.
-
#iso? ⇒ Boolean
Returns true if a code currency is ISO.
- #symbol_first? ⇒ Boolean
-
#to_currency ⇒ self
Conversion to +self+.
-
#to_s ⇒ String
Returns a string representation corresponding to the upcase +id+ attribute.
-
#to_str ⇒ String
Returns a string representation corresponding to the upcase +id+ attribute.
-
#to_sym ⇒ Symbol
Returns a symbol representation corresponding to the upcase +id+ attribute.
Methods included from Heuristics
Constructor Details
#initialize(id) ⇒ Money::Currency
Create a new +Currency+ object.
290 291 292 293 |
# File 'lib/money/currency.rb', line 290 def initialize(id) @id = id.to_sym initialize_data! end |
Instance Attribute Details
#decimal_mark ⇒ String (readonly) Also known as: separator
Returns The decimal mark, or character used to separate the whole unit from the subunit.
273 274 275 |
# File 'lib/money/currency.rb', line 273 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination, :format |
#disambiguate_symbol ⇒ String (readonly)
Returns Alternative currency used if symbol is ambiguous.
273 274 275 |
# File 'lib/money/currency.rb', line 273 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination, :format |
#format ⇒ Object (readonly)
Returns the value of attribute format.
273 274 275 |
# File 'lib/money/currency.rb', line 273 def format @format end |
#html_entity ⇒ String (readonly)
Returns The html entity for the currency symbol.
273 274 275 |
# File 'lib/money/currency.rb', line 273 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination, :format |
#id ⇒ Symbol (readonly)
Returns The symbol used to identify the currency, usually THE lowercase +iso_code+ attribute.
273 274 275 |
# File 'lib/money/currency.rb', line 273 def id @id end |
#iso_code ⇒ String (readonly)
Returns The international 3-letter code as defined by the ISO 4217 standard.
273 274 275 |
# File 'lib/money/currency.rb', line 273 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination, :format |
#iso_numeric ⇒ String (readonly)
Returns The international 3-numeric code as defined by the ISO 4217 standard.
273 274 275 |
# File 'lib/money/currency.rb', line 273 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination, :format |
#name ⇒ String (readonly)
Returns The currency name.
273 274 275 |
# File 'lib/money/currency.rb', line 273 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination, :format |
#priority ⇒ Integer (readonly)
Returns A numerical value you can use to sort/group the currency list.
273 274 275 |
# File 'lib/money/currency.rb', line 273 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination, :format |
#smallest_denomination ⇒ Integer (readonly)
Returns Smallest amount of cash possible (in the subunit of this currency).
273 274 275 |
# File 'lib/money/currency.rb', line 273 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination, :format |
#subunit ⇒ String (readonly)
Returns The name of the fractional monetary unit.
273 274 275 |
# File 'lib/money/currency.rb', line 273 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination, :format |
#subunit_to_unit ⇒ Integer (readonly)
Returns The proportion between the unit and the subunit.
273 274 275 |
# File 'lib/money/currency.rb', line 273 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination, :format |
#symbol ⇒ String (readonly)
Returns The currency symbol (UTF-8 encoded).
273 274 275 |
# File 'lib/money/currency.rb', line 273 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination, :format |
#symbol_first ⇒ Boolean (readonly)
Returns Should the currency symbol precede the amount, or should it come after?.
273 274 275 |
# File 'lib/money/currency.rb', line 273 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination, :format |
#thousands_separator ⇒ String (readonly) Also known as: delimiter
Returns The character used to separate thousands grouping of the whole unit.
273 274 275 |
# File 'lib/money/currency.rb', line 273 attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol, :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark, :thousands_separator, :symbol_first, :smallest_denomination, :format |
Class Method Details
._instances ⇒ Object
49 50 51 |
# File 'lib/money/currency.rb', line 49 def _instances @@instances end |
.all ⇒ Array
List the currencies imported and registered
136 137 138 139 140 141 142 143 144 |
# File 'lib/money/currency.rb', line 136 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 |
.each ⇒ Object
209 210 211 |
# File 'lib/money/currency.rb', line 209 def each all.each { |c| yield(c) } end |
.find(id) ⇒ Money::Currency
Lookup a currency with given +id+ an returns a +Currency+ instance on success, +nil+ otherwise.
retrieve the applicable attributes.
64 65 66 67 68 69 |
# File 'lib/money/currency.rb', line 64 def find(id) id = id.to_s.downcase.to_sym new(id) rescue UnknownCurrency nil end |
.find_by_iso_numeric(num) ⇒ Money::Currency
Lookup a currency with given +num+ as an ISO 4217 numeric and returns an +Currency+ instance on success, +nil+ otherwise.
the right currency id.
83 84 85 86 87 88 89 90 |
# File 'lib/money/currency.rb', line 83 def find_by_iso_numeric(num) num = num.to_s.rjust(3, '0') return if num.empty? id = iso_numeric_index[num] new(id) if id rescue UnknownCurrency nil end |
.inherit(parent_iso_code, curr) ⇒ Object
Inherit a new currency from existing one
185 186 187 188 189 |
# File 'lib/money/currency.rb', line 185 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 |
.new(id) ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/money/currency.rb', line 40 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 |
.register(curr) ⇒ Object
Register a new currency
173 174 175 176 177 178 179 |
# File 'lib/money/currency.rb', line 173 def register(curr) key = curr.fetch(:iso_code).downcase.to_sym @@mutex.synchronize { _instances.delete(key.to_s) } @table[key] = curr @stringified_keys = nil clear_iso_numeric_cache end |
.reset! ⇒ Object
213 214 215 216 217 |
# File 'lib/money/currency.rb', line 213 def reset! @@instances = {} @table = Loader.load_currencies clear_iso_numeric_cache end |
.stringified_keys ⇒ Set
We need a string-based validator before creating an unbounded number of symbols. http://www.randomhacks.net.s3-website-us-east-1.amazonaws.com/2007/01/20/13-ways-of-looking-at-a-ruby-symbol/#11 https://github.com/RubyMoney/money/issues/132
152 153 154 |
# File 'lib/money/currency.rb', line 152 def stringified_keys @stringified_keys ||= stringify_keys end |
.table ⇒ Object
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. https://www.answers.com/redirectSearch?query=monetary-unit == fractional monetary unit, subunit A monetary unit that is valued at a fraction (usually one hundredth) of the basic monetary unit https://www.answers.com/redirectSearch?query=fractional-monetary-unit-subunit
See https://en.wikipedia.org/wiki/List_of_circulating_currencies and https://metacpan.org/release/TNGUYEN/Locale-Currency-Format-1.28/view/Format.pm
126 127 128 |
# File 'lib/money/currency.rb', line 126 def table @table ||= Loader.load_currencies end |
.unregister(curr) ⇒ Boolean
Unregister a currency.
198 199 200 201 202 203 204 205 206 207 |
# File 'lib/money/currency.rb', line 198 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 |
.wrap(object) ⇒ Money::Currency
Wraps the object in a +Currency+ unless it's already a +Currency+ object.
object.
105 106 107 108 109 110 111 112 113 |
# File 'lib/money/currency.rb', line 105 def wrap(object) if object.nil? nil elsif object.is_a?(Currency) object else Currency.new(object) end end |
Instance Method Details
#<=>(other_currency) ⇒ -1, ...
Compares +self+ with +other_currency+ against the value of +priority+ attribute.
308 309 310 311 312 313 314 315 316 317 |
# File 'lib/money/currency.rb', line 308 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) ⇒ Boolean
Compares +self+ with +other_currency+ and returns +true+ if the are the same or if their +id+ attributes match.
331 332 333 |
# File 'lib/money/currency.rb', line 331 def ==(other_currency) self.equal?(other_currency) || compare_ids(other_currency) end |
#cents_based? ⇒ Boolean
Returns true if a subunit is cents-based.
441 442 443 |
# File 'lib/money/currency.rb', line 441 def cents_based? subunit_to_unit == 100 end |
#code ⇒ String
Returns currency symbol or iso code for currencies with no symbol.
415 416 417 |
# File 'lib/money/currency.rb', line 415 def code symbol || iso_code end |
#eql? ⇒ Object
279 |
# File 'lib/money/currency.rb', line 279 alias_method :eql?, :== |
#exponent ⇒ Integer Also known as: decimal_places
Returns the relation between subunit and unit as a base 10 exponent.
Note that MGA and MRU are exceptions and are rounded to 1
451 452 453 |
# File 'lib/money/currency.rb', line 451 def exponent Math.log10(subunit_to_unit).round end |
#hash ⇒ Integer
Returns a Integer hash value based on the +id+ attribute in order to use functions like & (intersection), group_by, etc.
352 353 354 |
# File 'lib/money/currency.rb', line 352 def hash id.hash end |
#inspect ⇒ String
Returns a human readable representation.
362 363 364 |
# File 'lib/money/currency.rb', line 362 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 |
#iso? ⇒ Boolean
Returns true if a code currency is ISO.
430 431 432 |
# File 'lib/money/currency.rb', line 430 def iso? iso_numeric && iso_numeric != '' end |
#symbol_first? ⇒ Boolean
419 420 421 |
# File 'lib/money/currency.rb', line 419 def symbol_first? !!@symbol_first end |
#to_currency ⇒ self
Conversion to +self+.
408 409 410 |
# File 'lib/money/currency.rb', line 408 def to_currency self end |
#to_s ⇒ String
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.
377 378 379 |
# File 'lib/money/currency.rb', line 377 def to_s id.to_s.upcase end |
#to_str ⇒ String
Returns a string representation corresponding to the upcase +id+ attribute. Useful in cases where only implicit conversions are made.
389 390 391 |
# File 'lib/money/currency.rb', line 389 def to_str id.to_s.upcase end |
#to_sym ⇒ Symbol
Returns a symbol representation corresponding to the upcase +id+ attribute.
401 402 403 |
# File 'lib/money/currency.rb', line 401 def to_sym id.to_s.upcase.to_sym end |