Class: Mint::Currency

Inherits:
Data
  • Object
show all
Defined in:
lib/minting/mint/currency.rb

Overview

Represents a specific currency unit, identified by ISO 4217 alphabetic code

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code:, symbol:, subunit: 0, priority: 0, country: nil, name: nil) ⇒ Currency

Returns a new instance of Currency.



9
10
11
12
13
14
15
16
# File 'lib/minting/mint/currency.rb', line 9

def initialize(code:, symbol:, subunit: 0, priority: 0, country: nil, name: nil)
  subunit = subunit.to_i
  priority = priority.to_i
  fractional_multiplier = 10**subunit
  minimum_amount = Rational(1, fractional_multiplier)
  super(code:, subunit:, symbol:, priority:, country:, name:,
        fractional_multiplier:, minimum_amount:)
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code

Returns:

  • (Object)

    the current value of code



7
8
9
# File 'lib/minting/mint/currency.rb', line 7

def code
  @code
end

#countryObject (readonly)

Returns the value of attribute country

Returns:

  • (Object)

    the current value of country



7
8
9
# File 'lib/minting/mint/currency.rb', line 7

def country
  @country
end

#fractional_multiplierObject (readonly)

Returns the value of attribute fractional_multiplier

Returns:

  • (Object)

    the current value of fractional_multiplier



7
8
9
# File 'lib/minting/mint/currency.rb', line 7

def fractional_multiplier
  @fractional_multiplier
end

#minimum_amountObject (readonly)

Returns the value of attribute minimum_amount

Returns:

  • (Object)

    the current value of minimum_amount



7
8
9
# File 'lib/minting/mint/currency.rb', line 7

def minimum_amount
  @minimum_amount
end

#nameObject (readonly)

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



7
8
9
# File 'lib/minting/mint/currency.rb', line 7

def name
  @name
end

#priorityObject (readonly)

Returns the value of attribute priority

Returns:

  • (Object)

    the current value of priority



7
8
9
# File 'lib/minting/mint/currency.rb', line 7

def priority
  @priority
end

#subunitObject (readonly)

Returns the value of attribute subunit

Returns:

  • (Object)

    the current value of subunit



7
8
9
# File 'lib/minting/mint/currency.rb', line 7

def subunit
  @subunit
end

#symbolObject (readonly)

Returns the value of attribute symbol

Returns:

  • (Object)

    the current value of symbol



7
8
9
# File 'lib/minting/mint/currency.rb', line 7

def symbol
  @symbol
end

Instance Method Details

#inspectObject



18
# File 'lib/minting/mint/currency.rb', line 18

def inspect = "<Currency:(#{code} #{symbol} #{subunit} #{name})>"

#normalize_amount(amount) ⇒ Object

Normalizes numeric amounts for this currency

  1. Converts to Rational

  2. Rounds to respect currency subunit



23
# File 'lib/minting/mint/currency.rb', line 23

def normalize_amount(amount) = amount.to_r.round(subunit)