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.116 lib/knj/exchangerates.rb
knjrbfw-0.0.115 lib/knj/exchangerates.rb
knjrbfw-0.0.114 lib/knj/exchangerates.rb
knjrbfw-0.0.113 lib/knj/exchangerates.rb
knjrbfw-0.0.111 lib/knj/exchangerates.rb
knjrbfw-0.0.110 lib/knj/exchangerates.rb
knjrbfw-0.0.109 lib/knj/exchangerates.rb
knjrbfw-0.0.108 lib/knj/exchangerates.rb
knjrbfw-0.0.107 lib/knj/exchangerates.rb
knjrbfw-0.0.105 lib/knj/exchangerates.rb
knjrbfw-0.0.104 lib/knj/exchangerates.rb
knjrbfw-0.0.103 lib/knj/exchangerates.rb
knjrbfw-0.0.102 lib/knj/exchangerates.rb
knjrbfw-0.0.101 lib/knj/exchangerates.rb
knjrbfw-0.0.100 lib/knj/exchangerates.rb
knjrbfw-0.0.99 lib/knj/exchangerates.rb
knjrbfw-0.0.98 lib/knj/exchangerates.rb
knjrbfw-0.0.97 lib/knj/exchangerates.rb
knjrbfw-0.0.96 lib/knj/exchangerates.rb
knjrbfw-0.0.95 lib/knj/exchangerates.rb