class Money

Attributes

amount[R]
currency[R]
manual_currencies[R]

Public Class Methods

new(amount, currency, manual_currencies={}) click to toggle source

use optional_rates for manual currency conversion

# File lib/puppy_money/money.rb, line 9
def initialize amount, currency, manual_currencies={}
  validate_amount amount
  validate_currency currency.try(:upcase)

  # validate manual currency unless it is a non_standard currency (e.g. bitcoin)
  manual_currencies.each do |name, info|
    validate_currency name.to_s.upcase unless info[:non_standard]
  end

  @amount                  = amount.round(2)
  @currency                = currency.try(:upcase)
  @manual_currencies       = manual_currencies
end

Public Instance Methods

conversion_amount(transfer_currency) click to toggle source
# File lib/puppy_money/money.rb, line 65
def conversion_amount transfer_currency
  convert_to(transfer_currency).amount
end
convert_to(transfer_currency, non_standard=false) click to toggle source
# File lib/puppy_money/money.rb, line 27
def convert_to transfer_currency, non_standard=false
  transfer_currency = transfer_currency.try(:upcase)
  # validate the transfer currency unless:
  ## 1. user explicitly specifies that it is a non-standard currency (e.g. bitcoin)
  validate_currency transfer_currency unless non_standard
  # use real-time exchange rates if:
  ## 1. user has not provided exchange rates manually
  ## 2  given transfer currency is not supported
  if @manual_currencies.empty? && RateApi.fetch_symbols(@currency).include?(transfer_currency)
    transfer_rate = RateApi.fetch_transfer_rate(@currency, transfer_currency)
    Money.new(@amount * transfer_rate, transfer_currency)
  end
end
inspect() click to toggle source
# File lib/puppy_money/money.rb, line 23
def inspect
  "#{@amount} #{@currency}"
end

Private Instance Methods

currency_error(currency) click to toggle source
# File lib/puppy_money/money.rb, line 88
def currency_error(currency)
  " `#{currency}` is an invalid (currency abbreviation must be a recognized 3 character string)"
end
validate_amount(amount) click to toggle source
# File lib/puppy_money/money.rb, line 71
def validate_amount amount
  # ensure that the amount is a numeric value
  raise ArgumentError, "Amount must be a number" unless amount.is_a? Numeric
end
validate_currency(currency, manual_rates=false) click to toggle source
# File lib/puppy_money/money.rb, line 76
def validate_currency currency, manual_rates=false
  if manual_rates
    currency.map do |c|
      # raise an error if manual rate is not recognized
      raise ArgumentError, currency_error(currency) unless Currencies.abbreviations.include? c.to_s.upcase
    end
  else
    # raise an error if base rate is not recognized
    raise ArgumentError, currency_error(currency) unless Currencies.abbreviations.include?(currency)
  end
end