Class: Money
- Inherits:
-
Object
- Object
- Money
- Defined in:
- lib/money.rb
Constant Summary collapse
- @@base_currency =
Set defaults
'EUR'- @@conversion_rates =
{ 'EUR' => 1, 'USD' => 1.11, 'Bitcoin' => 0.0047 }
Instance Attribute Summary collapse
-
#amount ⇒ Object
Returns the value of attribute amount.
-
#currency ⇒ Object
Returns the value of attribute currency.
Class Method Summary collapse
- .conversion_rates(base_currency, conversion_rates) ⇒ Object
- .getRate(from_currency, to_currency) ⇒ Object
Instance Method Summary collapse
- #*(number) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(number) ⇒ Object
- #<(other) ⇒ Object
-
#==(other) ⇒ Object
Comparisons.
- #>(other) ⇒ Object
-
#convert_to(to_currency) ⇒ Object
Convert method.
-
#initialize(amount, currency) ⇒ Money
constructor
Constructor.
- #inspect ⇒ Object
-
#other_amount(other) ⇒ Object
Arithmetics.
- #to_s ⇒ Object
Constructor Details
#initialize(amount, currency) ⇒ Money
Constructor
13 14 15 16 |
# File 'lib/money.rb', line 13 def initialize(amount, currency) @amount = amount.to_f @currency = currency end |
Instance Attribute Details
#amount ⇒ Object
Returns the value of attribute amount.
2 3 4 |
# File 'lib/money.rb', line 2 def amount @amount end |
#currency ⇒ Object
Returns the value of attribute currency.
2 3 4 |
# File 'lib/money.rb', line 2 def currency @currency end |
Class Method Details
.conversion_rates(base_currency, conversion_rates) ⇒ Object
18 19 20 21 |
# File 'lib/money.rb', line 18 def self.conversion_rates(base_currency, conversion_rates) @@base_currency = base_currency @@conversion_rates = conversion_rates end |
.getRate(from_currency, to_currency) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/money.rb', line 83 def self.getRate(from_currency, to_currency) return 1 if to_currency == from_currency if !@@conversion_rates[to_currency].nil? && from_currency == @@base_currency return @@conversion_rates[to_currency] end if !@@conversion_rates[from_currency].nil? && to_currency == @@base_currency return 1.0 / @@conversion_rates[from_currency] end end |
Instance Method Details
#*(number) ⇒ Object
51 52 53 |
# File 'lib/money.rb', line 51 def *(number) Money.new(self.amount * number.to_f, self.currency) end |
#+(other) ⇒ Object
43 44 45 |
# File 'lib/money.rb', line 43 def +(other) Money.new(self.amount + other_amount(other), self.currency) end |
#-(other) ⇒ Object
47 48 49 |
# File 'lib/money.rb', line 47 def -(other) Money.new(self.amount - other_amount(other), self.currency) end |
#/(number) ⇒ Object
55 56 57 |
# File 'lib/money.rb', line 55 def /(number) Money.new(self.amount / number.to_f, self.currency) end |
#<(other) ⇒ Object
70 71 72 73 |
# File 'lib/money.rb', line 70 def <(other) temp = other.convert_to(self.currency) self.amount.round(2) < temp.amount.round(2) end |
#==(other) ⇒ Object
Comparisons
60 61 62 63 |
# File 'lib/money.rb', line 60 def ==(other) temp = other.convert_to(self.currency) self.amount.round(2) == temp.amount.round(2) end |
#>(other) ⇒ Object
65 66 67 68 |
# File 'lib/money.rb', line 65 def >(other) temp = other.convert_to(self.currency) self.amount.round(2) > temp.amount.round(2) end |
#convert_to(to_currency) ⇒ Object
Convert method
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/money.rb', line 24 def convert_to(to_currency) if @@conversion_rates[to_currency].nil? puts 'Rate not found' else from_currency = self.currency rate = Money.getRate(from_currency, to_currency) converted_amount = self.amount * rate Money.new(converted_amount, to_currency) end end |
#inspect ⇒ Object
75 76 77 |
# File 'lib/money.rb', line 75 def inspect sprintf('%.2f %s', amount, currency) end |
#other_amount(other) ⇒ Object
Arithmetics
38 39 40 41 |
# File 'lib/money.rb', line 38 def other_amount(other) rate = Money.getRate(other.currency, self.currency) other.amount * rate end |
#to_s ⇒ Object
79 80 81 |
# File 'lib/money.rb', line 79 def to_s sprintf('%.2f %s', amount, currency) end |