Class: Money::NullCurrency

Inherits:
Object
  • Object
show all
Defined in:
lib/money/null_currency.rb

Overview

A placeholder currency for instances where no actual currency is available, as defined by ISO4217. You should rarely, if ever, need to use this directly. It’s here mostly for backwards compatibility and for that reason behaves like a dollar, which is how this gem worked before the introduction of currency.

Here follows a list of preferred alternatives over using Money with NullCurrency:

For comparisons where you don’t know the currency beforehand, you can use Numeric predicate methods like #positive?/#negative?/#zero?/#nonzero?. Comparison operators with Numeric (==, !=, <=, =>, <, >) work as well.

Money with NullCurrency has behaviour that may surprise you, such as database validations or GraphQL enum not allowing the string representation of NullCurrency. Prefer using Money.new(0, currency) where possible, as this sidesteps these issues and provides additional currency check safeties.

Unlike other currencies, it is allowed to calculate a Money object with NullCurrency with another currency. The resulting Money object will have the other currency.

Examples:

Money.new(1, 'CAD').positive? #=> true
Money.new(2, 'CAD') >= 0      #=> true
Money.new(0, Money::NULL_CURRENCY) + Money.new(5, 'CAD')
#=> #<Money value:5.00 currency:CAD>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNullCurrency

Returns a new instance of NullCurrency.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/money/null_currency.rb', line 39

def initialize
  @symbol                = '$'
  @disambiguate_symbol   = nil
  @subunit_symbol        = nil
  @iso_code              = 'XXX'
  @iso_numeric           = '999'
  @name                  = 'No Currency'
  @smallest_denomination = 1
  @subunit_to_unit       = 100
  @minor_units           = 2
  @decimal_mark          = '.'
  freeze
end

Instance Attribute Details

#decimal_markObject (readonly)

Returns the value of attribute decimal_mark.



36
37
38
# File 'lib/money/null_currency.rb', line 36

def decimal_mark
  @decimal_mark
end

#disambiguate_symbolObject (readonly)

Returns the value of attribute disambiguate_symbol.



36
37
38
# File 'lib/money/null_currency.rb', line 36

def disambiguate_symbol
  @disambiguate_symbol
end

#iso_codeObject (readonly)

Returns the value of attribute iso_code.



36
37
38
# File 'lib/money/null_currency.rb', line 36

def iso_code
  @iso_code
end

#iso_numericObject (readonly)

Returns the value of attribute iso_numeric.



36
37
38
# File 'lib/money/null_currency.rb', line 36

def iso_numeric
  @iso_numeric
end

#minor_unitsObject (readonly)

Returns the value of attribute minor_units.



36
37
38
# File 'lib/money/null_currency.rb', line 36

def minor_units
  @minor_units
end

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/money/null_currency.rb', line 36

def name
  @name
end

#smallest_denominationObject (readonly)

Returns the value of attribute smallest_denomination.



36
37
38
# File 'lib/money/null_currency.rb', line 36

def smallest_denomination
  @smallest_denomination
end

#subunit_symbolObject (readonly)

Returns the value of attribute subunit_symbol.



36
37
38
# File 'lib/money/null_currency.rb', line 36

def subunit_symbol
  @subunit_symbol
end

#subunit_to_unitObject (readonly)

Returns the value of attribute subunit_to_unit.



36
37
38
# File 'lib/money/null_currency.rb', line 36

def subunit_to_unit
  @subunit_to_unit
end

#symbolObject (readonly)

Returns the value of attribute symbol.



36
37
38
# File 'lib/money/null_currency.rb', line 36

def symbol
  @symbol
end

Instance Method Details

#compatible?(other) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/money/null_currency.rb', line 53

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

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

Returns:

  • (Boolean)


57
58
59
# File 'lib/money/null_currency.rb', line 57

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

#to_sObject



61
62
63
# File 'lib/money/null_currency.rb', line 61

def to_s
  ''
end