Skip to content

Commit

Permalink
Add ISO4217 namespace for Currency class.
Browse files Browse the repository at this point in the history
From now use "ISO4217::Currency" instead of "Currency".

Note that this is very desirable for Rails projects.
If you have model called "Currency" using this gem witin that Rails
project is complete mess.
Currently we are working on this kind of project.
  • Loading branch information
pr0d1r2 committed Feb 3, 2011
1 parent 40746bf commit a87d295
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 85 deletions.
18 changes: 9 additions & 9 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ Basic Usage

There are two ways to get a currency object. The first is to simply make it.

Currency.new('USD', :name => 'Dollars', :symbol => '$', :exchange_rate => 1)
ISO4217::Currency.new('USD', :name => 'Dollars', :symbol => '$', :exchange_rate => 1)

Or you can lookup a currency by its ISO 4217 code using the *from_code* method.

Currency.from_code('USD')
ISO4217::Currency.from_code('USD')

Once you have a Currency instance you can get basic information about it.

currency = Currency.from_code('USD')
currency = ISO4217::Currency.from_code('USD')
currency.code #=> "USD"
currency.name #=> "Dollars"
currency.symbol #=> "$"
Expand All @@ -36,12 +36,12 @@ Adding Currencies

Currencies keeps an internal list of currencies for use in the ExchangeBank and to be looked up with the *from_code* method. By default this list contains all the currencies in the ISO 4217 standard. A custom currency can be added using the *add* class method.

shiny_button = Currency.new('SBTTN', :name => 'Buttons', :symbol => '☼', :exchange_rate => 1000)
Currency.add(shiny_button)
shiny_button = ISO4217::Currency.new('SBTTN', :name => 'Buttons', :symbol => '☼', :exchange_rate => 1000)
ISO4217::Currency.add(shiny_button)

To do a massive addition of currencies you can load a yaml file using the *load_file* class method.

Currency.load_file('custom_currency.yaml')
ISO4217::Currency.load_file('custom_currency.yaml')

And the yaml file should look like ...

Expand All @@ -54,19 +54,19 @@ Defaults

You can set the base currency by using the *base_currency* class method. This defaults to 'USD'.

Currency.base_currency => 'GBP'
ISO4217::Currency.base_currency => 'GBP'

The exchange rate is either set manually or if nil looked up on Yahoo Finance and cached. If you want to disable looking up the currency set the *import_exchange_rates* class method to false.

Currency.import_exchange_rates = false
ISO4217::Currency.import_exchange_rates = false


Money Gem
---------

To use with the money gem you just set the default bank to the currencies bank.

Money.default_bank = Currency::ExchangeBank.new
Money.default_bank = ISO4217::Currency::ExchangeBank.new

The Currencies ExchangeBank works the same as the one in the money gem except that if an exchange rate isn't set by default it uses what is set in the currencies gem.

Expand Down
15 changes: 9 additions & 6 deletions lib/currencies/currency.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
class Currency
module ISO4217
end

class ISO4217::Currency
class << self
attr_accessor :currencies
attr_accessor :base_currency
Expand All @@ -12,7 +15,7 @@ def initialize(iso_code,opts={})
@code = iso_code.to_s.upcase
@name = opts['name']
@symbol = opts['symbol']
@exchange_currency = opts['exchange_currency'] || Currency.base_currency
@exchange_currency = opts['exchange_currency'] || self.class.base_currency
@exchange_rate = opts['exchange_rate'].to_f if opts['exchange_rate']
end

Expand All @@ -21,14 +24,14 @@ def [](value)
end

def exchange_rate
@exchange_rate = nil unless @exchange_currency == Currency.base_currency
@exchange_rate = nil unless @exchange_currency == self.class.base_currency
@exchange_rate ||= load_exchange_rate
end

def load_exchange_rate
@exchange_currency = Currency.base_currency
@exchange_currency = self.class.base_currency
return 1.0 if @code == @exchange_currency
if Currency.import_exchange_rates
if self.class.import_exchange_rates
http = Net::HTTP.new('download.finance.yahoo.com', 80)
response = http.get("/d/quotes.csv?e=.csv&f=sl1d1t1&s=#{@code}#{@exchange_currency}=X")
rate = response.body.split(',')[1]
Expand All @@ -49,7 +52,7 @@ def self.from_code(code)
end

def self.major_currencies_selection(currencies)
currencies.select { |code, currency| Currency.major_codes.include?(code) }.first
currencies.select { |code, currency| self.major_codes.include?(code) }.first
end

def self.best_from_currencies(currencies)
Expand Down
8 changes: 4 additions & 4 deletions lib/currencies/exchange_bank.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'thread'

class Currency
class ISO4217::Currency
class ExchangeBank
def self.instance
@@singleton
Expand Down Expand Up @@ -32,8 +32,8 @@ def exchange(cents, from_currency, to_currency)
if rate
(cents * rate).floor
else
from_currency = Currency.from_code(from_currency)
to_currency = Currency.from_code(to_currency)
from_currency = ISO4217::Currency.from_code(from_currency)
to_currency = ISO4217::Currency.from_code(to_currency)

if from_currency && to_currency && from_currency.exchange_rate && to_currency.exchange_rate && (from_currency.exchange_currency == to_currency.exchange_currency)
((cents * from_currency.exchange_rate) / to_currency.exchange_rate).floor
Expand All @@ -45,4 +45,4 @@ def exchange(cents, from_currency, to_currency)

@@singleton = ExchangeBank.new
end
end
end
Loading

0 comments on commit a87d295

Please sign in to comment.