Class: Money::Currency
- Inherits:
-
Object
- Object
- Money::Currency
- Defined in:
- lib/money/currency.rb,
lib/money/currency/loader.rb
Defined Under Namespace
Modules: Loader Classes: UnknownCurrency
Constant Summary collapse
- @@mutex =
Mutex.new
- @@loaded_currencies =
{}
Instance Attribute Summary collapse
-
#decimal_mark ⇒ Object
readonly
Returns the value of attribute decimal_mark.
-
#disambiguate_symbol ⇒ Object
readonly
Returns the value of attribute disambiguate_symbol.
-
#iso_code ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute iso_code.
-
#iso_numeric ⇒ Object
readonly
Returns the value of attribute iso_numeric.
-
#minor_units ⇒ Object
readonly
Returns the value of attribute minor_units.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#smallest_denomination ⇒ Object
readonly
Returns the value of attribute smallest_denomination.
-
#subunit_symbol ⇒ Object
readonly
Returns the value of attribute subunit_symbol.
-
#subunit_to_unit ⇒ Object
readonly
Returns the value of attribute subunit_to_unit.
-
#symbol ⇒ Object
readonly
Returns the value of attribute symbol.
Class Method Summary collapse
Instance Method Summary collapse
- #compatible?(other) ⇒ Boolean
- #eql?(other) ⇒ Boolean (also: #==)
- #hash ⇒ Object
-
#initialize(currency_iso) ⇒ Currency
constructor
A new instance of Currency.
Constructor Details
#initialize(currency_iso) ⇒ Currency
Returns a new instance of Currency.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/money/currency.rb', line 33 def initialize(currency_iso) data = self.class.currencies[currency_iso] raise UnknownCurrency, "Invalid iso4217 currency '#{currency_iso}'" unless data @symbol = data['symbol'] @disambiguate_symbol = data['disambiguate_symbol'] || data['symbol'] @subunit_symbol = data['subunit_symbol'] @iso_code = data['iso_code'] @iso_numeric = data['iso_numeric'] @name = data['name'] @smallest_denomination = data['smallest_denomination'] @subunit_to_unit = data['subunit_to_unit'] @decimal_mark = data['decimal_mark'] @minor_units = subunit_to_unit == 0 ? 0 : Math.log(subunit_to_unit, 10).round.to_i freeze end |
Instance Attribute Details
#decimal_mark ⇒ Object (readonly)
Returns the value of attribute decimal_mark.
30 31 32 |
# File 'lib/money/currency.rb', line 30 def decimal_mark @decimal_mark end |
#disambiguate_symbol ⇒ Object (readonly)
Returns the value of attribute disambiguate_symbol.
30 31 32 |
# File 'lib/money/currency.rb', line 30 def disambiguate_symbol @disambiguate_symbol end |
#iso_code ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute iso_code.
30 31 32 |
# File 'lib/money/currency.rb', line 30 def iso_code @iso_code end |
#iso_numeric ⇒ Object (readonly)
Returns the value of attribute iso_numeric.
30 31 32 |
# File 'lib/money/currency.rb', line 30 def iso_numeric @iso_numeric end |
#minor_units ⇒ Object (readonly)
Returns the value of attribute minor_units.
30 31 32 |
# File 'lib/money/currency.rb', line 30 def minor_units @minor_units end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
30 31 32 |
# File 'lib/money/currency.rb', line 30 def name @name end |
#smallest_denomination ⇒ Object (readonly)
Returns the value of attribute smallest_denomination.
30 31 32 |
# File 'lib/money/currency.rb', line 30 def smallest_denomination @smallest_denomination end |
#subunit_symbol ⇒ Object (readonly)
Returns the value of attribute subunit_symbol.
30 31 32 |
# File 'lib/money/currency.rb', line 30 def subunit_symbol @subunit_symbol end |
#subunit_to_unit ⇒ Object (readonly)
Returns the value of attribute subunit_to_unit.
30 31 32 |
# File 'lib/money/currency.rb', line 30 def subunit_to_unit @subunit_to_unit end |
#symbol ⇒ Object (readonly)
Returns the value of attribute symbol.
30 31 32 |
# File 'lib/money/currency.rb', line 30 def symbol @symbol end |
Class Method Details
.currencies ⇒ Object
25 26 27 |
# File 'lib/money/currency.rb', line 25 def currencies @@currencies ||= Loader.load_currencies end |
.find(currency_iso) ⇒ Object
19 20 21 22 23 |
# File 'lib/money/currency.rb', line 19 def find(currency_iso) new(currency_iso) rescue UnknownCurrency nil end |
.new(currency_iso) ⇒ Object Also known as: find!
12 13 14 15 16 |
# File 'lib/money/currency.rb', line 12 def new(currency_iso) raise UnknownCurrency, "Currency can't be blank" if currency_iso.nil? || currency_iso.to_s.empty? iso = currency_iso.to_s.downcase @@loaded_currencies[iso] || @@mutex.synchronize { @@loaded_currencies[iso] = super(iso) } end |
Instance Method Details
#compatible?(other) ⇒ Boolean
57 58 59 |
# File 'lib/money/currency.rb', line 57 def compatible?(other) other.is_a?(NullCurrency) || eql?(other) end |
#eql?(other) ⇒ Boolean Also known as: ==
49 50 51 |
# File 'lib/money/currency.rb', line 49 def eql?(other) self.class == other.class && iso_code == other.iso_code end |
#hash ⇒ Object
53 54 55 |
# File 'lib/money/currency.rb', line 53 def hash [ self.class, iso_code ].hash end |