Class: Knj::Exchangerates
Instance Attribute Summary collapse
-
#rates ⇒ Object
readonly
Returns the value of attribute rates.
Instance Method Summary collapse
- #add_rate(data) ⇒ Object
- #base=(data) ⇒ Object
-
#initialize(args = {}) ⇒ Exchangerates
constructor
A new instance of Exchangerates.
- #value(locale, floatval) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ Exchangerates
Returns a new instance of Exchangerates.
4 5 6 |
# File 'lib/knj/exchangerates.rb', line 4 def initialize(args = {}) @rates = {} end |
Instance Attribute Details
#rates ⇒ Object (readonly)
Returns the value of attribute rates.
2 3 4 |
# File 'lib/knj/exchangerates.rb', line 2 def rates @rates end |
Instance Method Details
#add_rate(data) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/knj/exchangerates.rb', line 13 def add_rate(data) if !data[:locale] or data[:locale].to_s.length <= 0 raise "Invalid locale given." end @rates[data[:locale].to_s] = data end |
#base=(data) ⇒ Object
8 9 10 11 |
# File 'lib/knj/exchangerates.rb', line 8 def base=(data) @base = data[:locale].to_s self.add_rate(data) end |
#value(locale, floatval) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/knj/exchangerates.rb', line 21 def value(locale, floatval) floatval = floatval.to_f locale = locale.to_s raise "No such locale: '#{locale}' in '#{@rates.keys.join(",")}'." if !@rates.key?(locale) base_rate = @rates[@base][:rate].to_f cur_rate = @rates[locale][:rate].to_f if base_rate == cur_rate return floatval elsif cur_rate < base_rate diff = 1 + (base_rate - cur_rate) return floatval * diff else return floatval / cur_rate end end |