Class: Money

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount = 0, currency = 'EUR') ⇒ Money

Returns a new instance of Money.



8
9
10
11
12
13
14
15
# File 'lib/money.rb', line 8

def initialize(amount = 0, currency = 'EUR')
  @currency = currency
  @amount = amount
  @conversions = {
    'USD' => 1.11,
    'Bitcoin' => 0.0047
  }
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



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

def amount
  @amount
end

#conversionsObject

Returns the value of attribute conversions.



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

def conversions
  @conversions
end

#currencyObject

Returns the value of attribute currency.



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

def currency
  @currency
end

Instance Method Details

#*(other) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/money.rb', line 58

def *(other)
  if other.is_a?(MoneyHdamico)
    other.convert_to(currency) if other.currency != currency
    MoneyHdamico.new(amount * other.amount, currency)
  elsif other.is_a?(Numeric)
    MoneyHdamico.new(amount * other, currency)
  else
    raise 'Cannot convert String into Numeric'
  end
end

#+(other) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/money.rb', line 36

def +(other)
  if other.is_a?(MoneyHdamico)
    other.convert_to(currency) if other.currency != currency
    MoneyHdamico.new(amount + other.amount, currency)
  elsif other.is_a?(Numeric)
    MoneyHdamico.new(amount + other, currency)
  else
    raise 'Cannot convert String into Numeric'
  end
end

#-(other) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/money.rb', line 25

def -(other)
  if other.is_a?(MoneyHdamico)
    other.convert_to(currency) if other.currency != currency
    MoneyHdamico.new(amount - other.amount, currency)
  elsif other.is_a?(Numeric)
    MoneyHdamico.new(amount - other, currency)
  else
    raise 'Cannot convert String into Numeric'
  end
end

#/(other) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/money.rb', line 47

def /(other)
  if other.is_a?(MoneyHdamico)
    other.convert_to(currency) if other.currency != currency
    MoneyHdamico.new(amount / other.amount, currency)
  elsif other.is_a?(Numeric)
    MoneyHdamico.new(amount / other, currency)
  else
    raise 'Cannot convert String into Numeric'
  end
end

#<(other) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/money.rb', line 69

def <(other)
  if other.is_a?(MoneyHdamico)
    other.convert_to(currency) if other.currency != currency
    return true if amount < other.amount
  end
  false
end

#==(other) ⇒ Object



85
86
87
88
89
90
# File 'lib/money.rb', line 85

def ==(other)
  if other.is_a?(MoneyHdamico)
    return true if amount == other.amount && currency == other.currency
  end
  false
end

#>(other) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/money.rb', line 77

def >(other)
  if other.is_a?(MoneyHdamico)
    other.convert_to(currency) if other.currency != currency
    return true if amount > other.amount
  end
  false
end

#convert_to(currency) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/money.rb', line 17

def convert_to(currency)
  return raise 'Incorrect currency' unless self.currency != currency || @conversions.include?(currency)

  self.amount = (@conversions[currency] * amount).round(2)
  self.currency = currency
  self
end