Class: Money::Currency

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(currency_iso) ⇒ Currency

Returns a new instance of Currency.

Raises:



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/money/currency.rb', line 59

def initialize(currency_iso)
  data = self.class.currencies[currency_iso]
  if data.nil? && Money::Config.current.experimental_crypto_currencies
    data = self.class.crypto_currencies[currency_iso]
  end
  if data.nil?
    custom_path = Money::Config.current.experimental_custom_currency_path
    data = self.class.custom_currencies(custom_path)[currency_iso] if custom_path
  end

  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_markObject (readonly)

Returns the value of attribute decimal_mark.



48
49
50
# File 'lib/money/currency.rb', line 48

def decimal_mark
  @decimal_mark
end

#disambiguate_symbolObject (readonly)

Returns the value of attribute disambiguate_symbol.



48
49
50
# File 'lib/money/currency.rb', line 48

def disambiguate_symbol
  @disambiguate_symbol
end

#iso_codeObject (readonly) Also known as: to_s

Returns the value of attribute iso_code.



48
49
50
# File 'lib/money/currency.rb', line 48

def iso_code
  @iso_code
end

#iso_numericObject (readonly)

Returns the value of attribute iso_numeric.



48
49
50
# File 'lib/money/currency.rb', line 48

def iso_numeric
  @iso_numeric
end

#minor_unitsObject (readonly)

Returns the value of attribute minor_units.



48
49
50
# File 'lib/money/currency.rb', line 48

def minor_units
  @minor_units
end

#nameObject (readonly)

Returns the value of attribute name.



48
49
50
# File 'lib/money/currency.rb', line 48

def name
  @name
end

#smallest_denominationObject (readonly)

Returns the value of attribute smallest_denomination.



48
49
50
# File 'lib/money/currency.rb', line 48

def smallest_denomination
  @smallest_denomination
end

#subunit_symbolObject (readonly)

Returns the value of attribute subunit_symbol.



48
49
50
# File 'lib/money/currency.rb', line 48

def subunit_symbol
  @subunit_symbol
end

#subunit_to_unitObject (readonly)

Returns the value of attribute subunit_to_unit.



48
49
50
# File 'lib/money/currency.rb', line 48

def subunit_to_unit
  @subunit_to_unit
end

#symbolObject (readonly)

Returns the value of attribute symbol.



48
49
50
# File 'lib/money/currency.rb', line 48

def symbol
  @symbol
end

Class Method Details

.crypto_currenciesObject



30
31
32
# File 'lib/money/currency.rb', line 30

def crypto_currencies
  @@crypto_currencies ||= Loader.load_crypto_currencies
end

.currenciesObject



26
27
28
# File 'lib/money/currency.rb', line 26

def currencies
  @@currencies ||= Loader.load_currencies
end

.custom_currencies(path) ⇒ Object



34
35
36
37
# File 'lib/money/currency.rb', line 34

def custom_currencies(path)
  @@custom_currencies_cache ||= {}
  @@custom_currencies_cache[path] ||= Loader.load_custom_currencies(path)
end

.find(currency_iso) ⇒ Object



20
21
22
23
24
# File 'lib/money/currency.rb', line 20

def find(currency_iso)
  new(currency_iso)
rescue UnknownCurrency
  nil
end

.new(currency_iso) ⇒ Object Also known as: find!

Raises:



13
14
15
16
17
# File 'lib/money/currency.rb', line 13

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

.reset_custom_currenciesObject



39
40
41
# File 'lib/money/currency.rb', line 39

def reset_custom_currencies
  @@custom_currencies_cache = nil
end

.reset_loaded_currenciesObject



43
44
45
# File 'lib/money/currency.rb', line 43

def reset_loaded_currencies
  @@loaded_currencies = {}
end

Instance Method Details

#compatible?(other) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/money/currency.rb', line 91

def compatible?(other)
  other.is_a?(NullCurrency) || eql?(other)
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


83
84
85
# File 'lib/money/currency.rb', line 83

def eql?(other)
  self.class == other.class && iso_code == other.iso_code
end

#hashObject



87
88
89
# File 'lib/money/currency.rb', line 87

def hash
  [self.class, iso_code].hash
end