class TqCurencyManager::Money

Constants

BITCOIN_FOR_EUR
USD_FOR_EUR

Init & getters

Public Class Methods

conversion_rates(base_curency, rates) click to toggle source

Setters

# File lib/tq_curency_manager.rb, line 52
def self.conversion_rates base_curency, rates
        @@rates[base_curency] = 1
        rates.each do |k,v|
                @@rates[k] = v.to_f
        end
        @@rates
end
new(amount, curency) click to toggle source
# File lib/tq_curency_manager.rb, line 16
def initialize amount, curency
        @amount = amount.to_f
        @curency = curency
        if !@@rates.key? curency
                raise ArgumentError.new("Not valid curency")
        end
end
rates() click to toggle source
# File lib/tq_curency_manager.rb, line 24
def self.rates
        @@rates
end

Public Instance Methods

*(float) click to toggle source
# File lib/tq_curency_manager.rb, line 87
def *(float)
        amount = self.amount * float
        self.class.new(amount, self.curency)
end
+(instance) click to toggle source
# File lib/tq_curency_manager.rb, line 77
def +(instance)
        amount = self.amount + instance.convert_to(self.curency).amount
        self.class.new(amount, self.curency)
end
-(instance) click to toggle source
# File lib/tq_curency_manager.rb, line 82
def -(instance)
        amount = self.amount - instance.convert_to(self.curency).amount
        self.class.new(amount, self.curency)
end
/(float) click to toggle source
# File lib/tq_curency_manager.rb, line 92
def /(float)
        amount = self.amount / float
        self.class.new(amount, self.curency)
end
<(instance) click to toggle source
# File lib/tq_curency_manager.rb, line 102
  def <(instance)
          amount = instance.convert_to(self.curency).amount
          self.amount < amount
end
==(instance) click to toggle source

Comparaison methods

# File lib/tq_curency_manager.rb, line 73
def ==(instance)
        self.convert_to('EUR').inspect == instance.convert_to('EUR').inspect
end
>(instance) click to toggle source
# File lib/tq_curency_manager.rb, line 97
   def >(instance)
           amount = instance.convert_to(self.curency).amount
           self.amount > amount
end
amount() click to toggle source
# File lib/tq_curency_manager.rb, line 28
def amount
        # we just give different rounding with bitcoin
        if curency == 'Bitcoin'
                @amount.round(8)
        else
                @amount.round(2)
        end
end
convert_to(curency) click to toggle source

Curency conversion

# File lib/tq_curency_manager.rb, line 62
def convert_to(curency)
        if @@rates[curency]
                self.class.new(amount_convert(curency), curency)
        else
                puts 'error : you have to initialize the curency convertion for ' + curency
                nil
        end
end
curency() click to toggle source
# File lib/tq_curency_manager.rb, line 37
def curency
        @curency
end
inspect() click to toggle source

'%.Xf' % allows to have X digits after the point

# File lib/tq_curency_manager.rb, line 42
def inspect
        if curency == 'Bitcoin'
                '%.8f' % amount + " " + curency
        else
                '%.2f' % amount + " " + curency
        end
end

Private Instance Methods

amount_convert(new_curency) click to toggle source
# File lib/tq_curency_manager.rb, line 109
def amount_convert(new_curency)
        curency_rate = @@rates[curency]
        new_curency_rate = @@rates[new_curency]
        amount = @amount.to_f

        ((amount * ( new_curency_rate / curency_rate )))
end