couchrest_model_search 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -13,13 +13,14 @@ This add search method calls to couchrest_model gem. Assuming you are using couc
13
13
  # by default, couchrest_model only update view_by functions.
14
14
  Article.update_search_doc
15
15
 
16
- @articles = Article.search 'by_content', "apple" # search by keyword apple
16
+ @articles = Article.search "apple" # search by keyword apple
17
17
 
18
- # you can also custom the search function.
18
+ # you can also customize the search function.
19
19
  class CustomArticle < CouchRest::Model::Base
20
- def self.fulltext
21
- { "by_content" => {"index" => %(function(doc) {})}
22
- }
23
- end
24
- end
25
-
20
+ search_by :fulltext,
21
+ :index => %(
22
+ function(doc) {
23
+ // ....
24
+ }
25
+ )
26
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{couchrest_model_search}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dorren Chen"]
12
- s.date = %q{2010-10-13}
12
+ s.date = %q{2010-10-18}
13
13
  s.description = %q{Add search function to CouchRest Model, assuming you're using couchdb-lucene}
14
14
  s.email = %q{dorrenchen@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -4,46 +4,62 @@ module CouchRest
4
4
  module Model
5
5
  module DesignDoc
6
6
  module ClassMethods
7
- # the default search function
8
- def fulltext
9
- { "by_content" => {
10
- "index" => %(
11
- function(doc) {
12
- var ret = new Document();
13
- function idx(obj) {
14
- for (var key in obj){
15
- switch (typeof obj[key]) {
16
- case 'object':
17
- idx(obj[key]);
18
- break;
19
- case 'function':
20
- break;
21
- default:
22
- ret.add(obj[key]);
23
- break;
7
+ alias_method :orig_default_design_doc, :default_design_doc
8
+ def default_design_doc
9
+ orig_default_design_doc.update "fulltext" =>
10
+ { "by_fulltext" => {
11
+ "index" => %(
12
+ function(doc) {
13
+ var ret = new Document();
14
+ function idx(obj) {
15
+ for (var key in obj){
16
+ switch (typeof obj[key]) {
17
+ case 'object':
18
+ idx(obj[key]);
19
+ break;
20
+ case 'function':
21
+ break;
22
+ default:
23
+ ret.add(obj[key]);
24
+ break;
25
+ }
24
26
  }
25
- }
26
- };
27
+ };
27
28
 
28
- if (doc['couchrest-type'] == '#{self.to_s}') {
29
- idx(doc);
29
+ if (doc['couchrest-type'] == '#{self.to_s}') {
30
+ idx(doc);
30
31
 
31
- if (doc._attachments) {
32
- for (var i in doc._attachments) {
33
- ret.attachment('attachment', i);
32
+ if (doc._attachments) {
33
+ for (var i in doc._attachments) {
34
+ ret.attachment('attachment', i);
35
+ }
34
36
  }
35
37
  }
38
+ return ret;
36
39
  }
37
- return ret;
38
- }
39
- )
40
+ )
41
+ }
40
42
  }
41
- }
42
43
  end
43
-
44
- alias_method :orig_default_design_doc, :default_design_doc
45
- def default_design_doc
46
- orig_default_design_doc.merge "fulltext" => fulltext
44
+
45
+ def update_search_doc
46
+ saved = stored_design_doc
47
+ if saved
48
+ saved["fulltext"] = design_doc["fulltext"]
49
+ saved.save
50
+ saved
51
+ else
52
+ design_doc.delete("_rev")
53
+ design_doc.database = database
54
+ design_doc.save
55
+ design_doc
56
+ end
57
+ end
58
+
59
+ alias_method :orig_save_design_doc, :save_design_doc
60
+ def save_design_doc(db = database, force = false)
61
+ orig_save_design_doc(db, force)
62
+ update_search_doc
47
63
  end
48
64
  end
49
65
  end
@@ -61,16 +77,28 @@ class CouchRest::Database
61
77
  end
62
78
 
63
79
  class CouchRest::Model::Base
64
- def self.search(view_fn, query, options={})
80
+ def self.search(query, view_fn="by_fulltext", options={})
65
81
  options[:include_docs] = true
66
82
  ret = self.database.search(self.to_s, view_fn, query, options)
67
83
  ret['rows'].map {|r| self.new(r['doc'])}
68
84
  end
69
85
 
70
- def self.update_search_doc
71
- dbdoc = database.get "_design/#{self.to_s}"
72
- dbdoc.update design_doc
73
- dbdoc.save
86
+ # example search functions
87
+ # class Aritlce
88
+ # search_by :title,
89
+ # :index => %(
90
+ # function(doc) {
91
+ # // ....
92
+ # }
93
+ # )
94
+ #
95
+ # now you can make search like
96
+ # Article.search "apple" # by default it uses "by_fulltext" search function
97
+ # Article.search "apple", :by_title
98
+ def self.search_by(fn_name, fn)
99
+ design_doc["fulltext"] ||= {}
100
+ design_doc["fulltext"]["by_#{fn_name}"] = fn.stringify_keys!
101
+ req_design_doc_refresh
74
102
  end
75
103
  end
76
104
 
@@ -1,22 +1,52 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- describe "CouchrestModelSearch" do
4
- class Article < CouchRest::Model::Base
3
+ class Article < CouchRest::Model::Base
4
+ use_database DB
5
+ end
6
+
7
+ describe "CouchrestModelSearch" do
8
+ before :each do
9
+ if doc = Article.stored_design_doc
10
+ doc.database = Article.database
11
+ doc.destroy
12
+ end
5
13
  end
6
14
 
7
15
  it "should overwrite default_design_doc" do
8
16
  Article.design_doc["fulltext"].should_not be_nil
9
- end
17
+ end
10
18
  end
11
-
12
- describe "CouchrestModelSearch" do
19
+
20
+ describe "overwrite design doc" do
13
21
  class CustomArticle < CouchRest::Model::Base
14
- def self.fulltext
15
- { "by_content" => {"index" => %(function(doc) {})}}
22
+ use_database DB
23
+ search_by :fulltext,
24
+ {:index => %(function(doc) {})}
25
+ end
26
+
27
+ before :each do
28
+ if doc = CustomArticle.stored_design_doc
29
+ doc.database = CustomArticle.database
30
+ doc.destroy
16
31
  end
17
32
  end
18
33
 
19
34
  it "should allow class to overwrite fulltext function" do
20
- CustomArticle.design_doc["fulltext"].should == CustomArticle.fulltext
35
+ CustomArticle.update_search_doc
36
+ CustomArticle.stored_design_doc["fulltext"]["by_fulltext"]["index"].should == %(function(doc) {})
37
+ end
38
+
39
+ it "should update search doc" do
40
+ CustomArticle.update_search_doc
41
+ CustomArticle.stored_design_doc["fulltext"]["by_fulltext"]["index"].should == %(function(doc) {})
42
+
43
+ class CustomArticle < CouchRest::Model::Base
44
+ use_database DB
45
+ search_by :fulltext,
46
+ {:index => %(function(doc) {// hello})}
47
+ end
48
+
49
+ CustomArticle.update_search_doc
50
+ CustomArticle.stored_design_doc["fulltext"]["by_fulltext"]["index"].should == %(function(doc) {// hello})
21
51
  end
22
52
  end
data/spec/spec_helper.rb CHANGED
@@ -4,6 +4,9 @@ require 'couchrest_model_search'
4
4
  require 'rspec'
5
5
  require 'rspec/autorun'
6
6
 
7
+ COUCHDB_SERVER = CouchRest.new "http://admin:password@localhost:5984"
8
+ DB = COUCHDB_SERVER.database!('couchrest_model_search_test')
9
+
7
10
  RSpec.configure do |config|
8
11
  # == Mock Framework
9
12
  #
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couchrest_model_search
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
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
  - Dorren Chen
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-13 00:00:00 -04:00
18
+ date: 2010-10-18 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency