class ConversMoney

ConversMoney it's a Money Conversor given by fixed currency and change rates of your choice.

Constants

VERSION

Attributes

amount[RW]
currency[RW]

Public Class Methods

conversion_rates(currency, conversion_rates) click to toggle source
# File lib/convers_money.rb, line 15
def self.conversion_rates(currency, conversion_rates)
  @conversion_rates ||= {}
  conversion_rates = Hash[conversion_rates.map { |fixed_currency, rates| [fixed_currency.upcase, rates] }]
  @conversion_rates.merge!(currency.upcase => conversion_rates)
  unless @conversion_rates.nil? || conversion_rates.nil?
    @conversion_rates.each do |fixed_currency, rates|
      rates.map do |currency_rate, value|
        inverse_conversion = { fixed_currency => format('%.5f', (1 / value)).to_f.round(5) }
        @conversion_rates = @conversion_rates.merge( currency_rate => inverse_conversion ) unless currency.eql? currency_rate
      end
    end
  end
end
convert!(amount, from_currency, to_currency) click to toggle source
# File lib/convers_money.rb, line 41
def self.convert!(amount, from_currency, to_currency)
  get_rate!(from_currency, to_currency) * amount
end
get_rate!(from_currency, to_currency) click to toggle source
# File lib/convers_money.rb, line 33
def self.get_rate!(from_currency, to_currency)
  rates = @conversion_rates[from_currency]
  raise "no convention rates from #{from_currency}" unless rates
  rate = rates[to_currency]
  raise "no conversion rates to #{to_currency}" unless rate
  rate
end
new(amount, currency) click to toggle source
# File lib/convers_money.rb, line 9
def initialize(amount, currency)
  @amount = amount
  @currency = currency.upcase
  ConversMoney.validate!
end
validate!() click to toggle source
# File lib/convers_money.rb, line 29
def self.validate!
  raise 'not configured' unless @conversion_rates&.any?
end

Private Class Methods

conversion_rates_access() click to toggle source
# File lib/convers_money.rb, line 132
def self.conversion_rates_access
  @conversion_rates
end

Public Instance Methods

*(other) click to toggle source
# File lib/convers_money.rb, line 99
def *(other)
  other = convert_to_reference_currency(other, currency)
  total_amount = amount * other.amount
  result = ConversMoney.new(total_amount.round(2), currency)
  if other.amount.is_a? Integer
    result.amount = result.amount.to_i
    result
  else
    result
  end
end
+(other) click to toggle source

def resut_conversion(other, currency, total_amount)

result = ConversMoney.new(total_amount.round(2), currency)
if other.amount.is_a? Integer
  result.amount = result.amount.to_i
  result
end

end

# File lib/convers_money.rb, line 75
def +(other)
  other = convert_to_reference_currency(other, currency)
  total_amount = amount + other.amount
  result = ConversMoney.new(total_amount.round(2), currency)
  if other.amount.is_a? Integer
    result.amount = result.amount.to_i
    result
  else
    result
  end
end
-(other) click to toggle source
# File lib/convers_money.rb, line 87
def -(other)
  other = convert_to_reference_currency(other, currency)
  total_amount = amount - other.amount
  result = ConversMoney.new(total_amount.round(2), currency)
  if other.amount.is_a? Integer
    result.amount = result.amount.to_i
    result
  else
    result
  end
end
/(other) click to toggle source
# File lib/convers_money.rb, line 111
def /(other)
  other = convert_to_reference_currency(other, currency)
  total_amount = amount / other.amount
  result = ConversMoney.new(total_amount.round(2), currency)
  if other.amount.is_a? Integer
    result.amount = result.amount.to_i
    result
  else
    result
  end
end
<=>(other) click to toggle source
# File lib/convers_money.rb, line 123
def <=>(other)
  if other.is_a? Float
    other = ConversMoney.new( other.round(2), currency)
  else
    other = convert_to_reference_currency(other, currency)
  end
  amount.round(2) <=> other.amount.round(2)
end
convert_to(currency) click to toggle source
# File lib/convers_money.rb, line 45
def convert_to(currency)
  if self.currency.eql?(currency)
    self
  else
    converted_amount = ConversMoney.convert!(@amount, @currency, currency)
    amount_str = format('%.2f', converted_amount)
    ConversMoney.new(amount_str.to_f.round(2), currency)
  end
end
convert_to_reference_currency(other, currency) click to toggle source
# File lib/convers_money.rb, line 59
def convert_to_reference_currency(other, currency)
  if (other.is_a? Float ) || (other.is_a? Integer)
    ConversMoney.new( other, currency)
  else
    other.convert_to(currency)
  end
end
inspect() click to toggle source
# File lib/convers_money.rb, line 55
def inspect
  "#{format('%.2f', @amount)} #{@currency}"
end