couchrest_model_search 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ v0.0.6
2
+ -- `escaped_search` now available on database (raw result response)
3
+
1
4
  v0.0.5
2
5
  -- new feature: `escaped_search` (see README)
3
6
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- couchrest_model_search (0.0.4)
4
+ couchrest_model_search (0.0.5)
5
5
  couchrest_model (~> 1.0.0)
6
6
 
7
7
  GEM
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
@@ -66,7 +66,25 @@ module CouchRest
66
66
  end
67
67
  end
68
68
 
69
+ module CouchRest
70
+ module Search
71
+ module Escape
72
+ def escape_special_characters(query)
73
+ new_query = query.dup
74
+ lucene_special_characters.map {|c| new_query.gsub!(c, %{\\} + c)}
75
+ new_query
76
+ end
77
+
78
+ def lucene_special_characters
79
+ @lucene_special_characters ||= %w[\ + - && || ! ( ) { } [ ] ^ " ~ * ? :]
80
+ end
81
+ end
82
+ end
83
+ end
84
+
69
85
  class CouchRest::Database
86
+ include CouchRest::Search::Escape
87
+
70
88
  def search(klass, view_fn, query, options={})
71
89
  url = CouchRest.paramify_url("#{@root}/_fti/_design/#{klass}/#{view_fn}", options.merge(:q => query))
72
90
  ActiveSupport::Notifications.instrument("search.lucene",
@@ -74,22 +92,25 @@ class CouchRest::Database
74
92
  CouchRest.get url
75
93
  end
76
94
  end
95
+
96
+ def escaped_search(klass, view_fn, query, options={})
97
+ search klass, view_fn, escape_special_characters(query), options
98
+ end
77
99
  end
78
100
 
79
101
  class CouchRest::Model::Base
80
- def self.search(query, view_fn="by_fulltext", options={})
81
- options[:include_docs] = true
82
- ret = self.database.search(self.to_s, view_fn, query, options)
83
- ret['rows'].map {|r| self.new(r['doc'])}
84
- end
102
+ class << self
103
+ include CouchRest::Search::Escape
85
104
 
86
- def self.escaped_search(query, view_fn="by_fulltext", options={})
87
- self.lucene_special_characters.map {|c| query.gsub!(c, %{\\} + c)}
88
- self.search query, view_fn, options
89
- end
105
+ def search(query, view_fn="by_fulltext", options={})
106
+ options[:include_docs] = true
107
+ ret = self.database.search(self.to_s, view_fn, query, options)
108
+ ret['rows'].map {|r| self.new(r['doc'])}
109
+ end
90
110
 
91
- def self.lucene_special_characters
92
- @lucene_special_characters ||= %w[\ + - && || ! ( ) { } [ ] ^ " ~ * ? :]
111
+ def escaped_search(query, view_fn="by_fulltext", options={})
112
+ self.search escape_special_characters(query), view_fn, options
113
+ end
93
114
  end
94
115
 
95
116
  # example search functions
@@ -17,12 +17,19 @@ describe "CouchrestModelSearch" do
17
17
  end
18
18
  end
19
19
 
20
- describe "##escaped_search" do
20
+ describe "CouchRest::Model::Base##escaped_search" do
21
21
  it "should escape the first parameter, then pass all args off to the search method" do
22
22
  Article.should_receive(:search).with('hello\:there', "by_fulltext", {}).and_return nil
23
23
  Article.escaped_search "hello:there"
24
24
  end
25
25
  end
26
+
27
+ describe "CouchRest::Database##escaped_search" do
28
+ it "should escape the third parameter, then pass all args off to the search method" do
29
+ Article.database.should_receive(:search).with(Article, "by_fulltext", 'hello\:there', {}).and_return nil
30
+ Article.database.escaped_search Article, "by_fulltext", "hello:there"
31
+ end
32
+ end
26
33
 
27
34
  describe "overwrite design doc" do
28
35
  class CustomArticle < CouchRest::Model::Base
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: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dorren Chen