Adds persistent bank to Money gem
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
Gemfile
Rakefile
Readme.md
money_persistent_bank.gemspec

Readme.md

Money Persistent Bank

"Money" gem provides VariableExchange which is useful for converting money from one currency to another. This gem provides PersistentBank which can save rates to a local storage and automatically load from it in another place.

This can be useful in a Rails application. You fetch rates from external source once a day in a rake task and your running application picks up the new rates on the next request.

Usage in Rails

Add it to your gemfile:

gem "money_persistent_bank", "~> 0.2.0"

Now you can use it in a rake task.

task :fetch_rates => :environment do
  bank = Money.default_bank

  # Here goes your code that fetches the rates from external source.
  # Emulating this:
  sleep 1
  bank.set_rate(:usd, :eur, 0.8)

  bank.export_rates # you have to explicitly export after setting the rates
end

(See also money_bank_sources.)

And somewhere in your application:

Money.default_bank.import_rates
1.to_money(:usd).exchange_to(:eur).to_f # => 0.8

When the rates are updated it will automatically use them.