Class: Money

Inherits:
Object
  • Object
show all
Includes:
Operations
Defined in:
lib/smart_money/money.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Operations

#*, #+, #-, #/, #<=>, #==, #convert_to, #inspect

Constructor Details

#initialize(amount = nil, currency = nil) ⇒ Money

Returns a new instance of Money.



9
10
11
12
# File 'lib/smart_money/money.rb', line 9

def initialize(amount = nil , currency = nil)
  @amount = amount
  @currency = currency.upcase if !currency.nil?
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



7
8
9
# File 'lib/smart_money/money.rb', line 7

def amount
  @amount
end

#currencyObject

Returns the value of attribute currency.



7
8
9
# File 'lib/smart_money/money.rb', line 7

def currency
  @currency
end

Class Method Details

.conversion_rates(currency, rates) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/smart_money/money.rb', line 16

def conversion_rates(currency, rates)
  if !currency.nil? && !rates.nil?
    @conversion_rates_table = {} if @conversion_rates_table.nil?
    rates.each do |curr,value|
      @conversion_rates_table[ [currency.upcase, curr.upcase] ] = value
      @conversion_rates_table[ [curr.upcase, currency.upcase] ] = (1/value.to_f).round(2)
      @conversion_rates_table[ [curr.upcase, curr.upcase] ] = 1
      @conversion_rates_table[ [currency.upcase, currency.upcase] ] = 1
    end
  else
    raise ArgumentError, "currency or rates attributes can't be be blank"
  end
end

.convert(amount, currency, to_currency) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/smart_money/money.rb', line 30

def convert(amount, currency, to_currency)
  if !@conversion_rates_table[[currency.upcase, to_currency.upcase]].nil?
    @conversion_rates_table[ [currency.upcase, to_currency.upcase]] * amount
  else
    raise ArgumentError, "conversion rate is not defined for '#{currency}'"
  end
end