mwmitchell-rsolr 0.7.0 → 0.7.1

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/CHANGES.txt CHANGED
@@ -1,3 +1,8 @@
1
+ 0.7.1 - February 27, 2009
2
+ Added simple query helper module -> RSolr::Query
3
+ Added tests for RSolr::Query
4
+ Modified Test::Unit::TestCase in test/test_helpers.rb
5
+
1
6
  0.7.0 - February 20, 2009
2
7
  Removed all param mapping behavior, code and tests
3
8
  - this stuff just gunks up rsolr and should be in an extension of some sort
@@ -17,7 +22,7 @@
17
22
  updated comments for #search method
18
23
  Updated tests
19
24
  Bumped up version
20
-
25
+
21
26
  0.6.8 - January 28, 2009
22
27
  New method added to RSolr::Connection::Base - #find_values_for_facet
23
28
  This method searches for facet values only, and sets the :rows param to 0
data/README.rdoc CHANGED
@@ -55,7 +55,7 @@ Use the #query method to send requests to the /select handler:
55
55
  response = solr.find_by_id(1)
56
56
 
57
57
  ==== Pagination
58
- Pagination is simplified from having a few helpful response methods:
58
+ Pagination is simplified by having a few helpful response methods:
59
59
 
60
60
  response = solr.query(:start=>0, :rows=>10, :q=>'*:*')
61
61
  response.per_page
@@ -0,0 +1,58 @@
1
+ module RSolr::Query
2
+
3
+ module HelperMethods
4
+
5
+ # returns a quoted or non-quoted string
6
+ # "value" should be a string
7
+ # "quote" should be true/false
8
+ def prep_value(value, quote)
9
+ quote ? %("#{value}") : value
10
+ end
11
+
12
+ # value can be a string, array, hash or symbol
13
+ # symbols are treated as strings
14
+ # arrays are recursed through #build_query
15
+ # keys for hashes are fields for fielded queries, the values are recused through #build_query
16
+ # strings/symbols are sent to #prep_value for possible quoting
17
+ #
18
+ # opts can have:
19
+ # :quote=>bool - default false
20
+ # :join=>string - default ' '
21
+ def build_query(value, opts={})
22
+ opts[:join]||=' '
23
+ opts[:quote]||=false
24
+ result = (
25
+ case value
26
+ when Array
27
+ value.collect do |item|
28
+ build_query item, opts
29
+ end.flatten
30
+ when String,Symbol
31
+ [prep_value(value.to_s, opts[:quote])]
32
+ when Hash
33
+ value.collect do |(k,v)|
34
+ "#{k}:#{build_query(v, opts)}"
35
+ end.flatten
36
+ else
37
+ [prep_value(value.to_s, opts[:quote])]
38
+ end
39
+ )
40
+ opts[:join] ? result.join(opts[:join]) : result
41
+ end
42
+
43
+ # start_for(2, 10)
44
+ # calculates the :start value for pagination etc..
45
+ def start_for(current_page, per_page)
46
+ page = current_page.to_s.to_i
47
+ page = page > 0 ? page : 1
48
+ ((page - 1) * (per_page || 0))
49
+ end
50
+
51
+ end # end HelperMethods
52
+
53
+ # Easy access: RSolr::Query::Helper.start_for(page=1, per_page=10)
54
+ class Helper
55
+ extend HelperMethods
56
+ end
57
+
58
+ end
data/lib/rsolr.rb CHANGED
@@ -7,13 +7,14 @@ proc {|base, files|
7
7
 
8
8
  module RSolr
9
9
 
10
- VERSION = '0.7.0'
10
+ VERSION = '0.7.1'
11
11
 
12
12
  autoload :Message, 'rsolr/message'
13
13
  autoload :Response, 'rsolr/response'
14
14
  autoload :Connection, 'rsolr/connection'
15
15
  autoload :Indexer, 'rsolr/indexer'
16
16
  autoload :HTTPClient, 'rsolr/http_client'
17
+ autoload :Query, 'rsolr/query'
17
18
 
18
19
  # factory for creating connections
19
20
  # opts[:adapter] is either :http or :direct
@@ -0,0 +1,30 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helpers.rb')
2
+
3
+ class RSolrQueryHelperTest < RSolrBaseTest
4
+
5
+ H = RSolr::Query::Helper
6
+
7
+ test 'pre_value' do
8
+ value = 'the man'
9
+ assert_equal 'the man', H.prep_value(value, false)
10
+ assert_equal "\"the man\"", H.prep_value(value, :quote=>true)
11
+ end
12
+
13
+ test 'build_query' do
14
+ assert_equal 'testing', H.build_query('testing')
15
+ assert_equal '"testing"', H.build_query('testing', :quote=>true)
16
+ assert_equal 'testing again', H.build_query(['testing', 'again'])
17
+ assert_equal '"testing" "again"', H.build_query(['testing', 'again'], :quote=>true)
18
+ assert_equal 'name:whatever', H.build_query({:name=>'whatever'})
19
+ assert_equal 'name:"whatever"', H.build_query({:name=>'whatever'}, :quote=>true)
20
+ assert_equal 'sam name:whatever i am', H.build_query(['sam', {:name=>'whatever'}, 'i', 'am'])
21
+ assert_equal 'testing AND blah', H.build_query(['testing', 'blah'], :join=>' AND ')
22
+ end
23
+
24
+ test 'start_for' do
25
+ per_page = 8
26
+ current_page = 2
27
+ assert_equal 8, H.start_for(current_page, per_page)
28
+ end
29
+
30
+ end
data/test/test_helpers.rb CHANGED
@@ -2,6 +2,24 @@ $: << File.dirname(__FILE__)
2
2
  require File.join(File.dirname(__FILE__), '..', 'lib', 'rsolr')
3
3
  require 'test/unit'
4
4
 
5
+ #
6
+ class Test::Unit::TestCase
7
+
8
+ def self.test(name, &block)
9
+ test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
10
+ defined = instance_method(test_name) rescue false
11
+ raise "#{test_name} is already defined in #{self}" if defined
12
+ if block_given?
13
+ define_method(test_name, &block)
14
+ else
15
+ define_method(test_name) do
16
+ flunk "No implementation provided for #{name}"
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+
5
23
  class RSolrBaseTest < Test::Unit::TestCase
6
24
 
7
25
  def assert_class(expected, instance)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mwmitchell-rsolr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Mitchell
@@ -56,6 +56,7 @@ files:
56
56
  - lib/rsolr/response/query.rb
57
57
  - lib/rsolr/response/update.rb
58
58
  - lib/rsolr/response.rb
59
+ - lib/rsolr/query.rb
59
60
  - LICENSE
60
61
  - Rakefile
61
62
  - README.rdoc
@@ -97,6 +98,7 @@ test_files:
97
98
  - test/http_client/util_test.rb
98
99
  - test/indexer.rb
99
100
  - test/message_test.rb
101
+ - test/query_helper_test.rb
100
102
  - test/response/base_test.rb
101
103
  - test/response/pagination_test.rb
102
104
  - test/response/query_test.rb