Class: Geld::Config
- Inherits:
-
Object
- Object
- Geld::Config
- Defined in:
- lib/geld/config.rb
Instance Method Summary collapse
-
#add_currency(currency, rates) ⇒ Object
Set a new currency configuration.
-
#get_rate(from, to) ⇒ Object
returns the specific rate.
Instance Method Details
#add_currency(currency, rates) ⇒ Object
Set a new currency configuration. If the rates table is set more than once, the new one overrides the older
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/geld/config.rb', line 6 def add_currency(currency, rates) validate_currency(currency) validate_rates(rates) @currencies ||= {} # Converts rates to BigDecimal bd_rates = rates.inject({}){|hash, (k,v)| hash[k] = BigDecimal.new(v.to_s); hash } @currencies[currency] = bd_rates bd_rates.each do |a_currency, a_rate| # reverse operation will be the first element current_rate = BigDecimal.new("1").div(a_rate, 2) # 1/rate 2 digits @currencies[a_currency] = { currency => current_rate } # now the others (bd_rates.keys - [a_currency]).each do |other_currency| @currencies[a_currency][other_currency] = @currencies[currency][other_currency].mult(current_rate, 2) end end # returns the new calculations @currencies end |
#get_rate(from, to) ⇒ Object
returns the specific rate
33 34 35 36 37 |
# File 'lib/geld/config.rb', line 33 def get_rate(from, to) @currencies && @currencies[from] && @currencies[from][to] end |