class Samin::Money

Money class that implements required features for currency conversion

Attributes

amount[R]
currency[R]
currency_ref_conversion_rates[W]
currency_ref_name[W]

Public Class Methods

conversion_rates(currency_name = 'EUR', rates = {}) click to toggle source
# File lib/samin/money.rb, line 80
def self.conversion_rates(currency_name = 'EUR', rates = {})
  unless currency_name.nil? || rates.nil?
    @@currency_ref_name ||= currency_name
    @@currency_ref_conversion_rates ||= rates
    return true
  end
  false
end
new(amount = 0, currency = 'CHF') click to toggle source
# File lib/samin/money.rb, line 8
def initialize(amount = 0, currency = 'CHF')
  @amount   = amount
  @currency = currency
end

Public Instance Methods

*(other) click to toggle source
# File lib/samin/money.rb, line 49
def *(other)
  Money.new((@amount * other).to_f, @currency)
end
+(other) click to toggle source
# File lib/samin/money.rb, line 23
def +(other)
  sum = -1
  if other.currency.eql? @currency
    sum = @amount + other.amount
  else
    other_converted = other.convert_from(other.currency)
    sum = other_converted.amount + @amount
  end
  Money.new(sum, @@currency_ref_name)
end
-(other) click to toggle source
# File lib/samin/money.rb, line 34
def -(other)
  sub = -1
  if other.currency.eql? @currency
    sub = @amount - other.amount
  else
    other_converted = other.convert_from(other.currency)
    sub = @amount - other_converted.amount
  end
  Money.new(sub, @@currency_ref_name)
end
/(other) click to toggle source
# File lib/samin/money.rb, line 45
def /(other)
  Money.new((@amount / other).to_f, @currency)
end
<(other) click to toggle source
# File lib/samin/money.rb, line 62
def <(other)
  if other.currency.eql? @currency
    return @amount < other.amount.round(2)
  else
    other_converted = other.convert_from(@currency)
    return @amount < other_converted.amount.round(2)
  end
end
==(other) click to toggle source
# File lib/samin/money.rb, line 53
def ==(other)
  if other.currency.eql? @currency
    return other.amount.round(2).eql? @amount.round(2)
  else
    return other.convert_to(@currency)
      .amount.round(2).eql? @amount.round(2)
  end
end
>(other) click to toggle source
# File lib/samin/money.rb, line 71
def >(other)
  if other.currency.eql? @currency
    return @amount > other.amount.round(2)
  else
    other_converted = other.convert_from(@currency)
    return @amount > other_converted.amount.round(2)
  end
end
convert_from(currency) click to toggle source
# File lib/samin/money.rb, line 89
def convert_from(currency)
  rate = @@currency_ref_conversion_rates[currency]
  Money.new((@amount / rate).to_f.round(2), currency)
end
convert_to(currency) click to toggle source
# File lib/samin/money.rb, line 17
def convert_to(currency)
  rate = @@currency_ref_conversion_rates[currency]
  return nil if rate.nil?
  Money.new((@amount * rate).to_f.round(2), currency)
end
inspect() click to toggle source
# File lib/samin/money.rb, line 13
def inspect
  "#{@amount} #{@currency}"
end