Class: MoneyConvert

Inherits:
Object
  • Object
show all
Defined in:
lib/money-convert/money-convert.rb,
lib/money-convert/version.rb,
lib/money-convert/conversion_rates.rb

Overview

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

Defined Under Namespace

Classes: ConversionRates

Constant Summary collapse

VERSION =
"0.1.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, currency) ⇒ MoneyConvert

Returns a new instance of MoneyConvert.



8
9
10
11
# File 'lib/money-convert/money-convert.rb', line 8

def initialize(amount, currency)
  @amount = amount
  @currency = currency
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



6
7
8
# File 'lib/money-convert/money-convert.rb', line 6

def amount
  @amount
end

#base_currencyObject

Returns the value of attribute base_currency.



6
7
8
# File 'lib/money-convert/money-convert.rb', line 6

def base_currency
  @base_currency
end

#currencyObject

Returns the value of attribute currency.



6
7
8
# File 'lib/money-convert/money-convert.rb', line 6

def currency
  @currency
end

#to_currenciesObject

Returns the value of attribute to_currencies.



6
7
8
# File 'lib/money-convert/money-convert.rb', line 6

def to_currencies
  @to_currencies
end

Class Method Details

.conversion_rates(base_currency, to_currencies) ⇒ Object

Sets the @@base_currency and @@to_currencies class valiables



110
111
112
113
114
# 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

Instance Method Details

#*(other) ⇒ Object

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


61
62
63
64
65
# File 'lib/money-convert/money-convert.rb', line 61

def *(other)
  return unless other.is_a?(Integer)

  amount.to_i * other
end

#/(other) ⇒ Object

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


50
51
52
53
54
# File 'lib/money-convert/money-convert.rb', line 50

def /(other)
  return unless other.is_a?(Integer)

  amount.to_i / other
end

#<(other) ⇒ Object

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


97
98
99
100
101
102
103
104
105
106
107
# 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) ⇒ Object

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


71
72
73
74
75
76
77
# File 'lib/money-convert/money-convert.rb', line 71

def ==(other)
  if amount == other.amount && currency == other.currency
    true
  else
    false
  end
end

#>(other) ⇒ Object

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


84
85
86
87
88
89
90
# 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) ⇒ Object

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


23
24
25
26
27
28
# 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

#inspectObject



13
14
15
# File 'lib/money-convert/money-convert.rb', line 13

def inspect
  "#{@amount} #{@currency}"
end