Class: Monee::Money

Inherits:
Object
  • Object
show all
Includes:
Arithmetic, Comparison, Conversion
Defined in:
lib/monee/money.rb

Overview

class to manipulate currencies as required

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Comparison

#<=>

Methods included from Conversion

#convert_to, #convert_to_cents

Methods included from Arithmetic

#choose_operand, #coerce, #operator

Constructor Details

#initialize(amount, currency_code) ⇒ Money

Initializes a money object

Parameters:

  • amount (Numeric)
  • currency_code (String)


30
31
32
33
34
35
36
# File 'lib/monee/money.rb', line 30

def initialize(amount, currency_code)
  @amount = amount
  @cents = amount.to_cents
  @currency_code = currency_code
  validate_args!
  set_currency
end

Instance Attribute Details

#centsObject (readonly)

Returns the value of attribute cents.



9
10
11
# File 'lib/monee/money.rb', line 9

def cents
  @cents
end

#currency_codeObject (readonly)

Returns the value of attribute currency_code.



9
10
11
# File 'lib/monee/money.rb', line 9

def currency_code
  @currency_code
end

Class Method Details

.conversion_rates(base_currency, currency_rates) ⇒ void

This method returns an undefined value.

Configures the config singleton class with default values

Parameters:

  • base_currency (String)
  • currency_rates (Hash)


17
18
19
20
21
22
23
# File 'lib/monee/money.rb', line 17

def conversion_rates(base_currency, currency_rates)
  Currency.configure do |config|
    config.base_currency = base_currency
    config.currency_rates = currency_rates
    config.set_default_rate
  end
end

Instance Method Details

#amountNumeric

Returns passed amount of the object.

Returns:

  • (Numeric)

    passed amount of the object



49
50
51
# File 'lib/monee/money.rb', line 49

def amount
  cents.to_amount
end

#currencyString

Returns the currency_code of the current object.

Returns:

  • (String)

    the currency_code of the current object



44
45
46
# File 'lib/monee/money.rb', line 44

def currency
  currency_code
end

#inspectString

Returns formatted string of money.

Returns:

  • (String)

    formatted string of money



54
55
56
# File 'lib/monee/money.rb', line 54

def inspect
  "#{format('%.2f', amount)} #{currency}"
end

#klassMoney

Returns method to access this class.

Returns:

  • (Money)

    method to access this class



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

def klass
  self.class
end