couch_view 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -86,7 +86,17 @@ Feature: CouchView::Config
86
86
  "reduce" => "_count"
87
87
  }
88
88
  }
89
+
89
90
 
91
+ Conditions are additive; the more you call conditions, the more conditions get added to it. If you attempt to add the same condition twice, it will silently ignore the second attempt:
92
+
93
+ config = CouchView::Config.new Article
94
+ config.conditions Published
95
+ config.conditions #==> [Published]
96
+ condig.conditions Visible
97
+ config.conditions #==> [Published, Visible]
98
+ config.conditions Published
99
+ config.conditions #==> [Published, Visible]
90
100
 
91
101
  @db
92
102
  Scenario: Generating a name based on the properties passed in to map over
@@ -224,7 +234,7 @@ Feature: CouchView::Config
224
234
  """
225
235
 
226
236
 
227
- @db @focus
237
+ @db
228
238
  Scenario: Creating conditions by passing a block to the map
229
239
 
230
240
  Given the following model:
@@ -347,7 +357,7 @@ Feature: CouchView::Config
347
357
  """
348
358
 
349
359
 
350
- @db @focus
360
+ @db
351
361
  Scenario: Giving your view a custom base name
352
362
 
353
363
  Given the following model:
@@ -394,3 +404,46 @@ Feature: CouchView::Config
394
404
  """
395
405
  @config.view_names.sort.should == ["funny", "funny_published", "funny_published_visible", "funny_visible"]
396
406
  """
407
+
408
+
409
+ @db @focus
410
+ Scenario: Adding the same condition multiple times to a config will result in the condition only being added once
411
+
412
+ Given the following model:
413
+ """
414
+ class Article < CouchRest::Model::Base
415
+ include CouchView
416
+ end
417
+ """
418
+
419
+ And the following conditions:
420
+ """
421
+ module Published
422
+ def conditions
423
+ "#{super} && doc.published == true"
424
+ end
425
+ end
426
+
427
+ module Visible
428
+ def conditions
429
+ "#{super} && doc.visible == true"
430
+ end
431
+ end
432
+ """
433
+
434
+ When I create a CouchView::Config for my Article model:
435
+ """
436
+ @config = CouchView::Config.new Article
437
+ """
438
+
439
+ Then adding the same condition multiple times will result in the condition only being added once:
440
+ """
441
+ @config.conditions Published
442
+ @config.conditions.should == [Published]
443
+ @config.conditions Visible
444
+ @config.conditions.should == [Published, Visible]
445
+ @config.conditions Published
446
+ @config.conditions.should == [Published, Visible]
447
+ @config.conditions Visible
448
+ @config.conditions.should == [Published, Visible]
449
+ """
@@ -1,10 +1,10 @@
1
+ @db
1
2
  Feature: CouchView
2
3
  As a programmer
3
4
  I want a `CouchView` mixin for my `CouchRest::Model::Base` models
4
5
  So that I can define maps and reduces on my model
5
6
 
6
7
 
7
- @db
8
8
  Scenario: Define a map over a property
9
9
  Given the following model definition:
10
10
  """
@@ -38,7 +38,6 @@ Feature: CouchView
38
38
  """
39
39
 
40
40
 
41
- @db @focus
42
41
  Scenario: Define a map on your model with conditions
43
42
  Given the following conditions:
44
43
  """
@@ -94,7 +93,6 @@ Feature: CouchView
94
93
  """
95
94
 
96
95
 
97
- @db
98
96
  Scenario: Define a map on your model with the `map` class method
99
97
  Given the following map definition:
100
98
  """
@@ -123,7 +121,6 @@ Feature: CouchView
123
121
  """
124
122
 
125
123
 
126
- @db
127
124
  Scenario: Retrieve a map proxy
128
125
 
129
126
  Given the following map definition:
@@ -162,7 +159,6 @@ Feature: CouchView
162
159
  """
163
160
 
164
161
 
165
- @db
166
162
  Scenario: Generate a reduce proxy for counting the number of results in your query
167
163
 
168
164
  Given the following map definition:
@@ -200,7 +196,7 @@ Feature: CouchView
200
196
  @proxy.class.should be(CouchView::Count::Proxy)
201
197
  """
202
198
 
203
- @db
199
+
204
200
  Scenario: Counting the number of rows in a reduce query
205
201
 
206
202
  Given the following map definition:
@@ -228,7 +224,7 @@ Feature: CouchView
228
224
  Article.count_by_id!.should == 4
229
225
  """
230
226
 
231
- @db
227
+
232
228
  Scenario: Query a map on your model
233
229
 
234
230
  Given the following map definition:
@@ -263,7 +259,6 @@ Feature: CouchView
263
259
  """
264
260
 
265
261
 
266
- @db
267
262
  Scenario: Defining a custom "reduce" on your view
268
263
 
269
264
  Given a Article model:
@@ -306,7 +301,7 @@ Feature: CouchView
306
301
  Article.reduce_by_label!['rows'].first['value'].should == -1
307
302
  """
308
303
 
309
- @db @focus
304
+
310
305
  Scenario: Giving your view a custom name
311
306
 
312
307
  Given the following model:
@@ -357,3 +352,63 @@ Feature: CouchView
357
352
  """
358
353
  Article.count_over_label!.should == 2
359
354
  """
355
+
356
+
357
+ @focus
358
+ Scenario: Chaining together multiple conditions by defining a map multiple times
359
+
360
+ Given the following model:
361
+ """
362
+ class Article < CouchRest::Model::Base
363
+ include CouchView
364
+ end
365
+ """
366
+
367
+ And the following conditions:
368
+ """
369
+ module Published
370
+ def conditions
371
+ "#{super} && doc.published == true"
372
+ end
373
+ end
374
+
375
+ module Visible
376
+ def conditions
377
+ "#{super} && doc.visible == true"
378
+ end
379
+ end
380
+ """
381
+
382
+ When I define a map over labels that includes a published condition:
383
+ """
384
+ Article.map :label do
385
+ conditions Published
386
+ end
387
+ """
388
+
389
+ Then Article should respond to "by_by_label_published":
390
+ """
391
+ proc { Article.by_by_label_published }.should_not raise_exception
392
+ """
393
+
394
+ When I update the map over label definition with a visible condition:
395
+ """
396
+ Article.map :label do
397
+ conditions Visible
398
+ end
399
+ """
400
+
401
+ Then Article should respond to "by_by_label_published":
402
+ """
403
+ proc { Article.by_by_label_published }.should_not raise_exception
404
+ """
405
+
406
+ Then Article should respond to "by_by_label_visible":
407
+ """
408
+ proc { Article.by_by_label_visible }.should_not raise_exception
409
+ """
410
+
411
+ Then Article should respond to "by_by_label_published_visible":
412
+ """
413
+ proc { Article.by_by_label_published_visible }.should_not raise_exception
414
+ """
@@ -9,3 +9,7 @@ end
9
9
  When /^I give it a base name of .*:$/ do |code|
10
10
  eval code
11
11
  end
12
+
13
+ Then /^adding the same condition multiple times will result in the condition only being added once:$/ do |code|
14
+ eval code
15
+ end
@@ -85,3 +85,15 @@ end
85
85
  Then /^".*" should return.*:$/ do |code|
86
86
  eval code
87
87
  end
88
+
89
+ When /^I define a map over labels that includes a published condition:$/ do |code|
90
+ eval code
91
+ end
92
+
93
+ Then /^Article should respond to ".*":$/ do |code|
94
+ eval code
95
+ end
96
+
97
+ When /^I update the map over label definition with a visible condition:$/ do |code|
98
+ eval code
99
+ end
@@ -25,6 +25,7 @@ module CouchView
25
25
  @conditions
26
26
  else
27
27
  @conditions += args
28
+ @conditions.uniq!
28
29
  end
29
30
  end
30
31
 
@@ -12,12 +12,19 @@ module CouchView
12
12
  view_config = CouchView::Config.new self
13
13
  view_config.instance_eval &block
14
14
  view_config.base_view_name name if name
15
+ base_view_name = view_config.base_view_name
16
+
17
+ if view_configs[base_view_name]
18
+ view_config.conditions *view_configs[base_view_name].conditions
19
+ view_configs[base_view_name] = view_config
20
+ else
21
+ view_configs[base_view_name] = view_config
22
+ end
23
+
15
24
  view_config.views.each do |view_name, view|
16
25
  view_by view_name, :map => view[:map], :reduce => view[:reduce]
17
26
  end
18
27
 
19
- base_view_name = view_config.base_view_name
20
-
21
28
  instance_eval <<-METHODS
22
29
  def map_#{base_view_name}!
23
30
  generate_view_proxy_for("#{base_view_name}").get!
@@ -57,5 +64,9 @@ module CouchView
57
64
  def generate_view_proxy_for(view)
58
65
  CouchView::Proxy.new self, "by_#{view}".to_sym
59
66
  end
67
+
68
+ def view_configs
69
+ @view_configs ||= {}
70
+ end
60
71
  end
61
72
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couch_view
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt Parker
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-08-17 00:00:00 Z
18
+ date: 2011-08-18 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: couchrest