Class: Samin::Money
- Inherits:
-
Object
- Object
- Samin::Money
- Defined in:
- lib/samin/money.rb
Overview
Money class that implements required features for currency conversion
Instance Attribute Summary collapse
-
#amount ⇒ Object
readonly
Returns the value of attribute amount.
-
#currency ⇒ Object
readonly
Returns the value of attribute currency.
-
#currency_ref_conversion_rates ⇒ Object
writeonly
Sets the attribute currency_ref_conversion_rates.
-
#currency_ref_name ⇒ Object
writeonly
Sets the attribute currency_ref_name.
Class Method Summary collapse
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #/(other) ⇒ Object
- #<(other) ⇒ Object
- #==(other) ⇒ Object
- #>(other) ⇒ Object
- #convert_from(currency) ⇒ Object
- #convert_to(currency) ⇒ Object
-
#initialize(amount = 0, currency = 'CHF') ⇒ Money
constructor
A new instance of Money.
- #inspect ⇒ Object
Constructor Details
#initialize(amount = 0, currency = 'CHF') ⇒ Money
Returns a new instance of Money.
8 9 10 11 |
# File 'lib/samin/money.rb', line 8 def initialize(amount = 0, currency = 'CHF') @amount = amount @currency = currency end |
Instance Attribute Details
#amount ⇒ Object (readonly)
Returns the value of attribute amount.
6 7 8 |
# File 'lib/samin/money.rb', line 6 def amount @amount end |
#currency ⇒ Object (readonly)
Returns the value of attribute currency.
6 7 8 |
# File 'lib/samin/money.rb', line 6 def currency @currency end |
#currency_ref_conversion_rates=(value) ⇒ Object (writeonly)
Sets the attribute currency_ref_conversion_rates
5 6 7 |
# File 'lib/samin/money.rb', line 5 def currency_ref_conversion_rates=(value) @currency_ref_conversion_rates = value end |
#currency_ref_name=(value) ⇒ Object (writeonly)
Sets the attribute currency_ref_name
5 6 7 |
# File 'lib/samin/money.rb', line 5 def currency_ref_name=(value) @currency_ref_name = value end |
Class Method Details
.conversion_rates(currency_name = 'EUR', rates = {}) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/samin/money.rb', line 80 def self.conversion_rates(currency_name = 'EUR', rates = {}) unless currency_name.nil? || rates.nil? @@currency_ref_name ||= currency_name @@currency_ref_conversion_rates ||= rates return true end false end |
Instance Method Details
#*(other) ⇒ Object
49 50 51 |
# File 'lib/samin/money.rb', line 49 def *(other) Money.new((@amount * other).to_f, @currency) end |
#+(other) ⇒ Object
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/samin/money.rb', line 23 def +(other) sum = -1 if other.currency.eql? @currency sum = @amount + other.amount else other_converted = other.convert_from(other.currency) sum = other_converted.amount + @amount end Money.new(sum, @@currency_ref_name) end |
#-(other) ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/samin/money.rb', line 34 def -(other) sub = -1 if other.currency.eql? @currency sub = @amount - other.amount else other_converted = other.convert_from(other.currency) sub = @amount - other_converted.amount end Money.new(sub, @@currency_ref_name) end |
#/(other) ⇒ Object
45 46 47 |
# File 'lib/samin/money.rb', line 45 def /(other) Money.new((@amount / other).to_f, @currency) end |
#<(other) ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/samin/money.rb', line 62 def <(other) if other.currency.eql? @currency return @amount < other.amount.round(2) else other_converted = other.convert_from(@currency) return @amount < other_converted.amount.round(2) end end |
#==(other) ⇒ Object
53 54 55 56 57 58 59 60 |
# File 'lib/samin/money.rb', line 53 def ==(other) if other.currency.eql? @currency return other.amount.round(2).eql? @amount.round(2) else return other.convert_to(@currency) .amount.round(2).eql? @amount.round(2) end end |
#>(other) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/samin/money.rb', line 71 def >(other) if other.currency.eql? @currency return @amount > other.amount.round(2) else other_converted = other.convert_from(@currency) return @amount > other_converted.amount.round(2) end end |
#convert_from(currency) ⇒ Object
89 90 91 92 |
# File 'lib/samin/money.rb', line 89 def convert_from(currency) rate = @@currency_ref_conversion_rates[currency] Money.new((@amount / rate).to_f.round(2), currency) end |
#convert_to(currency) ⇒ Object
17 18 19 20 21 |
# File 'lib/samin/money.rb', line 17 def convert_to(currency) rate = @@currency_ref_conversion_rates[currency] return nil if rate.nil? Money.new((@amount * rate).to_f.round(2), currency) end |
#inspect ⇒ Object
13 14 15 |
# File 'lib/samin/money.rb', line 13 def inspect "#{@amount} #{@currency}" end |