Sha256: 3df079e9acc4ddfddc27f8ef4e30c6406e03035bf01ba88d070a451302002220

Contents?: true

Size: 846 Bytes

Versions: 101

Compression:

Stored size: 846 Bytes

Contents

class Knj::Exchangerates
  attr_reader :rates
  
  def initialize(args = {})
    @rates = {}
  end
  
  def base=(data)
    @base = data[:locale].to_s
    self.add_rate(data)
  end
  
  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
  
  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
end

Version data entries

101 entries across 101 versions & 1 rubygems

Version Path
knjrbfw-0.0.94 lib/knj/exchangerates.rb
knjrbfw-0.0.93 lib/knj/exchangerates.rb
knjrbfw-0.0.92 lib/knj/exchangerates.rb
knjrbfw-0.0.91 lib/knj/exchangerates.rb
knjrbfw-0.0.90 lib/knj/exchangerates.rb
knjrbfw-0.0.89 lib/knj/exchangerates.rb
knjrbfw-0.0.88 lib/knj/exchangerates.rb
knjrbfw-0.0.87 lib/knj/exchangerates.rb
knjrbfw-0.0.86 lib/knj/exchangerates.rb
knjrbfw-0.0.85 lib/knj/exchangerates.rb
knjrbfw-0.0.84 lib/knj/exchangerates.rb
knjrbfw-0.0.83 lib/knj/exchangerates.rb
knjrbfw-0.0.82 lib/knj/exchangerates.rb
knjrbfw-0.0.81 lib/knj/exchangerates.rb
knjrbfw-0.0.80 lib/knj/exchangerates.rb
knjrbfw-0.0.79 lib/knj/exchangerates.rb
knjrbfw-0.0.78 lib/knj/exchangerates.rb
knjrbfw-0.0.77 lib/knj/exchangerates.rb
knjrbfw-0.0.75 lib/knj/exchangerates.rb
knjrbfw-0.0.74 lib/knj/exchangerates.rb