mwmitchell-rsolr-ext 0.5.6 → 0.5.7

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.
@@ -15,11 +15,11 @@ module RSolr::Ext::Request
15
15
  :phrase_filters, # quoted fq params,
16
16
  :facets
17
17
  ]
18
-
18
+
19
19
  def map_per_page(value,output)
20
20
  output[:rows] = value.to_i
21
21
  end
22
-
22
+
23
23
  def map_page(value,output)
24
24
  raise ':per_page must be set when using :page' unless output[:rows]
25
25
  page = value.to_s.to_i-1
@@ -6,16 +6,12 @@ module RSolr::Ext::Response
6
6
 
7
7
  class Base < Mash
8
8
 
9
- attr_reader :raw_response
10
-
11
- def initialize(raw_response)
12
- @raw_response = raw_response
13
- super(raw_response)
14
- RSolr::Ext::HashMethodizer.methodize!(self)
9
+ def header
10
+ self[:responseHeader]
15
11
  end
16
12
 
17
13
  def ok?
18
- response_header.status == 0
14
+ header[:status] == 0
19
15
  end
20
16
 
21
17
  end
@@ -26,17 +22,27 @@ module RSolr::Ext::Response
26
22
  include Facetable
27
23
 
28
24
  def initialize(*a)
29
- super
25
+ super(*a)
30
26
  activate_pagination!
31
27
  end
32
28
 
29
+ def response
30
+ self[:response]
31
+ end
32
+
33
+ def docs
34
+ response[:docs]
35
+ end
36
+
37
+ private
38
+
33
39
  def activate_pagination!
34
- response.docs.each{ |d| d.extend DocExt }
35
- d = response.docs
40
+ d = self[:response][:docs]
41
+ d.each{|dhash| dhash.extend DocExt }
36
42
  d.extend Pageable
37
- d.start = response_header.params[:start].to_s.to_i
38
- d.per_page = response_header.params[:rows].to_s.to_i
39
- d.total = response.num_found
43
+ d.start = self[:responseHeader][:params][:start].to_s.to_i
44
+ d.per_page = self[:responseHeader][:params][:rows].to_s.to_i
45
+ d.total = self[:response][:numFound]
40
46
  end
41
47
 
42
48
  end
@@ -46,13 +52,13 @@ module RSolr::Ext::Response
46
52
  end
47
53
 
48
54
  #
49
- class RSolr::Ext::Response::Luke < Base
55
+ class Luke < Base
50
56
 
51
57
  # Returns an array of fields from the index
52
58
  # An optional rule can be used for "grepping" field names:
53
59
  # field_list(/_facet$/)
54
60
  def field_list(rule=nil)
55
- fetch(:fields).select do |k,v|
61
+ self[:fields].select do |k,v|
56
62
  rule ? k =~ rule : true
57
63
  end.collect{|k,v|k}
58
64
  end
data/lib/rsolr-ext.rb CHANGED
@@ -15,11 +15,10 @@ module RSolr
15
15
 
16
16
  module Ext
17
17
 
18
- VERSION = '0.5.6'
18
+ VERSION = '0.5.7'
19
19
 
20
20
  autoload :Request, 'rsolr-ext/request.rb'
21
21
  autoload :Response, 'rsolr-ext/response.rb'
22
- autoload :HashMethodizer, 'rsolr-ext/hash_methodizer.rb'
23
22
  autoload :Mapable, 'rsolr-ext/mapable.rb'
24
23
 
25
24
  end
data/rsolr-ext.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "rsolr-ext"
3
- s.version = "0.5.6"
4
- s.date = "2009-03-17"
3
+ s.version = "0.5.7"
4
+ s.date = "2009-03-18"
5
5
  s.summary = "An extension lib for RSolr"
6
6
  s.email = "goodieboy@gmail.com"
7
7
  s.homepage = "http://github.com/mwmitchell/rsolr_ext"
@@ -13,7 +13,6 @@ Gem::Specification.new do |s|
13
13
  "lib/core_ext.rb",
14
14
  "lib/mash.rb",
15
15
 
16
- "lib/rsolr-ext/hash_methodizer.rb",
17
16
  "lib/rsolr-ext/mapable.rb",
18
17
 
19
18
  "lib/rsolr-ext/request/queryable.rb",
@@ -32,7 +31,6 @@ Gem::Specification.new do |s|
32
31
  "rsolr-ext.gemspec"
33
32
  ]
34
33
  s.test_files = [
35
- 'test/hash_methodizer_test.rb',
36
34
  'test/request_test.rb',
37
35
  'test/response_test.rb',
38
36
  'test/test_unit_test_case.rb',
@@ -7,22 +7,35 @@ class RSolrExtResponseTest < Test::Unit::TestCase
7
7
  test 'base response class' do
8
8
  raw_response = eval(mock_query_response)
9
9
  r = RSolr::Ext::Response::Base.new(raw_response)
10
+ assert r.respond_to?(:header)
10
11
  assert r.ok?
11
12
  end
12
13
 
13
14
  test 'standard response class' do
14
15
  raw_response = eval(mock_query_response)
15
16
  r = RSolr::Ext::Response::Standard.new(raw_response)
17
+ assert r.respond_to?(:response)
16
18
  assert r.ok?
17
- assert_equal 10, r.response.docs.size
18
- assert_equal 'EXPLICIT', r.response_header.params.echo_params
19
- assert_equal r['responseHeader'], r.response_header
20
- assert_equal r[:responseHeader], r.response_header
21
- assert_equal 1, r.response.docs.previous_page
22
- assert_equal 1, r.response.docs.next_page
19
+ assert_equal 10, r[:response][:docs].size
20
+ assert_equal 'EXPLICIT', r[:responseHeader][:params][:echoParams]
21
+ assert_equal 1, r[:response][:docs].previous_page
22
+ assert_equal 1, r[:response][:docs].next_page
23
23
  #
24
- assert r.response.docs.kind_of?(RSolr::Ext::Response::Pageable)
24
+ assert r[:response][:docs].kind_of?(RSolr::Ext::Response::Pageable)
25
25
  assert r.kind_of?(RSolr::Ext::Response::Facetable)
26
26
  end
27
27
 
28
+ test 'standard response doc ext methods' do
29
+ raw_response = eval(mock_query_response)
30
+ r = RSolr::Ext::Response::Standard.new(raw_response)
31
+ doc = r.docs.first
32
+ assert doc.has?(:cat, /^elec/)
33
+ assert ! doc.has?(:cat, 'elec')
34
+ assert doc.has?(:cat, 'electronics')
35
+
36
+ assert 'electronics', doc.get(:cat)
37
+ assert_nil doc.get(:xyz)
38
+ assert_equal 'def', doc.get(:xyz, :default=>'def')
39
+ end
40
+
28
41
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mwmitchell-rsolr-ext
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.6
4
+ version: 0.5.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Mitchell
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-17 00:00:00 -07:00
12
+ date: 2009-03-18 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -25,7 +25,6 @@ extra_rdoc_files:
25
25
  files:
26
26
  - lib/core_ext.rb
27
27
  - lib/mash.rb
28
- - lib/rsolr-ext/hash_methodizer.rb
29
28
  - lib/rsolr-ext/mapable.rb
30
29
  - lib/rsolr-ext/request/queryable.rb
31
30
  - lib/rsolr-ext/request.rb
@@ -65,7 +64,6 @@ signing_key:
65
64
  specification_version: 2
66
65
  summary: An extension lib for RSolr
67
66
  test_files:
68
- - test/hash_methodizer_test.rb
69
67
  - test/request_test.rb
70
68
  - test/response_test.rb
71
69
  - test/test_unit_test_case.rb
@@ -1,51 +0,0 @@
1
- #
2
- # A hash modifier that creates method readers from key names.
3
- # NOTE: reader methods are created recursively.
4
- # The method names are the same as the key names,
5
- # except that the values are snake-cased, for example:
6
- # - QTime -> q_time
7
- # - debugQuery -> debug_query
8
- #
9
- class RSolr::Ext::HashMethodizer
10
-
11
- class << self
12
-
13
- def snake_case(v)
14
- v = v.to_s
15
- return v.downcase if v =~ /^[A-Z]+$/
16
- v.gsub(/([A-Z]+)(?=[A-Z][a-z]?)|\B[A-Z]/, '_\&') =~ /_*(.*)/
17
- return $+.downcase
18
- end
19
-
20
- def methodize!(h)
21
- h.keys.each do |k|
22
- meth = snake_case(k)
23
- meth = meth.gsub(/[^_a-z]/i, '_').gsub(/^_+/i, '')
24
- val_key = case k
25
- when String
26
- "'#{k}'"
27
- when Symbol
28
- ":#{k}"
29
- else
30
- raise 'Supports only string/symbol keys!'
31
- end
32
- h.instance_eval <<-RUBY
33
- def #{meth}
34
- val = self[#{val_key}]
35
- if val.respond_to?(:each_pair)
36
- RSolr::Ext::HashMethodizer.methodize!(val)
37
- elsif val.is_a?(Array)
38
- val.each do |item|
39
- RSolr::Ext::HashMethodizer.methodize!(item) if item.respond_to?(:each_pair)
40
- end
41
- end
42
- val
43
- end
44
- RUBY
45
- end
46
-
47
- end
48
-
49
- end
50
-
51
- end
@@ -1,55 +0,0 @@
1
- require 'test_unit_test_case'
2
- require File.join(File.dirname(__FILE__), '..', 'lib', 'rsolr-ext')
3
-
4
- class RSolrExtRequestTest < Test::Unit::TestCase
5
-
6
- def testable_hash
7
- {';key-with-invalid-method-chars'=>1, :name=>'Sam', :age=>90, :kids=>['Fred', 'Betty'], :favorites=>{:red=>1, :blue=>9}}
8
- end
9
-
10
- test 'the default behavior of a hash, after methodize!' do
11
- my_hash = testable_hash
12
-
13
- original_methods = my_hash.methods
14
- key_count = my_hash.keys.size
15
-
16
- RSolr::Ext::HashMethodizer.methodize!(my_hash)
17
-
18
- assert_equal key_count, my_hash.keys.size
19
-
20
- [:name, :age, :kids, :favorites].each do |k|
21
- assert my_hash.keys.include?(k)
22
- end
23
-
24
- assert_equal 1, my_hash[:favorites][:red]
25
- assert_equal 9, my_hash[:favorites][:blue]
26
-
27
- assert_equal Hash, my_hash.class
28
-
29
- # make sure that the difference in method size is the size of the keys in my_hash
30
- assert_equal my_hash.methods.size - key_count, original_methods.size
31
-
32
- assert my_hash.respond_to?(:key_with_invalid_method_chars)
33
- end
34
-
35
- test 'the method accessors on a modified hash' do
36
-
37
- my_hash = testable_hash
38
-
39
- assert_raise NoMethodError do
40
- my_hash.favorites
41
- end
42
-
43
- RSolr::Ext::HashMethodizer.methodize!(my_hash)
44
-
45
- assert_equal my_hash[:name], my_hash.name
46
- assert_equal my_hash[:age], my_hash.age
47
- assert_equal my_hash[:kids], my_hash.kids
48
- assert_equal my_hash[:favorites], my_hash.favorites
49
-
50
- assert_equal my_hash[:favorites][:blue], my_hash.favorites.blue
51
- assert_equal my_hash[:favorites][:red], my_hash.favorites.red
52
-
53
- end
54
-
55
- end