cartman 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,6 +12,7 @@ module Cartman
12
12
  line_item_id = @@redis.incr CART_LINE_ITEM_ID_KEY
13
13
  @@redis.pipelined do
14
14
  @@redis.mapped_hmset("cartman:line_item:#{line_item_id}", options)
15
+ @@redis.hincrby("cartman:line_item:#{line_item_id}", :_version, 1)
15
16
  @@redis.sadd key, line_item_id
16
17
  @@redis.sadd index_key, "#{options[:type]}:#{options[:id]}"
17
18
  @@redis.set index_key_for(options), line_item_id
@@ -91,6 +92,11 @@ module Cartman
91
92
  @@redis.expire item, Cartman::Configuration.cart_expires_in
92
93
  end
93
94
  end
95
+ @@redis.hincrby self.class.versions_key, version_key, 1
96
+ end
97
+
98
+ def version
99
+ @@redis.hget(self.class.versions_key, version_key).to_i
94
100
  end
95
101
 
96
102
  def reassign(new_id)
@@ -107,6 +113,10 @@ module Cartman
107
113
  @uid = new_id
108
114
  end
109
115
 
116
+ def cache_key
117
+ "cart/#{@uid}-#{version}"
118
+ end
119
+
110
120
  private
111
121
 
112
122
  def key(id=@uid)
@@ -117,6 +127,14 @@ module Cartman
117
127
  key(id) + ":index"
118
128
  end
119
129
 
130
+ def version_key(id=@uid)
131
+ id
132
+ end
133
+
134
+ def self.versions_key
135
+ "cartman:cart:versions"
136
+ end
137
+
120
138
  def index_keys(id=@uid)
121
139
  @@redis.keys "#{index_key(id)}:*"
122
140
  end
@@ -28,14 +28,29 @@ module Cartman
28
28
  @id == item._id
29
29
  end
30
30
 
31
+ def touch
32
+ cart.touch
33
+ Cartman::Configuration.redis.hincrby _key, :_version, 1
34
+ end
35
+
31
36
  def _key
32
37
  "cartman:line_item:#{@id}"
33
38
  end
34
39
 
40
+ def _version
41
+ super.to_i
42
+ end
43
+
44
+ def cache_key
45
+ "item/#{@id}-#{_version}"
46
+ end
47
+
35
48
  def method_missing(method, *args, &block)
36
49
  if method.to_s =~ /=\z/
37
50
  Cartman::Configuration.redis.hset _key, method[0..-2], args[0].to_s
38
51
  @data.store(method[0..-2].to_sym, args[0].to_s)
52
+ version = touch
53
+ @data.store(:_version, version)
39
54
  else
40
55
  @data.fetch(method)
41
56
  end
@@ -1,3 +1,3 @@
1
1
  module Cartman
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -187,6 +187,12 @@ describe Cartman do
187
187
  Cartman.config.redis.ttl("cartman:cart:1:index").should eq(Cartman.config.cart_expires_in)
188
188
  Cartman.config.redis.ttl("cartman:cart:1:index:Bottle:17").should eq(Cartman.config.cart_expires_in)
189
189
  end
190
+
191
+ it "should record that the cart was updated" do
192
+ cart.add_item(id: 17, type: "Bottle", name: "Bordeux", unit_cost: 92.12, cost_in_cents: 18424, quantity: 2)
193
+ cart.touch
194
+ cart.version.should eq(2)
195
+ end
190
196
  end
191
197
 
192
198
  describe "#reassign" do
@@ -214,5 +220,11 @@ describe Cartman do
214
220
  cart.send(:key)[-1].should eq("1")
215
221
  end
216
222
  end
223
+
224
+ describe "#cache_key" do
225
+ it "should return /cart/{cart_id}-{version}/" do
226
+ cart.cache_key.should eq("cart/#{cart.instance_variable_get(:@uid)}-#{cart.version}")
227
+ end
228
+ end
217
229
  end
218
230
  end
@@ -24,6 +24,10 @@ describe Cartman do
24
24
  new_item = cart.send(:get_item, item._id)
25
25
  new_item.quantity.should eq("3")
26
26
  end
27
+
28
+ it "should touch the item and cart" do
29
+ expect{item.quantity = 4}.to change{item._version}.by(1)
30
+ end
27
31
  end
28
32
 
29
33
  describe "#cost" do
@@ -43,5 +47,18 @@ describe Cartman do
43
47
  end
44
48
  end
45
49
 
50
+ describe "#touch" do
51
+ it "should record that the record was changed" do
52
+ item.touch
53
+ item._version.should eq(1)
54
+ end
55
+ end
56
+
57
+ describe "#cache_key" do
58
+ it "should return item/{id}-{version}" do
59
+ item.cache_key.should eq("item/#{item._id}-#{item._version}")
60
+ end
61
+ end
62
+
46
63
  end
47
64
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cartman
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-26 00:00:00.000000000 Z
12
+ date: 2012-10-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: redis
@@ -81,7 +81,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
81
  version: '0'
82
82
  segments:
83
83
  - 0
84
- hash: -4477531500718157343
84
+ hash: -2016413100471598065
85
85
  required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  none: false
87
87
  requirements:
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  version: '0'
91
91
  segments:
92
92
  - 0
93
- hash: -4477531500718157343
93
+ hash: -2016413100471598065
94
94
  requirements: []
95
95
  rubyforge_project:
96
96
  rubygems_version: 1.8.23