cyx-solr_query 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Cyril David
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = solr_query
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Cyril David. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "solr_query"
8
+ gem.summary = %Q{TODO}
9
+ gem.email = "cyx.ucron@gmail.com"
10
+ gem.homepage = "http://github.com/cyx/solr_query"
11
+ gem.authors = ["Cyril David"]
12
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ end
18
+
19
+ require 'rake/testtask'
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = 'test/**/*_test.rb'
23
+ test.verbose = true
24
+ end
25
+
26
+ begin
27
+ require 'rcov/rcovtask'
28
+ Rcov::RcovTask.new do |test|
29
+ test.libs << 'test'
30
+ test.pattern = 'test/**/*_test.rb'
31
+ test.verbose = true
32
+ end
33
+ rescue LoadError
34
+ task :rcov do
35
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
36
+ end
37
+ end
38
+
39
+
40
+ task :default => :test
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ if File.exist?('VERSION.yml')
45
+ config = YAML.load(File.read('VERSION.yml'))
46
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
47
+ else
48
+ version = ""
49
+ end
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "solr_query #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
56
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/solr_query.rb ADDED
@@ -0,0 +1,78 @@
1
+ require 'activesupport'
2
+
3
+ class SolrQuery
4
+ AND = ' AND '
5
+ OR = ' OR '
6
+
7
+ def self.escape( str )
8
+ str.to_s.gsub(/([!+\-\(\)\{\}\[\]^\"\~\*\?\:\\])/, '\\\\\1')
9
+ end
10
+
11
+ def self.create( operator = AND )
12
+ solr_query = new
13
+ yield solr_query
14
+ solr_query.to_s( operator )
15
+ end
16
+
17
+ attr_reader :conditions
18
+
19
+ def initialize
20
+ @conditions = []
21
+ @stream = [ [] ]
22
+ end
23
+
24
+ def union( &block )
25
+ do_operation( OR, &block )
26
+ end
27
+
28
+ def intersection( &block )
29
+ do_operation( AND, &block )
30
+ end
31
+
32
+ def push( condition )
33
+ stream.push( condition )
34
+ end
35
+
36
+ def stream
37
+ @stream[-1]
38
+ end
39
+
40
+ def condition( field, value, escaping = true )
41
+ return if value.blank?
42
+ stream.push("#{field}:#{escape(value, escaping)}")
43
+ end
44
+
45
+ def term( value, escaping = true )
46
+ return if value.to_s.strip.blank?
47
+ stream.push( escape(value, escaping) )
48
+ end
49
+
50
+ def like( value, escaping = true )
51
+ "#{escape(value.downcase, escaping)}*"
52
+ end
53
+
54
+ def to_s( operator = AND )
55
+ stream.join( operator )
56
+ end
57
+
58
+ protected
59
+ def parenthesize( str )
60
+ "( #{str} )"
61
+ end
62
+
63
+ def do_operation( op )
64
+ solr_query = SolrQuery.new
65
+ @stream << solr_query
66
+ yield
67
+ generated = @stream.pop.to_s( op )
68
+ stream.push(parenthesize(generated)) if generated.present?
69
+ end
70
+
71
+ def escape( str, escaping = true )
72
+ if escaping
73
+ self.class.escape( str )
74
+ else
75
+ str
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,156 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class SolrQueryTest < Test::Unit::TestCase
4
+ context "given term 'quick brown'" do
5
+ should "be quick brown" do
6
+ result = SolrQuery.create do |q|
7
+ q.term 'quick brown'
8
+ end
9
+
10
+ assert_equal 'quick brown', result
11
+ end
12
+ end
13
+
14
+ context "given term ' '" do
15
+ should "be ''" do
16
+ result = SolrQuery.create { |q| q.term ' ' }
17
+ assert_equal '', result
18
+ end
19
+ end
20
+
21
+ context "given term '' condition 'category_id', nil" do
22
+ should " be '' " do
23
+ result = SolrQuery.create do |q|
24
+ q.term ''
25
+ q.condition 'category_id', nil
26
+ end
27
+
28
+ assert_equal '', result
29
+ end
30
+ end
31
+ # GIVEN:
32
+ #
33
+ # SolrQuery.create do |q|
34
+ # q.union {
35
+ # q.condition 'allowed', 'all'
36
+ #
37
+ # q.union {
38
+ # q.condition 'allowed', 'contacts'
39
+ # q.condition 'poster_contact_ids', 1
40
+ # }
41
+ # }
42
+ # end
43
+ context "given the query above" do
44
+ should "be allowed:all OR (allowed:contacts OR poster_contact_ids:1)" do
45
+ result = SolrQuery.create do |q|
46
+ q.union {
47
+ q.condition 'allowed', 'all'
48
+
49
+ q.union {
50
+ q.condition 'allowed', 'contacts'
51
+ q.condition 'poster_contact_ids', 1
52
+ }
53
+ }
54
+ end
55
+
56
+ assert_equal '( allowed:all OR ( allowed:contacts OR poster_contact_ids:1 ) )', result
57
+ end
58
+ end
59
+
60
+ context "given condition 'allowed', 'all'" do
61
+ should "be allowed:all" do
62
+ result = SolrQuery.create do |q|
63
+ q.condition 'allowed', 'all'
64
+ end
65
+ assert_equal 'allowed:all', result
66
+ end
67
+ end
68
+
69
+ context "given condition 'allowed', 'all' then condition 'poster_contact_ids', 1" do
70
+ should "be allowed:all AND poster_contact_ids:1" do
71
+ result = SolrQuery.create do |q|
72
+ q.condition 'allowed', 'all'
73
+ q.condition 'poster_contact_ids', 1
74
+ end
75
+
76
+ assert_equal 'allowed:all AND poster_contact_ids:1', result
77
+ end
78
+ end
79
+
80
+ context "given condition 'allowed', 'all' union { condition 'poster_contact_ids', 1 condition 'hoy', 'hey' }" do
81
+ should "be allowed:all AND (poster_contact_ids:1 OR hoy:hey)" do
82
+ result = SolrQuery.create do |q|
83
+ q.condition 'allowed', 'all'
84
+ q.union {
85
+ q.condition 'poster_contact_ids', 1
86
+ q.condition 'hoy', 'hey'
87
+ }
88
+ end
89
+
90
+ assert_equal 'allowed:all AND ( poster_contact_ids:1 OR hoy:hey )', result
91
+ end
92
+ end
93
+
94
+ context "given condition 'allowed', 'all' intersection { condition 'poster_contact_ids', 1 condition 'hoy', 'hey' }" do
95
+ should "be allowed:all AND (poster_contact_ids:1 AND hoy:hey)" do
96
+ result = SolrQuery.create do |q|
97
+ q.condition 'allowed', 'all'
98
+ q.intersection {
99
+ q.condition 'poster_contact_ids', 1
100
+ q.condition 'hoy', 'hey'
101
+ }
102
+ end
103
+
104
+ assert_equal 'allowed:all AND ( poster_contact_ids:1 AND hoy:hey )', result
105
+ end
106
+ end
107
+
108
+ # union {
109
+ # set :allowed, 'all'
110
+ #
111
+ # intersection {
112
+ # set :allowed, 'contacts'
113
+ # set :poster_contact_ids, 1
114
+ # }
115
+ #
116
+ # union {
117
+ # set :allowed, 1
118
+ # set :allowed, 2
119
+ # }
120
+ # }
121
+ #
122
+ context "given the pseudo code above" do
123
+ should "be (allowed:all OR (allowed:contacts AND poster_contact_ids:1) OR (allowed:1 OR allowed:2)" do
124
+ result = SolrQuery.create do |q|
125
+ q.union {
126
+ q.condition 'allowed', 'all'
127
+
128
+ q.intersection {
129
+ q.condition 'allowed', 'contacts'
130
+ q.condition 'poster_contact_ids', 1
131
+ }
132
+
133
+ q.union {
134
+ q.condition 'allowed', 1
135
+ q.condition 'allowed', 2
136
+ }
137
+ }
138
+ end
139
+
140
+ assert_equal '( allowed:all OR ( allowed:contacts AND poster_contact_ids:1 ) OR ( allowed:1 OR allowed:2 ) )', result
141
+ end
142
+ end
143
+
144
+ context "given condition 'allowed', 'all' intersection { }" do
145
+ should "be allowed:all" do
146
+ result = SolrQuery.create do |q|
147
+ q.condition 'allowed', 'all'
148
+ q.intersection {
149
+
150
+ }
151
+ end
152
+
153
+ assert_equal 'allowed:all', result
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+
8
+ require 'solr_query'
9
+
10
+ class Test::Unit::TestCase
11
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cyx-solr_query
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Cyril David
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-03 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: cyx.ucron@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - lib/solr_query.rb
33
+ - test/solr_query_test.rb
34
+ - test/test_helper.rb
35
+ has_rdoc: false
36
+ homepage: http://github.com/cyx/solr_query
37
+ post_install_message:
38
+ rdoc_options:
39
+ - --charset=UTF-8
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ requirements: []
55
+
56
+ rubyforge_project:
57
+ rubygems_version: 1.2.0
58
+ signing_key:
59
+ specification_version: 3
60
+ summary: TODO
61
+ test_files:
62
+ - test/solr_query_test.rb
63
+ - test/test_helper.rb