currency.rb

Path: lib/currency.rb
Last Update: Thu May 03 11:05:27 -0500 2007
Comparable Money Base UndefinedExchange MissingCurrency RateSourceError UnavailableRates InvalidRate UnknownCurrency Generic SubclassResponsibility InvalidMoneyValue InvalidMoneyString InvalidReentrancy Unimplemented UnknownRate IncompatibleCurrency InvalidCurrencyCode ::Exception Parser Config Factory Currency Formatter Rate TimeQuantitizer ClassMethods Macro Exchange ClassMethods ActiveRecord Exception Currency dot/f_6.png

Copyright (C) 2006-2007 Kurt Stephens <ruby-currency(at)umleta.com>

See LICENSE.txt for details.

Currency

The Currency package provides an object-oriented model of:

  • currencies
  • exchanges
  • exchange rates
  • exchange rate sources
  • monetary values

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}"

ActiveRecord Suppport

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.

Recent Enhancements

Storage and retrival of historical exchange rates

Automatic derivation of rates from base rates.

Rate caching

Rate Providers

Customizable formatting and parsing

Future Enhancements

  • Support for inflationary rates within a currency, e.g. $10 USD in the year 1955 converted to 2006 USD.

SVN Repo

   svn checkout svn://rubyforge.org/var/svn/currency/currency/trunk

Examples

See the examples/ and test/ directorys

Author

Kurt Stephens kurtstephens.com

Support

rubyforge.org/forum/forum.php?forum_id=7643

Copyright

Copyright (C) 2006-2007 Kurt Stephens <ruby-currency(at)umleta.com>

See LICENSE.txt for details.

Required files

currency/currency_version   currency/config   currency/exception   currency/money   currency/currency   currency/currency/factory   currency/money   currency/formatter   currency/parser   currency/exchange   currency/exchange/rate   currency/exchange/rate/deriver   currency/exchange/rate/source   currency/exchange/rate/source/test   currency/exchange/time_quantitizer   currency/core_extensions  

[Validate]