class Money

Attributes

amount[R]
currency[R]

Public Class Methods

new(amount, currency) click to toggle source

initialize the amount and currency

# File lib/dawanda_currency_conversion/dawanda_currency_conversion.rb, line 4
def initialize(amount, currency)
        @amount = amount.to_i
        @currency = currency
end

Public Instance Methods

*(x) click to toggle source
# File lib/dawanda_currency_conversion/dawanda_currency_conversion.rb, line 47
def *(x)
        Money.new(amount * x, currency)
end
+(x) click to toggle source
# File lib/dawanda_currency_conversion/dawanda_currency_conversion.rb, line 39
def +(x)
        is_curr_same(x) { |x,y| Money.new(x + y, currency)}
end
-(x) click to toggle source
# File lib/dawanda_currency_conversion/dawanda_currency_conversion.rb, line 43
def -(x)
        is_curr_same(x) { |x,y| Money.new(x - y, currency)}
end
/(x) click to toggle source
# File lib/dawanda_currency_conversion/dawanda_currency_conversion.rb, line 51
def /(x)
        Money.new(amount / x, currency)
end
<(x) click to toggle source
# File lib/dawanda_currency_conversion/dawanda_currency_conversion.rb, line 63
def <(x)
        true if currency < x.currency
        false
end
==(x) click to toggle source
# File lib/dawanda_currency_conversion/dawanda_currency_conversion.rb, line 55
def ==(x)
        true if currency == x.currency
        false if currency != x.currency
end
>(x) click to toggle source
# File lib/dawanda_currency_conversion/dawanda_currency_conversion.rb, line 59
def >(x)
        true if currency > x.currency
        false
end
changed_curr(newcurr) click to toggle source

This calles the conversation rate that was given to use and multiplies the amount by that much

# File lib/dawanda_currency_conversion/dawanda_currency_conversion.rb, line 25
def changed_curr(newcurr)
        multiplier = Money.conversion_rate(from: currency, to: newcurr)
        amount * multiplier
end
conversion_rate(from:,to:) click to toggle source
# File lib/dawanda_currency_conversion/dawanda_currency_conversion.rb, line 35
def conversion_rate(from:,to:)
        saved_data[from][to] || puts "Didn't convert correctly"
end
conversion_rates(currency, conv_rates = nil) click to toggle source

This will take the given converstion rate data and save it using the conversation_rate method

# File lib/dawanda_currency_conversion/dawanda_currency_conversion.rb, line 31
def conversion_rates(currency, conv_rates = nil)
        saved_data[currency] = conv_rates
end
convert_to(newcurr) click to toggle source

this will convert the currency using the changed_curr method

# File lib/dawanda_currency_conversion/dawanda_currency_conversion.rb, line 20
def convert_to(newcurr)
        Money.new(changed_curr(newcurr),newcurr)
end
inspect() click to toggle source

print the amount and currency

# File lib/dawanda_currency_conversion/dawanda_currency_conversion.rb, line 15
def inspect
        "#{amount} and #{currency}"
end

Private Instance Methods

is_curr_same(temp) { |amount, amount| ... } click to toggle source
# File lib/dawanda_currency_conversion/dawanda_currency_conversion.rb, line 70
def is_curr_same(temp)
        if currency == temp.currency
                yield amount, temp.amount
        else
                change = temp.convert_to(currency)
                yield amount, change.amount
        end
end