langalex-couch_potato 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -173,9 +173,9 @@ Composite keys are also possible:
173
173
  view :all, :key => [:created_at, :name]
174
174
  end
175
175
 
176
- The creation of views is based on view specification classes (see the CouchPotato::View) module. The above code used the ModelViewSpec class which is used to the simple find model by property searches. For more sophisticated searches you can use other view specifications (either use the built-in or provide your own) by passing a type parameter:
176
+ The creation of views is based on view specification classes (see the CouchPotato::View module). 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:
177
177
 
178
- If you have larger structures and you only want to load some attributes you can customize the view you can use the PropertiesViewSpec (the full class name is automatically derived):
178
+ 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):
179
179
 
180
180
  class User
181
181
  property :name
@@ -209,6 +209,8 @@ To process this raw data you can also pass in a results filter:
209
209
 
210
210
  In this case querying the view would only return the emitted value for each row.
211
211
 
212
+ You can pass in your own view specifications by passing in :type => MyViewSpecClass. Take a look at the CouchPotato::View::*ViewSpec classes to get an idea of how this works.
213
+
212
214
  #### Associations
213
215
 
214
216
  Not supported. Not sure if they ever will be. You can implement those yourself using views and custom methods on your models.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
3
  :minor: 2
4
- :patch: 4
4
+ :patch: 5
@@ -18,11 +18,19 @@ module CouchPotato
18
18
  def view(view_name, options)
19
19
  self.class.instance_eval do
20
20
  define_method view_name do |view_parameters = {}|
21
- klass = options[:type] ? options[:type].to_s.camelize : 'Model'
22
- CouchPotato::View.const_get("#{klass}ViewSpec").new self, view_name, options, view_parameters
21
+ view_spec_class(options[:type]).new self, view_name, options, view_parameters
23
22
  end
24
23
  end
25
24
  end
25
+
26
+ def view_spec_class(type)
27
+ if type && type.is_a?(Class)
28
+ type
29
+ else
30
+ name = type.nil? ? 'Model' : type.to_s.camelize
31
+ CouchPotato::View.const_get("#{name}ViewSpec")
32
+ end
33
+ end
26
34
  end
27
35
  end
28
36
  end
@@ -1,29 +1,51 @@
1
1
  module CouchPotato
2
2
  module View
3
- # A view to return model instances by searching its properties
3
+ # A view to return model instances by searching its properties.
4
+ # If you pass reduce => true will count instead
4
5
  #
5
6
  # example:
6
7
  # view :my_view, :key => :name
7
8
  class ModelViewSpec < BaseViewSpec
8
9
 
9
10
  def view_parameters
10
- {:include_docs => true}.merge(super)
11
+ _super = super
12
+ if _super[:reduce]
13
+ _super
14
+ else
15
+ {:include_docs => true, :reduce => false}.merge(_super)
16
+ end
11
17
  end
12
18
 
13
19
  def map_function
14
20
  "function(doc) {
15
- emit(#{formatted_key(key)}, null);
21
+ if(doc.ruby_class && doc.ruby_class == '#{@klass.name}') {
22
+ emit(#{formatted_key(key)}, null);
23
+ }
16
24
  }"
17
25
  end
18
26
 
27
+ def reduce_function
28
+ "function(key, values) {
29
+ return values.length;
30
+ }"
31
+ end
32
+
19
33
  def process_results(results)
20
- results['rows'].map do |row|
21
- klass.json_create row['doc']
34
+ if count?
35
+ results['rows'].first['value']
36
+ else
37
+ results['rows'].map do |row|
38
+ klass.json_create row['doc']
39
+ end
22
40
  end
23
41
  end
24
42
 
25
43
  private
26
44
 
45
+ def count?
46
+ view_parameters[:reduce]
47
+ end
48
+
27
49
  def key
28
50
  options[:key]
29
51
  end
@@ -7,7 +7,9 @@ module CouchPotato
7
7
  class PropertiesViewSpec < ModelViewSpec
8
8
  def map_function
9
9
  "function(doc) {
10
- emit(#{formatted_key(key)}, #{properties_for_map(properties)});
10
+ if(doc.ruby_class && doc.ruby_class == '#{@klass.name}') {
11
+ emit(#{formatted_key(key)}, #{properties_for_map(properties)});
12
+ }
11
13
  }"
12
14
  end
13
15
 
@@ -7,6 +7,7 @@ class Build
7
7
  property :time
8
8
 
9
9
  view :timeline, :key => :time
10
+ view :count, :key => :time, :reduce => true
10
11
  view :minimal_timeline, :key => :time, :properties => [:state], :type => :properties
11
12
  view :key_array_timeline, :key => [:time, :state]
12
13
  view :custom_timeline, :map => "function(doc) { emit(doc._id, {state: 'custom_' + doc.state}); }", :type => :custom
@@ -33,26 +34,36 @@ describe 'view' do
33
34
  CouchPotato.database.view Build.timeline(:key => 1)
34
35
  end
35
36
 
37
+ it "should not return documents that don't have a matching ruby_class" do
38
+ CouchPotato.couchrest_database.save_doc({:time => 'x'})
39
+ CouchPotato.database.view(Build.timeline).should == []
40
+ end
41
+
42
+ it "should count documents" do
43
+ CouchPotato.database.save_document Build.new(:state => 'success', :time => '2008-01-01')
44
+ CouchPotato.database.view(Build.count(:reduce => true)).should == 1
45
+ end
46
+
36
47
  describe "properties defined" do
37
48
  it "should assign the configured properties" do
38
- CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01'})
49
+ CouchPotato.couchrest_database.save_doc(:state => 'success', :time => '2008-01-01', :ruby_class => 'Build')
39
50
  CouchPotato.database.view(Build.minimal_timeline).first.state.should == 'success'
40
51
  end
41
52
 
42
53
  it "should not assign the properties not configured" do
43
- CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01'})
54
+ CouchPotato.couchrest_database.save_doc(:state => 'success', :time => '2008-01-01', :ruby_class => 'Build')
44
55
  CouchPotato.database.view(Build.minimal_timeline).first.time.should be_nil
45
56
  end
46
57
 
47
58
  it "should assign the id even if it is not configured" do
48
- id = CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01'})['id']
59
+ id = CouchPotato.couchrest_database.save_doc(:state => 'success', :time => '2008-01-01', :ruby_class => 'Build')['id']
49
60
  CouchPotato.database.view(Build.minimal_timeline).first._id.should == id
50
61
  end
51
62
  end
52
63
 
53
64
  describe "no properties defined" do
54
65
  it "should assign all properties to the objects by default" do
55
- id = CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01'})['id']
66
+ id = CouchPotato.couchrest_database.save_doc({:state => 'success', :time => '2008-01-01', :ruby_class => 'Build'})['id']
56
67
  result = CouchPotato.database.view(Build.timeline).first
57
68
  result.state.should == 'success'
58
69
  result.time.should == '2008-01-01'
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe CouchPotato::View::CustomViews do
4
+
5
+ class MyViewSpec; end
6
+ class ModelWithView
7
+ include CouchPotato::Persistence
8
+ view :all, :type => MyViewSpec
9
+ end
10
+
11
+ it "should use a custom viewspec class" do
12
+ MyViewSpec.should_receive(:new)
13
+ ModelWithView.all
14
+ end
15
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: langalex-couch_potato
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
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-05-19 00:00:00 -07:00
12
+ date: 2009-05-21 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -86,6 +86,7 @@ files:
86
86
  - spec/spec_helper.rb
87
87
  - spec/unit/attributes_spec.rb
88
88
  - spec/unit/create_spec.rb
89
+ - spec/unit/customs_views_spec.rb
89
90
  - spec/unit/database_spec.rb
90
91
  - spec/unit/dirty_attributes_spec.rb
91
92
  - spec/unit/string_spec.rb
@@ -126,6 +127,7 @@ test_files:
126
127
  - spec/spec_helper.rb
127
128
  - spec/unit/attributes_spec.rb
128
129
  - spec/unit/create_spec.rb
130
+ - spec/unit/customs_views_spec.rb
129
131
  - spec/unit/database_spec.rb
130
132
  - spec/unit/dirty_attributes_spec.rb
131
133
  - spec/unit/string_spec.rb