Skip to content

Commit 3e923ed

Browse files
authored
Add configuration to disable cache (#34)
1 parent 7b3a66b commit 3e923ed

4 files changed

Lines changed: 31 additions & 14 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ This project adheres to [Semantic Versioning].
88
Changes:
99
- Drop support for Rails < 7.0.
1010

11+
Feature:
12+
- Add `cache_enabled` to config to be able to enable/disable cache.
13+
1114
Fix:
1215
- Make `RateStore` marshalable so a bank using it can be marshaled.
1316

‎README.md‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ ActiveCurrency::AddRates.call(currencies: %w[EUR USD])
3333
You can then exchange money by using the Money gem helpers:
3434

3535
```rb
36-
10.to_money('EUR').exchange_to('USD').cents
36+
10.to_money("EUR").exchange_to("USD").cents
3737
```
3838

3939
If you need to look up the previous currency rates:
4040

4141
```rb
42-
ActiveCurrency::Rate.value_for('EUR', 'USD', 1.month.ago)
42+
ActiveCurrency::Rate.value_for("EUR", "USD", 1.month.ago)
4343
# => 1.151
44-
ActiveCurrency::Rate.where(from: 'EUR', to: 'USD').pluck(:value)
44+
ActiveCurrency::Rate.where(from: "EUR", to: "USD").pluck(:value)
4545
# => [1.162, 1.162, 1.161, 1.161, 1.163, …]
4646
```
4747

@@ -88,7 +88,7 @@ add the following to your application’s initializers:
8888
```rb
8989
ActiveCurrency.configure do |config|
9090
config.remote_bank = :open_exchange_rates
91-
config.open_exchange_rates_app_id = '…'
91+
config.open_exchange_rates_app_id = "…"
9292
end
9393
```
9494

@@ -101,16 +101,21 @@ You can provide any Money-compatible bank when calling
101101
ActiveCurrency::AddRates.call(…, bank: …)
102102
```
103103

104-
## Apply a multiplier
105-
106-
If you want to increase or decrease the currency rates by a given multiplier,
107-
you can do so by setting the `multiplier` option:
104+
## Configuration
108105

109106
```rb
110107
ActiveCurrency.configure do |config|
111-
config.multiplier = {
112-
["EUR", "USD"] => 1.01,
113-
}
108+
# Use the money-open-exchange-rates bank instead.
109+
# config.remote_bank = :open_exchange_rates
110+
# config.open_exchange_rates_app_id = "…"
111+
112+
# Disable `Rails.cache`.
113+
# config.cache_enabled = false
114+
115+
# Increase or decrease the currency rates by a given multiplier.
116+
# config.multiplier = {
117+
# ["EUR", "USD"] => 1.01,
118+
# }
114119
end
115120
```
116121

@@ -124,8 +129,8 @@ For that, you can use a fake rate store in your `rails_helper.rb`:
124129
```rb
125130
MoneyRails.configure do |config|
126131
rate_store = Money::RatesStore::Memory.new.tap do |store|
127-
store.add_rate('USD', 'EUR', 1.5)
128-
store.add_rate('EUR', 'USD', 0.67)
132+
store.add_rate("USD", "EUR", 1.5)
133+
store.add_rate("EUR", "USD", 0.67)
129134
end
130135
config.default_bank = Money::Bank::VariableExchange.new(rate_store)
131136
end

‎lib/active_currency/cacheable_store.rb‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module CacheableStore
77
include AfterCommitEverywhere
88

99
def get_rate(from, to, date = nil)
10+
return super unless ActiveCurrency.configuration.cache_enabled?
1011
return super if date
1112

1213
Rails.cache.fetch(cache_key(from, to), expires_in: 1.hour) do
@@ -17,6 +18,8 @@ def get_rate(from, to, date = nil)
1718
def add_rate(from, to, rate, date = nil)
1819
super
1920

21+
return unless ActiveCurrency.configuration.cache_enabled?
22+
2023
after_commit do
2124
Rails.cache.delete(cache_key(from, to))
2225
end

‎lib/active_currency/configuration.rb‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ def initialize
66
@remote_bank = :eu_central_bank
77
@open_exchange_rates_app_id = nil
88
@multiplier = {}
9+
@cache_enabled = true
910
end
1011

1112
attr_accessor :remote_bank,
1213
:open_exchange_rates_app_id,
13-
:multiplier
14+
:multiplier,
15+
:cache_enabled
16+
17+
def cache_enabled?
18+
!!@cache_enabled
19+
end
1420
end
1521
end

0 commit comments

Comments
 (0)