Skip to content

Commit 7b3a66b

Browse files
authored
Make RateStore marshalable (add #marshal_dump) (#31)
1 parent 273f4fe commit 7b3a66b

3 files changed

Lines changed: 27 additions & 0 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+
Fix:
12+
- Make `RateStore` marshalable so a bank using it can be marshaled.
13+
1114
## v1.4.1
1215

1316
Fix:

lib/active_currency/rate_store.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,15 @@
33
module ActiveCurrency
44
class RateStore < DatabaseStore
55
include CacheableStore
6+
7+
# Money::Bank::VariableExchange#marshal_load rebuilds the store with
8+
# `store_info.shift.new(*store_info)`, so a store's #marshal_dump must
9+
# return `[StoreClass, *constructor_args]`. RateStore takes no arguments,
10+
# so returning `[self.class]` lets a bank backed by this store be
11+
# marshaled -- for example when the configured default bank is cached
12+
# under a Marshal-based cache/serializer.
13+
def marshal_dump
14+
[self.class]
15+
end
616
end
717
end

spec/lib/active_currency/rate_store_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,18 @@
104104
end
105105
end
106106
end
107+
108+
describe "#marshal_dump" do
109+
it "dumps the class so the store can be rebuilt on load" do
110+
expect(store.marshal_dump).to eq([described_class])
111+
end
112+
113+
it "lets a bank backed by the store round-trip through Marshal" do
114+
bank = Money::Bank::VariableExchange.new(store)
115+
116+
loaded = Marshal.load(Marshal.dump(bank))
117+
118+
expect(loaded.store).to be_a(described_class)
119+
end
120+
end
107121
end

0 commit comments

Comments
 (0)