Permalink
Browse files

Ensured that all methods work the same with symbol as they would with…

… string.
  • Loading branch information...
1 parent 05ba475 commit fd82f4208c81fdb1be7db14cd601dfd1a2eeadca @jnunemaker committed Jun 15, 2010
Showing with 38 additions and 3 deletions.
  1. +5 −3 lib/bin/store.rb
  2. +33 −0 spec/bin/store_spec.rb
View
@@ -12,6 +12,7 @@ def expires_in
end
def write(key, value, options={})
+ key = key.to_s
super do
expires = Time.now.utc + ((options && options[:expires_in]) || expires_in)
raw = !!options[:raw]
@@ -23,15 +24,15 @@ def write(key, value, options={})
def read(key, options=nil)
super do
- if doc = collection.find_one(:_id => key, :expires_at => {'$gt' => Time.now.utc})
+ if doc = collection.find_one(:_id => key.to_s, :expires_at => {'$gt' => Time.now.utc})
doc['raw'] ? doc['value'] : Marshal.load(doc['value'].to_s)
end
end
end
def delete(key, options=nil)
super do
- collection.remove(:_id => key)
+ collection.remove(:_id => key.to_s)
end
end
@@ -69,11 +70,12 @@ def stats
private
def counter_key_upsert(key, amount)
+ key = key.to_s
collection.update(
{:_id => key}, {
'$inc' => {:value => amount},
'$set' => {
- :expires_at => Time.now.utc + 1.year,
+ :expires_at => Time.now.utc + 1.year,
:raw => true
},
}, :upsert => true)
View
@@ -56,6 +56,13 @@
store.write('foo', 'bar', :expires_in => 5.seconds)
document['expires_at'].to_i.should == (Time.now.utc + 5.seconds).to_i
end
+
+ it "always sets key as string" do
+ store.write(:baz, 'wick')
+ doc = collection.find_one(:_id => 'baz')
+ doc.should_not be_nil
+ doc['_id'].should be_instance_of(String)
+ end
end
describe "#read" do
@@ -86,6 +93,10 @@
store.write('foo', 'bar', :expires_in => 20.seconds)
store.read('foo').should == 'bar'
end
+
+ it "works with symbol" do
+ store.read(:foo).should == 'bar'
+ end
end
describe "#delete" do
@@ -98,6 +109,12 @@
store.delete('foo')
store.read('foo').should be_nil
end
+
+ it "works with symbol" do
+ store.read(:foo).should_not be_nil
+ store.delete(:foo)
+ store.read(:foo).should be_nil
+ end
end
describe "#delete_matched" do
@@ -133,6 +150,11 @@
it "returns false if key not found" do
store.exist?('not:found:key').should be_false
end
+
+ it "works with symbol" do
+ store.exist?(:foo).should be_true
+ store.exist?(:notfoundkey).should be_false
+ end
end
describe "#clear" do
@@ -155,6 +177,11 @@
store.increment('views', 2)
store.read('views').should == 3
end
+
+ it "works with symbol" do
+ store.increment(:views, 2)
+ store.read(:views).should == 2
+ end
end
describe "#decrement" do
@@ -165,6 +192,12 @@
store.decrement('views', 2)
store.read('views').should == 1
end
+
+ it "works with symbol" do
+ store.increment(:views, 2)
+ store.decrement(:views, 1)
+ store.read(:views).should == 1
+ end
end
describe "#stats" do

0 comments on commit fd82f42

Please sign in to comment.