mwmitchell-rsolr-ext 0.7.1 → 0.7.2

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/README.rdoc CHANGED
@@ -59,4 +59,20 @@ The #find method provides a convenient way to search solr. Here are some example
59
59
  books_about_jefferson = solr.find 'jefferson', :phrase_filters=>{:type=>'book'}
60
60
 
61
61
  # q=something -- the entire response
62
- solr_response = solr.find {:q=>'something'}, :include_response=>true
62
+ solr_response = solr.find {:q=>'something'}, :include_response=>true
63
+
64
+ ===The Doc Module
65
+ You can create your own "models" using RSolr::Ext::Doc
66
+ class Book
67
+ include RSolr::Ext::Doc
68
+ @default_params = {:fq=>'object_type:"book"', :rows=>10}
69
+
70
+ def self.find_by_author(author)
71
+ find(:phrase_filters=>{:author=>author})
72
+ end
73
+ end
74
+
75
+ all_books = Book.find('*:*')
76
+ hawk_books = Book.find_by_author('hawk')
77
+
78
+ If you wanna paginate, just throw the collection into the WillPaginate view helper.
@@ -0,0 +1,93 @@
1
+ # include this module into a plain ruby class:
2
+ # class Book
3
+ # include RSolr::Ext::Doc
4
+ # connection = RSolr::Ext.connect
5
+ # default_params = {:phrase_filters=>'type:book'}
6
+ # end
7
+ #
8
+ # Then:
9
+ # number_10 = Book.find_by_id(10)
10
+ #
11
+ module RSolr::Ext::Doc
12
+
13
+ # Class level methods for altering object instances
14
+ module Callbacks
15
+
16
+ # creates the @hooks container ("hooks" are blocks or procs).
17
+ # returns an array
18
+ def hooks
19
+ @hooks ||= []
20
+ end
21
+
22
+ # method that only accepts a block
23
+ # The block is executed when an object is created via #new -> SolrDoc.new
24
+ # The blocks scope is the instance of the object.
25
+ def after_initialize(&blk)
26
+ hooks << blk
27
+ end
28
+
29
+ # Removes the current set of after_initialize blocks.
30
+ # You would use this if you wanted to open a class back up,
31
+ # but clear out the previously defined blocks.
32
+ def clear_after_initialize_blocks!
33
+ @hooks = []
34
+ end
35
+
36
+ end
37
+
38
+ #
39
+ # Findable is a module that gets mixed into the SolrDocument class object.
40
+ # These methods will be available through the class like: SolrDocument.find and SolrDocument.find_by_id
41
+ #
42
+ module Findable
43
+
44
+ attr_accessor :connection
45
+
46
+ def connection
47
+ @connection ||= RSolr::Ext.connect
48
+ end
49
+
50
+ def find(*args)
51
+ mode, solr_params, opts = connection.send(:extract_find_opts!, *args)
52
+ connection.find(*[mode, solr_params, opts]) { |doc| self.new(doc) }
53
+ end
54
+
55
+ def find_by_id(id, solr_params={}, opts={})
56
+ connection.find_by_id(id, solr_params, opts) { |doc| self.new(doc) }
57
+ end
58
+
59
+ end
60
+
61
+ # Called by Ruby Module API
62
+ # extends this *class* object
63
+ def self.included(base)
64
+ base.extend Callbacks
65
+ base.extend Findable
66
+ end
67
+
68
+ # The original object passed in to the #new method
69
+ attr :_source_hash
70
+
71
+ # Constructor **for the class that is getting this module included**
72
+ # source_doc should be a hash or something similar
73
+ # calls each of after_initialize blocks
74
+ def initialize(source_doc={})
75
+ @_source_hash = source_doc
76
+ self.class.hooks.each do |h|
77
+ instance_eval &h
78
+ end
79
+ end
80
+
81
+ # for easy access to the solr id (route helpers etc..)
82
+ def id
83
+ @_source_hash['id']
84
+ end
85
+
86
+ # the wrapper method to the @_source_hash object.
87
+ # If a method is missing, it gets sent to @_source_hash
88
+ # with all of the original params and block
89
+ def method_missing(m, *args, &b)
90
+ @_source_hash.send(m, *args, &b)
91
+ end
92
+
93
+ end
@@ -34,11 +34,7 @@ module RSolr::Ext::Findable
34
34
  def find(*args, &blk)
35
35
  mode, solr_params, opts = extract_find_opts!(*args)
36
36
 
37
- opts[:include_response] = false unless opts.key?(:include_response)
38
-
39
- if solr_params.is_a?(String)
40
- solr_params = {:q=>solr_params}
41
- end
37
+ opts[:include_response] ||= true
42
38
 
43
39
  solr_params[:rows] = 1 if mode == :first
44
40
  valid_solr_params = RSolr::Ext.map_params(solr_params)
@@ -68,9 +64,14 @@ module RSolr::Ext::Findable
68
64
  end
69
65
 
70
66
  # find_by_id(10, :handler=>'catalog')
67
+ # find_by_id(:id=>10)
71
68
  def find_by_id(id, solr_params={}, opts={}, &blk)
72
- solr_params[:phrases] ||= {}
73
- solr_params[:phrases][:id] = id
69
+ if id.respond_to?(:each_pair)
70
+ solr_params = id
71
+ else
72
+ solr_params[:phrases] ||= {}
73
+ solr_params[:phrases][:id] = id.to_s
74
+ end
74
75
  self.find(:first, solr_params, opts, &blk)
75
76
  end
76
77
 
@@ -78,13 +79,15 @@ module RSolr::Ext::Findable
78
79
 
79
80
  def extract_find_opts!(*args)
80
81
  mode = :all
81
- # extract the mode (:all or :first)
82
82
  valid_modes = [:all, :first]
83
83
  if args[0].is_a?(Symbol)
84
84
  mode = valid_modes.include?(args[0]) ? args.shift : raise("Invalid find mode; should be :first or :all")
85
85
  end
86
86
  # extract solr params
87
- solr_params = args.shift || {}
87
+ solr_params = args.shift
88
+ unless solr_params.respond_to?(:each_pair)
89
+ solr_params = {:q=>solr_params.to_s}
90
+ end
88
91
  # extract options
89
92
  opts = args.shift || {}
90
93
  [mode, solr_params, opts]
@@ -32,6 +32,11 @@ module RSolr::Ext::Request::Queryable
32
32
  return value.collect do |(k,v)|
33
33
  if v.is_a?(Range)
34
34
  "#{k}:#{build_range(v)}"
35
+ # If the value is an array, we want the same param, multiple times (not a query join)
36
+ elsif v.is_a?(Array)
37
+ v.collect do |vv|
38
+ "#{k}:#{build_query(vv, quote_string)}"
39
+ end
35
40
  else
36
41
  "#{k}:#{build_query(v, quote_string)}"
37
42
  end
@@ -84,9 +84,9 @@ module RSolr::Ext::Response::Docs
84
84
  d = base['response']['docs']
85
85
  d.extend Pageable
86
86
  d.each{|item|item.extend Accessable}
87
- d.start = base['responseHeader']['params']['start'].to_s.to_i
88
87
  d.per_page = base['responseHeader']['params']['rows'].to_s.to_i
89
- d.total = base['responseHeader']['numFound'].to_s.to_i
88
+ d.start = base['response']['start'].to_s.to_i
89
+ d.total = base['response']['numFound'].to_s.to_i
90
90
  end
91
91
 
92
92
  def docs
@@ -24,8 +24,8 @@ module RSolr::Ext::Response
24
24
 
25
25
  def initialize(*args)
26
26
  super(*args)
27
- extend Docs if self['response']['docs']
28
- extend Facets if key?('facet_counts')
27
+ extend Docs
28
+ extend Facets
29
29
  end
30
30
 
31
31
  def response
data/lib/rsolr-ext.rb CHANGED
@@ -23,12 +23,13 @@ module RSolr
23
23
 
24
24
  module Ext
25
25
 
26
- VERSION = '0.7.1'
26
+ VERSION = '0.7.2'
27
27
 
28
28
  autoload :Request, 'rsolr-ext/request.rb'
29
29
  autoload :Response, 'rsolr-ext/response.rb'
30
30
  autoload :Mapable, 'rsolr-ext/mapable.rb'
31
31
  autoload :Findable, 'rsolr-ext/findable.rb'
32
+ autoload :Doc, 'rsolr-ext/doc.rb'
32
33
 
33
34
  # RSolr::Ext.map_params({})
34
35
  def self.map_params(r)
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.7.1"
4
- s.date = "2009-05-04"
3
+ s.version = "0.7.2"
4
+ s.date = "2009-05-17"
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"
@@ -12,6 +12,8 @@ Gem::Specification.new do |s|
12
12
 
13
13
  "lib/mash.rb",
14
14
 
15
+ "lib/rsolr-ext/doc.rb",
16
+
15
17
  "lib/rsolr-ext/findable.rb",
16
18
 
17
19
  "lib/rsolr-ext/mapable.rb",
data/test/request_test.rb CHANGED
@@ -25,10 +25,10 @@ class RSolrExtRequestTest < Test::Unit::TestCase
25
25
  test 'fq param using the phrase_filters mapping' do
26
26
  std = RSolr::Ext::Request::Standard.new
27
27
  solr_params = std.map(
28
- :phrase_filters=>[{:manu=>'Apple'}, {:color=>'red'}]
28
+ :phrase_filters=>{:manu=>['Apple', 'ASG'], :color=>['red', 'blue']}
29
29
  )
30
- assert solr_params[:fq].is_a?(Array)
31
- assert solr_params[:fq].first.is_a?(String)
30
+ expected = {:fq=>["color:\"red\"", "color:\"blue\"", "manu:\"Apple\"", "manu:\"ASG\""]}
31
+ assert expected, solr_params
32
32
  end
33
33
 
34
34
  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.7.1
4
+ version: 0.7.2
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-05-04 00:00:00 -07:00
12
+ date: 2009-05-17 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,6 +24,7 @@ extra_rdoc_files:
24
24
  - README.rdoc
25
25
  files:
26
26
  - lib/mash.rb
27
+ - lib/rsolr-ext/doc.rb
27
28
  - lib/rsolr-ext/findable.rb
28
29
  - lib/rsolr-ext/mapable.rb
29
30
  - lib/rsolr-ext/request/queryable.rb