bin 0.5 → 0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/bin/store.rb +9 -4
- data/lib/bin/version.rb +1 -1
- data/spec/bin/store_spec.rb +18 -1
- metadata +2 -2
data/lib/bin/store.rb
CHANGED
@@ -11,10 +11,12 @@ module Bin
|
|
11
11
|
@expires_in ||= options[:expires_in] || 1.year
|
12
12
|
end
|
13
13
|
|
14
|
-
def write(key, value, options=
|
14
|
+
def write(key, value, options={})
|
15
15
|
super do
|
16
16
|
expires = Time.now.utc + ((options && options[:expires_in]) || expires_in)
|
17
|
-
|
17
|
+
raw = !!options[:raw]
|
18
|
+
value = raw ? value : Marshal.dump(value)
|
19
|
+
doc = {:_id => key, :value => value, :expires_at => expires, :raw => raw}
|
18
20
|
collection.save(doc)
|
19
21
|
end
|
20
22
|
end
|
@@ -22,7 +24,7 @@ module Bin
|
|
22
24
|
def read(key, options=nil)
|
23
25
|
super do
|
24
26
|
if doc = collection.find_one(:_id => key, :expires_at => {'$gt' => Time.now.utc})
|
25
|
-
doc['value']
|
27
|
+
doc['raw'] ? doc['value'] : Marshal.load(doc['value'])
|
26
28
|
end
|
27
29
|
end
|
28
30
|
end
|
@@ -70,7 +72,10 @@ module Bin
|
|
70
72
|
collection.update(
|
71
73
|
{:_id => key}, {
|
72
74
|
'$inc' => {:value => amount},
|
73
|
-
'$set' => {
|
75
|
+
'$set' => {
|
76
|
+
:expires_at => Time.now.utc + 1.year,
|
77
|
+
:raw => true
|
78
|
+
},
|
74
79
|
}, :upsert => true)
|
75
80
|
end
|
76
81
|
end
|
data/lib/bin/version.rb
CHANGED
data/spec/bin/store_spec.rb
CHANGED
@@ -37,7 +37,18 @@ describe Bin::Store do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
it "sets value key to value" do
|
40
|
+
store.read('foo').should == 'bar'
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should marshal value by default" do
|
44
|
+
document['value'].should == Marshal.dump('bar')
|
45
|
+
document['raw'].should be_false
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should be able to store in raw format" do
|
49
|
+
store.write('foo', 'bar', :raw => true)
|
40
50
|
document['value'].should == 'bar'
|
51
|
+
document['raw'].should be_true
|
41
52
|
end
|
42
53
|
|
43
54
|
it "sets expires in to default if not provided" do
|
@@ -54,12 +65,18 @@ describe Bin::Store do
|
|
54
65
|
before(:each) do
|
55
66
|
store.write('foo', 'bar')
|
56
67
|
end
|
68
|
+
let(:document) { collection.find_one(:_id => 'foo') }
|
57
69
|
|
58
70
|
it "returns nil for key not found" do
|
59
71
|
store.read('non:existent:key').should be_nil
|
60
72
|
end
|
61
73
|
|
62
|
-
it "returns value key value for key found" do
|
74
|
+
it "returns unmarshalled value key value for key found" do
|
75
|
+
store.read('foo').should == 'bar'
|
76
|
+
end
|
77
|
+
|
78
|
+
it "returns raw value if document raw key is true" do
|
79
|
+
store.write('foo', 'bar', :raw => true)
|
63
80
|
store.read('foo').should == 'bar'
|
64
81
|
end
|
65
82
|
|