couch_potato 0.2.18 → 0.2.19
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.md +3 -0
- data/README.md +8 -0
- data/VERSION.yml +1 -1
- data/lib/couch_potato/view/model_view_spec.rb +19 -5
- data/lib/couch_potato/view/properties_view_spec.rb +3 -7
- data/spec/unit/model_view_spec_spec.rb +13 -0
- metadata +4 -2
data/CHANGES.md
CHANGED
data/README.md
CHANGED
@@ -197,6 +197,14 @@ Composite keys are also possible:
|
|
197
197
|
view :all, :key => [:created_at, :name]
|
198
198
|
end
|
199
199
|
|
200
|
+
You can also pass conditions as a JavaScript string:
|
201
|
+
|
202
|
+
class User
|
203
|
+
property :name
|
204
|
+
|
205
|
+
view :completed, :key => :name, :conditions => 'doc.completed = true'
|
206
|
+
end
|
207
|
+
|
200
208
|
The creation of views is based on view specification classes (see [CouchPotato::View::BaseViewSpec](http://rdoc.info/rdoc/langalex/couch_potato/blob/e8f0069e5529ad08a1bd1f02637ea8f1d6d0ab5b/CouchPotato/View/BaseViewSpec.html) and its descendants for more detailed documentation). The above code uses the ModelViewSpec class which is used to find models by their properties. For more sophisticated searches you can use other view specifications (either use the built-in or provide your own) by passing a type parameter:
|
201
209
|
|
202
210
|
If you have larger structures and you only want to load some attributes you can use the PropertiesViewSpec (the full class name is automatically derived):
|
data/VERSION.yml
CHANGED
@@ -5,6 +5,9 @@ module CouchPotato
|
|
5
5
|
#
|
6
6
|
# example:
|
7
7
|
# view :my_view, :key => :name
|
8
|
+
#
|
9
|
+
# in addition you can pass in conditions as a javascript string
|
10
|
+
# view :my_view_only_completed, :key => :name, :conditions => 'doc.completed = true'
|
8
11
|
class ModelViewSpec < BaseViewSpec
|
9
12
|
|
10
13
|
def view_parameters
|
@@ -17,11 +20,9 @@ module CouchPotato
|
|
17
20
|
end
|
18
21
|
|
19
22
|
def map_function
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
}
|
24
|
-
}"
|
23
|
+
map_body do
|
24
|
+
"emit(#{formatted_key(key)}, null);"
|
25
|
+
end
|
25
26
|
end
|
26
27
|
|
27
28
|
def reduce_function
|
@@ -40,6 +41,19 @@ module CouchPotato
|
|
40
41
|
|
41
42
|
private
|
42
43
|
|
44
|
+
def map_body(&block)
|
45
|
+
"function(doc) {
|
46
|
+
if(doc.#{JSON.create_id} && doc.#{JSON.create_id} == '#{@klass.name}'#{conditions_js}) {
|
47
|
+
" + yield + "
|
48
|
+
}
|
49
|
+
}"
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
def conditions_js
|
54
|
+
" && (#{options[:conditions]})" if options[:conditions]
|
55
|
+
end
|
56
|
+
|
43
57
|
def count?
|
44
58
|
view_parameters[:reduce]
|
45
59
|
end
|
@@ -6,11 +6,9 @@ module CouchPotato
|
|
6
6
|
# view :my_view, :key => :name, :properties => [:name, :author], :type => :properties
|
7
7
|
class PropertiesViewSpec < ModelViewSpec
|
8
8
|
def map_function
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
}
|
13
|
-
}"
|
9
|
+
map_body do
|
10
|
+
"emit(#{formatted_key(key)}, #{properties_for_map(properties)});"
|
11
|
+
end
|
14
12
|
end
|
15
13
|
|
16
14
|
def process_results(results)
|
@@ -32,8 +30,6 @@ module CouchPotato
|
|
32
30
|
def properties_for_map(properties)
|
33
31
|
'{' + properties.map{|p| "#{p}: doc.#{p}"}.join(', ') + '}'
|
34
32
|
end
|
35
|
-
|
36
|
-
|
37
33
|
end
|
38
34
|
end
|
39
35
|
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe CouchPotato::View::ModelViewSpec, 'map_function' do
|
4
|
+
it "should include conditions" do
|
5
|
+
spec = CouchPotato::View::ModelViewSpec.new Object, 'all', {:conditions => 'doc.closed = true'}, {}
|
6
|
+
spec.map_function.should include('if(doc.ruby_class && doc.ruby_class == \'Object\' && (doc.closed = true))')
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should not include conditions when they are nil" do
|
10
|
+
spec = CouchPotato::View::ModelViewSpec.new Object, 'all', {}, {}
|
11
|
+
spec.map_function.should include('if(doc.ruby_class && doc.ruby_class == \'Object\')')
|
12
|
+
end
|
13
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: couch_potato
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.19
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Lang
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-15 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -107,6 +107,7 @@ files:
|
|
107
107
|
- spec/unit/database_spec.rb
|
108
108
|
- spec/unit/dirty_attributes_spec.rb
|
109
109
|
- spec/unit/json_create_id_spec.rb
|
110
|
+
- spec/unit/model_view_spec_spec.rb
|
110
111
|
- spec/unit/rspec_matchers_spec.rb
|
111
112
|
- spec/unit/string_spec.rb
|
112
113
|
- spec/unit/view_query_spec.rb
|
@@ -159,6 +160,7 @@ test_files:
|
|
159
160
|
- spec/unit/database_spec.rb
|
160
161
|
- spec/unit/dirty_attributes_spec.rb
|
161
162
|
- spec/unit/json_create_id_spec.rb
|
163
|
+
- spec/unit/model_view_spec_spec.rb
|
162
164
|
- spec/unit/rspec_matchers_spec.rb
|
163
165
|
- spec/unit/string_spec.rb
|
164
166
|
- spec/unit/view_query_spec.rb
|