class MoneyConvert

Class for converting money from one currency to another and perform some arthimetic operations on two MoneyConvert objects.

Constants

VERSION

Attributes

amount[RW]
base_currency[RW]
currency[RW]
to_currencies[RW]

Public Class Methods

conversion_rates(base_currency, to_currencies) click to toggle source

Sets the +@@base_currency+ and +@@to_currencies+ class valiables

# File lib/money-convert/money-convert.rb, line 110
def self.conversion_rates(base_currency, to_currencies)
  conversion_rates = MoneyConvert::ConversionRates.new(base_currency, to_currencies)
  @@base_currency = conversion_rates.base_currency
  @@to_currencies = conversion_rates.to_currencies
end
new(amount, currency) click to toggle source
# File lib/money-convert/money-convert.rb, line 8
def initialize(amount, currency)
  @amount = amount
  @currency = currency
end

Public Instance Methods

*(other) click to toggle source

Return multiplication of two MoneyConvert object, if other object has different currency it will get converted into this object currency For Example:

MoneyConvert.new(50, 'EUR') * MoneyConvert.new(20, 'USD')
  => returns amount in EUR currency
# File lib/money-convert/money-convert.rb, line 61
def *(other)
  return unless other.is_a?(Integer)

  amount.to_i * other
end
/(other) click to toggle source

Return division of two MoneyConvert object, if other object has different currency it will get converted into this object currency For Example:

MoneyConvert.new(50, 'EUR') / MoneyConvert.new(20, 'USD')
  => returns amount in EUR currency
# File lib/money-convert/money-convert.rb, line 50
def /(other)
  return unless other.is_a?(Integer)

  amount.to_i / other
end
<(other) click to toggle source

Return true if other object amount is less than this object amount, and the currency should be same For Example:

MoneyConvert.new(20, 'EUR') < MoneyConvert.new(30, 'EUR')
  => returns boolean
# File lib/money-convert/money-convert.rb, line 97
def <(other)
  if amount < other.amount && currency == other.currency
    true
  elsif currency != other.currency
    converted_amount = convert(currency)

    true if amount < converted_amount
  else
    false
  end
end
==(other) click to toggle source

Return true if other object and this object has same amount and currency For Example:

MoneyConvert.new(50, 'EUR') == MoneyConvert.new(50, 'EUR')
  => returns boolean
# File lib/money-convert/money-convert.rb, line 71
def ==(other)
  if amount == other.amount && currency == other.currency
    true
  else
    false
  end
end
>(other) click to toggle source

Return true if other object amount is gtater than this object amount, and the currency should be same For Example:

MoneyConvert.new(50, 'EUR') > MoneyConvert.new(30, 'EUR')
  => returns boolean
# File lib/money-convert/money-convert.rb, line 84
def >(other)
  if amount > other.amount && currency == other.currency
    true
  else
    false
  end
end
convert_to(to_currency) click to toggle source

Returns class object with converted currency values For example:

fifty_eur = MoneyConvert.new(50, 'EUR')
fifty_eur_in_usd = fifty_eur.convert_to('USD')
  =>   fifty_eur_in_usd.currency
  =>   fifty_eur_in_usd.amount
# File lib/money-convert/money-convert.rb, line 23
def convert_to(to_currency)
  return unless validate_currency(to_currency) && @@base_currency == currency

  toal_amount = convert(to_currency)
  self.class.new(toal_amount, to_currency)
end
inspect() click to toggle source
# File lib/money-convert/money-convert.rb, line 13
def inspect
  "#{@amount} #{@currency}"
end

Private Instance Methods

convert(to_currency) click to toggle source

Returns converted currency

# File lib/money-convert/money-convert.rb, line 119
def convert(to_currency)
  (@amount.to_i * @@to_currencies.fetch(to_currency, 1)).round(2)
end
validate_currency(to_currency) click to toggle source

Checks wether the to_currency is present in the given +@@to_curencies+ hash

# File lib/money-convert/money-convert.rb, line 124
def validate_currency(to_currency)
  @@to_currencies&.keys&.include?(to_currency) || to_currency == @@base_currency
end