Permalink
Browse files

Added default read_multiple.

Can be overridden per store to save network round trips.
  • Loading branch information...
1 parent d34caeb commit f265568422216259eab6d3ea45f1b69baf2e46ba @jnunemaker committed Nov 7, 2012
Showing with 31 additions and 0 deletions.
  1. +6 −0 lib/adapter/defaults.rb
  2. +25 −0 lib/adapter/spec/an_adapter.rb
View
@@ -7,6 +7,12 @@ def fetch(key, value=nil, &block)
end
end
+ def read_multiple(*keys)
+ result = {}
+ keys.each { |key| result[key_for(key)] = read(key) }
+ result
+ end
+
def key?(key)
!read(key).nil?
end
@@ -42,6 +42,31 @@
end
end
+ describe "#read_multiple" do
+ before do
+ adapter.write(key, attributes)
+ adapter.write(key2, attributes2)
+ end
+
+ it "returns Hash of keys and values" do
+ adapter.read_multiple(key, key2).should eq({
+ key => attributes,
+ key2 => attributes2,
+ })
+ end
+
+ context "with mix of keys that are and are not available" do
+ it "returns Hash of keys and values where unavailable keys are nil" do
+ adapter.read_multiple(key, key2, 'foo', 'bar').should eq({
+ key => attributes,
+ key2 => attributes2,
+ 'foo' => nil,
+ 'bar' => nil,
+ })
+ end
+ end
+ end
+
describe "#key?" do
it "returns true if key available" do
adapter.write(key, attributes)

0 comments on commit f265568

Please sign in to comment.