| Path: | lib/currency.rb |
| Last Update: | Thu May 03 11:05:27 -0500 2007 |
Copyright (C) 2006-2007 Kurt Stephens <ruby-currency(at)umleta.com>
See LICENSE.txt for details.
The Currency package provides an object-oriented model of:
The core classes are:
The example below uses Currency::Exchange::Xe to automatically get exchange rates from xe.com/ :
require 'currency'
require 'currency/exchange/rate/deriver'
require 'currency/exchange/rate/source/xe'
require 'currency/exchange/rate/source/timed_cache'
# Rate source initialization
provider = Currency::Exchange::Rate::Source::Xe.new
deriver = Currency::Exchange::Rate::Deriver.new(:source => provider)
cache = Currency::Exchange::Rate::Source::TimedCache.new(:source => deriver)
Currency::Exchange::Rate::Source.default = cache
usd = Currency::Money('6.78', :USD)
puts "usd = #{usd.format}"
cad = usd.convert(:CAD)
puts "cad = #{cad.format}"
This package also contains ActiveRecord support for money values:
require 'currency'
require 'currency/active_record'
class Entry < ActiveRecord::Base
money :amount
end
In the example above, the entries.amount database column is an INTEGER that represents US cents. The currency code of the money value can be stored in an additional database column or a default currency can be used.
svn checkout svn://rubyforge.org/var/svn/currency/currency/trunk
See the examples/ and test/ directorys
Kurt Stephens kurtstephens.com
rubyforge.org/forum/forum.php?forum_id=7643
Copyright (C) 2006-2007 Kurt Stephens <ruby-currency(at)umleta.com>
See LICENSE.txt for details.