Class: DawandaConverter::Money
- Inherits:
-
Object
- Object
- DawandaConverter::Money
- Defined in:
- lib/dawanda_converter/money.rb
Constant Summary collapse
- @@conversions =
Declared as a Class variable so all objects (Money) created have the same conversion rates available
Hash.new
Class Method Summary collapse
-
.conversion_rates(currency = "", hash = {}) ⇒ Object
————————————————————————–#.
Instance Method Summary collapse
- #*(y) ⇒ Object
-
#+(y) ⇒ Object
————-OPERATORS SECTION——————————————-#.
- #-(y) ⇒ Object
- #/(y) ⇒ Object
- #<(y) ⇒ Object
- #==(y) ⇒ Object
- #>(y) ⇒ Object
-
#amount ⇒ Object
——————–ACCESSORS———————————————#.
- #amount=(amount) ⇒ Object
- #conversions ⇒ Object
- #convert_to(currency) ⇒ Object
- #currency ⇒ Object
- #currency=(currency) ⇒ Object
-
#initialize(amount, currency) ⇒ Money
constructor
A new instance of Money.
- #inspect ⇒ Object
Constructor Details
#initialize(amount, currency) ⇒ Money
Returns a new instance of Money.
5 6 7 8 |
# File 'lib/dawanda_converter/money.rb', line 5 def initialize(amount, currency) @amount = ( amount || 0 ) #if nil it just assigns ZERO @currency = currency end |
Class Method Details
.conversion_rates(currency = "", hash = {}) ⇒ Object
————————————————————————–#
35 36 37 |
# File 'lib/dawanda_converter/money.rb', line 35 def self.conversion_rates(currency="", hash={}) @@conversions[currency] = hash unless currency.empty? || hash.empty? end |
Instance Method Details
#*(y) ⇒ Object
71 72 73 |
# File 'lib/dawanda_converter/money.rb', line 71 def *(y) return "#{self.amount * y} #{self.currency}" end |
#+(y) ⇒ Object
————-OPERATORS SECTION——————————————-#
50 51 52 53 54 55 56 |
# File 'lib/dawanda_converter/money.rb', line 50 def +(y) if self.currency != y.currency y.convert_to(self.currency) end return "#{self.amount + y.amount} #{self.currency}" end |
#-(y) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/dawanda_converter/money.rb', line 58 def -(y) if self.currency != y.currency y.convert_to(self.currency) end return "#{self.amount - y.amount} #{self.currency}" end |
#/(y) ⇒ Object
66 67 68 69 |
# File 'lib/dawanda_converter/money.rb', line 66 def /(y) return "You cannot devide by ZERO!." if y == 0 return "#{self.amount / y} #{self.currency}" end |
#<(y) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/dawanda_converter/money.rb', line 80 def <(y) if self.currency != y.currency y.convert_to(self.currency) end return true if self.amount < y.amount return false end |
#==(y) ⇒ Object
75 76 77 78 |
# File 'lib/dawanda_converter/money.rb', line 75 def ==(y) return true if self.amount == y.amount && self.currency == y.currency return false end |
#>(y) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/dawanda_converter/money.rb', line 89 def >(y) if self.currency != y.currency y.convert_to(self.currency) end return true if self.amount > y.amount return false end |
#amount ⇒ Object
——————–ACCESSORS———————————————#
10 11 12 |
# File 'lib/dawanda_converter/money.rb', line 10 def amount @amount end |
#amount=(amount) ⇒ Object
22 23 24 |
# File 'lib/dawanda_converter/money.rb', line 22 def amount=(amount) @amount = (amount || 0 ) end |
#conversions ⇒ Object
30 31 32 |
# File 'lib/dawanda_converter/money.rb', line 30 def conversions @@conversions end |
#convert_to(currency) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/dawanda_converter/money.rb', line 39 def convert_to(currency) unless @@conversions.empty? || @@conversions[self.currency].nil? self.amount = @@conversions[self.currency][currency].to_f * self.amount self.currency = currency else puts "You must initialize conversion rates first." end end |
#currency ⇒ Object
14 15 16 |
# File 'lib/dawanda_converter/money.rb', line 14 def currency @currency end |
#currency=(currency) ⇒ Object
18 19 20 |
# File 'lib/dawanda_converter/money.rb', line 18 def currency=(currency) @currency = currency end |
#inspect ⇒ Object
26 27 28 |
# File 'lib/dawanda_converter/money.rb', line 26 def inspect "#{@amount} #{@currency}" end |