class Monee::Money

class to manipulate currencies as required

Attributes

cents[R]
currency_code[R]

Public Class Methods

conversion_rates(base_currency, currency_rates) click to toggle source

Configures the config singleton class with default values

@param base_currency [String] @param currency_rates [Hash] @return [void]

# 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
new(amount, currency_code) click to toggle source

Initializes a money object

@param amount [Numeric] @param currency_code [String] @return [Money]

# 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

Public Instance Methods

amount() click to toggle source

@return [Numeric] passed amount of the object

# File lib/monee/money.rb, line 49
def amount
  cents.to_amount
end
currency() click to toggle source

@return [String] the currency_code of the current object

# File lib/monee/money.rb, line 44
def currency
  currency_code
end
inspect() click to toggle source

@return [String] formatted string of money

# File lib/monee/money.rb, line 54
def inspect
  "#{format('%.2f', amount)} #{currency}"
end
klass() click to toggle source

@return [Money] method to access this class

# File lib/monee/money.rb, line 39
def klass
  self.class
end

Private Instance Methods

set_currency() click to toggle source

Sets the currency object in currency instance variable

# File lib/monee/money.rb, line 70
def set_currency
  @currency ||= Currency.new(code: currency_code)
end
validate_args!() click to toggle source

Validation of money arguments

@raise [InvalidAmount] if amount is negative or not a number @raise [InvalidCurrency] if currency_code is not alphabets

# File lib/monee/money.rb, line 64
def validate_args!
  raise InvalidAmount unless @amount.is_a?(Numeric) && @amount >= 0
  raise InvalidCurrency unless @currency_code =~ /[A-Za-z]/
end