mongo_watchable 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,12 @@
1
1
  module MongoWatchable
2
2
  class Proxy
3
3
  attr_reader :array_key
4
+ attr_reader :array_count_key
4
5
  attr_reader :model
5
6
  attr_reader :target_class
6
7
 
7
- def initialize(model, array_key, target_class)
8
- @model, @array_key, @target_class = model, array_key, target_class
8
+ def initialize(model, array_key, array_count_key, target_class)
9
+ @model, @array_key, @array_count_key, @target_class = model, array_key, array_count_key, target_class
9
10
  end
10
11
 
11
12
  def to_a
@@ -47,11 +48,12 @@ module MongoWatchable
47
48
 
48
49
  def << (entry)
49
50
  array << entry.id
51
+ increment_array_count
50
52
  @fetch ? @fetch << entry : fetch_all
51
53
  end
52
54
 
53
55
  def delete(entry)
54
- array.delete entry.id
56
+ decrement_array_count if array.delete(entry.id)
55
57
  @fetch ? @fetch.delete(entry) : fetch_all
56
58
  end
57
59
 
@@ -67,5 +69,13 @@ module MongoWatchable
67
69
  def array
68
70
  model.send(array_key)
69
71
  end
72
+
73
+ def increment_array_count
74
+ model.send("#{array_count_key}=", model.send(array_count_key) + 1)
75
+ end
76
+
77
+ def decrement_array_count
78
+ model.send("#{array_count_key}=", model.send(array_count_key) - 1)
79
+ end
70
80
  end
71
81
  end
@@ -4,9 +4,10 @@ module MongoWatchable
4
4
  watchable.class_eval do
5
5
  MongoWatchable.watchers.each do |watcher|
6
6
  key :"#{watcher.name.underscore}_watcher_ids", Array
7
+ key :"#{watcher.name.underscore}_watchers_count", Integer, :default => 0, :index => true
7
8
 
8
9
  define_method :"#{watcher.name.underscore}_watchers" do
9
- MongoWatchable::Proxy.new(self, :"#{watcher.name.underscore}_watcher_ids", watcher)
10
+ MongoWatchable::Proxy.new(self, :"#{watcher.name.underscore}_watcher_ids", :"#{watcher.name.underscore}_watchers_count", watcher)
10
11
  end
11
12
  end
12
13
  end
@@ -14,9 +15,10 @@ module MongoWatchable
14
15
  MongoWatchable.watchers.each do |watcher|
15
16
  watcher.class_eval do
16
17
  key :"#{watchable.name.underscore}_watching_ids", Array
18
+ key :"#{watchable.name.underscore}_watchings_count", Integer, :default => 0, :index => true
17
19
 
18
20
  define_method :"#{watchable.name.underscore}_watchings" do
19
- MongoWatchable::Proxy.new(self, :"#{watchable.name.underscore}_watching_ids", watchable)
21
+ MongoWatchable::Proxy.new(self, :"#{watchable.name.underscore}_watching_ids", :"#{watchable.name.underscore}_watchings_count", watchable)
20
22
  end
21
23
  end
22
24
  end
@@ -4,9 +4,10 @@ module MongoWatchable
4
4
  watcher.class_eval do
5
5
  MongoWatchable.watchables.each do |watchable|
6
6
  key :"#{watchable.name.underscore}_watching_ids", Array
7
+ key :"#{watchable.name.underscore}_watchings_count", Integer, :default => 0, :index => true
7
8
 
8
9
  define_method :"#{watchable.name.underscore}_watchings" do
9
- MongoWatchable::Proxy.new(self, :"#{watchable.name.underscore}_watching_ids", watchable)
10
+ MongoWatchable::Proxy.new(self, :"#{watchable.name.underscore}_watching_ids", :"#{watchable.name.underscore}_watchings_count", watchable)
10
11
  end
11
12
  end
12
13
  end
@@ -14,9 +15,10 @@ module MongoWatchable
14
15
  MongoWatchable.watchables.each do |watchable|
15
16
  watchable.class_eval do
16
17
  key :"#{watcher.name.underscore}_watcher_ids", Array
18
+ key :"#{watcher.name.underscore}_watchers_count", Integer, :default => 0, :index => true
17
19
 
18
20
  define_method :"#{watcher.name.underscore}_watchers" do
19
- MongoWatchable::Proxy.new(self, :"#{watcher.name.underscore}_watcher_ids", watcher)
21
+ MongoWatchable::Proxy.new(self, :"#{watcher.name.underscore}_watcher_ids", :"#{watcher.name.underscore}_watchers_count", watcher)
20
22
  end
21
23
  end
22
24
  end
@@ -75,6 +77,14 @@ module MongoWatchable
75
77
  end
76
78
  send("#{klass.name.underscore}_watchings")
77
79
  end
80
+
81
+ def watchings_count_proxy_for(watchable)
82
+ klass = watchable.class
83
+ while klass.superclass && klass.superclass.include?(MongoWatchable::Watchable)
84
+ klass = klass.superclass
85
+ end
86
+ send("#{klass.name.underscore}_watchings")
87
+ end
78
88
 
79
89
  private
80
90
  def root_watching_class(instance)
@@ -38,11 +38,15 @@ class MongoWatchableTest < ActiveSupport::TestCase
38
38
  @widget.reload
39
39
  end
40
40
 
41
- should "should only have 1 widget in widget_watchings array" do
41
+ should "only have 1 widget in widget_watchings array" do
42
42
  assert_equal 1, @user.widget_watchings.size
43
43
  end
44
44
 
45
- should "should have widget in widget watchings array" do
45
+ should "increment widget_watchings_count" do
46
+ assert_equal 1, @user.widget_watchings_count
47
+ end
48
+
49
+ should "have widget in widget watchings array" do
46
50
  assert_equal @widget, @user.widget_watchings.first
47
51
  end
48
52
 
@@ -50,6 +54,10 @@ class MongoWatchableTest < ActiveSupport::TestCase
50
54
  assert_equal 1, @widget.user_watchers.size
51
55
  end
52
56
 
57
+ should "increment user_watchers_count" do
58
+ assert_equal 1, @widget.user_watchers_count
59
+ end
60
+
53
61
  should "be the watcher in widgets user_watchers array" do
54
62
  assert_equal @user, @widget.user_watchers.first
55
63
  end
@@ -84,21 +92,29 @@ class MongoWatchableTest < ActiveSupport::TestCase
84
92
  assert @return
85
93
  end
86
94
 
87
- should "should only have 2 widgets in widget_watchings array" do
95
+ should "only have 2 widgets in widget_watchings array" do
88
96
  assert_equal 2, @user.widget_watchings.size
89
97
  end
98
+
99
+ should "increment widget_watchings_count" do
100
+ assert_equal 2, @user.widget_watchings_count
101
+ end
90
102
 
91
- should "should have widget in widget watchings array" do
103
+ should "have widget in widget watchings array" do
92
104
  assert_equal @widget, @user.widget_watchings.first
93
105
  end
94
106
 
95
- should "should have widget2 in widget watchings array" do
107
+ should "have widget2 in widget watchings array" do
96
108
  assert_equal @widget2, @user.widget_watchings.last
97
109
  end
98
110
 
99
111
  should "have only one watcher in widget's user_watchers array" do
100
112
  assert_equal 1, @widget.user_watchers.size
101
113
  end
114
+
115
+ should "not increment user_watchers_count" do
116
+ assert_equal 1, @widget.user_watchers_count
117
+ end
102
118
 
103
119
  should "be the watcher in widget's user_watchers array" do
104
120
  assert_equal @user, @widget.user_watchers.first
@@ -127,6 +143,14 @@ class MongoWatchableTest < ActiveSupport::TestCase
127
143
  should "have no user_watchers in widget" do
128
144
  assert @widget.user_watchers.empty?
129
145
  end
146
+
147
+ should "have 0 widget_watchings_count" do
148
+ assert_equal 0, @user.widget_watchings_count
149
+ end
150
+
151
+ should "have 0 user_watchers_count" do
152
+ assert_equal 0, @widget.user_watchers_count
153
+ end
130
154
 
131
155
  should "return with true" do
132
156
  assert @return
@@ -144,11 +168,15 @@ class MongoWatchableTest < ActiveSupport::TestCase
144
168
  @widget.reload
145
169
  end
146
170
 
147
- should "should only have 1 widget in widget_watchings array" do
171
+ should "only have 1 widget in widget_watchings array" do
148
172
  assert_equal 1, @user.widget_watchings.size
149
173
  end
150
174
 
151
- should "should have widget in widget watchings array" do
175
+ should "increment widget_watchings_count" do
176
+ assert_equal 1, @user.widget_watchings_count
177
+ end
178
+
179
+ should "have widget in widget watchings array" do
152
180
  assert_equal @widget, @user.widget_watchings.first
153
181
  end
154
182
 
@@ -156,6 +184,10 @@ class MongoWatchableTest < ActiveSupport::TestCase
156
184
  assert_equal 1, @widget.user_watchers.size
157
185
  end
158
186
 
187
+ should "increment user_watchers_count" do
188
+ assert_equal 1, @widget.user_watchers_count
189
+ end
190
+
159
191
  should "be the watcher in widgets user_watchers array" do
160
192
  assert_equal @user, @widget.user_watchers.first
161
193
  end
@@ -172,21 +204,29 @@ class MongoWatchableTest < ActiveSupport::TestCase
172
204
  @widget2.reload
173
205
  end
174
206
 
175
- should "should only have 2 widgets in widget_watchings array" do
207
+ should "have 2 widgets in widget_watchings array" do
176
208
  assert_equal 2, @user.widget_watchings.size
177
209
  end
210
+
211
+ should "increment widget_watchings_count" do
212
+ assert_equal 2, @user.widget_watchings_count
213
+ end
178
214
 
179
- should "should have widget in widget watchings array" do
215
+ should "have widget in widget watchings array" do
180
216
  assert_equal @widget, @user.widget_watchings.first
181
217
  end
182
218
 
183
- should "should have widget2 in widget watchings array" do
219
+ should "have widget2 in widget watchings array" do
184
220
  assert_equal @widget2, @user.widget_watchings.last
185
221
  end
186
222
 
187
- should "have only one watcher in widget's user_watchers array" do
223
+ should "have one watcher in widget's user_watchers array" do
188
224
  assert_equal 1, @widget.user_watchers.size
189
225
  end
226
+
227
+ should "not increment user_watchers_count" do
228
+ assert_equal 1, @widget.user_watchers_count
229
+ end
190
230
 
191
231
  should "be the watcher in widget's user_watchers array" do
192
232
  assert_equal @user, @widget.user_watchers.first
@@ -223,6 +263,14 @@ class MongoWatchableTest < ActiveSupport::TestCase
223
263
  should "have no user_watchers in widget" do
224
264
  assert @widget.user_watchers.empty?
225
265
  end
266
+
267
+ should "have 0 widget_watchings_count" do
268
+ assert_equal 0, @user.widget_watchings_count
269
+ end
270
+
271
+ should "have 0 user_watchers_count" do
272
+ assert_equal 0, @widget.user_watchers_count
273
+ end
226
274
  end
227
275
  end
228
276
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
7
  - 2
9
- version: 0.1.2
8
+ - 0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jonathan Bell
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-25 00:00:00 -05:00
17
+ date: 2010-06-14 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency