bin 0.5 → 0.6

Sign up to get free protection for your applications and to get access to all the features.
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=nil)
14
+ def write(key, value, options={})
15
15
  super do
16
16
  expires = Time.now.utc + ((options && options[:expires_in]) || expires_in)
17
- doc = {:_id => key, :value => value, :expires_at => expires}
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' => {:expires_at => Time.now.utc + 1.year},
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
@@ -1,4 +1,4 @@
1
1
  # encoding: UTF-8
2
2
  module Bin
3
- Version = '0.5'
3
+ Version = '0.6'
4
4
  end
@@ -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
 
metadata CHANGED
@@ -4,8 +4,8 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 5
8
- version: "0.5"
7
+ - 6
8
+ version: "0.6"
9
9
  platform: ruby
10
10
  authors:
11
11
  - John Nunemaker