xapian_db 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ##0.4.1 (December 16th, 2010)
2
+
3
+ Bugfixes:
4
+
5
+ - fixed the handling of invalid page arguments in resultset.paginate. Invalid page arguments return
6
+ an empty result set
7
+ - searches with an empty search expression do not raise an exception anymore and return an empty
8
+ resultset
9
+
1
10
  ##0.4.0 (December 15th, 2010)
2
11
 
3
12
  Features:
@@ -23,6 +23,10 @@ module XapianDb
23
23
  # - :order (Array<Symbol>) Accepts an array of attribute names for sorting
24
24
  # - :sort_decending (Boolean) Allows to reverse the sorting
25
25
  define_singleton_method(:search) do |expression, options={}|
26
+
27
+ # return an empty search if no search expression is given
28
+ return XapianDb.database.search(nil) if expression.nil? || expression.strip.empty?
29
+
26
30
  options = {:sort_decending => false}.merge options
27
31
  class_scope = "indexed_class:#{klass.name.downcase}"
28
32
 
@@ -61,9 +61,12 @@ module XapianDb
61
61
  opts = {:per_page => 10, :sort_decending => false}.merge(options)
62
62
  @query_parser ||= QueryParser.new(self)
63
63
  query = @query_parser.parse(expression)
64
+
65
+ # If we do not have a valid query we return an empty result set
66
+ return Resultset.new(nil, opts) unless query
67
+
64
68
  enquiry = Xapian::Enquire.new(reader)
65
69
  enquiry.query = query
66
-
67
70
  if opts[:sort_indices]
68
71
  raise ArgumentError.new("Sorting is available for class scoped searches only") unless expression =~ /^indexed_class:/
69
72
  sorter = Xapian::MultiValueSorter.new
@@ -26,6 +26,7 @@ module XapianDb
26
26
  # Parse an expression
27
27
  # @return [Xapian::Query] The query object (see http://xapian.org/docs/apidoc/html/classXapian_1_1Query.html)
28
28
  def parse(expression)
29
+ return nil if expression.nil? || expression.strip.empty?
29
30
  parser = Xapian::QueryParser.new
30
31
  parser.database = @db.reader
31
32
  parser.default_op = Xapian::Query::OP_AND # Could be made configurable
@@ -31,26 +31,31 @@ module XapianDb
31
31
  attr_accessor :spelling_suggestion
32
32
 
33
33
  # Constructor
34
- # @param [Xapian::Enquire] enquiry a Xapian query result (see http://xapian.org/docs/apidoc/html/classXapian_1_1Enquire.html)
34
+ # @param [Xapian::Enquire] enquiry a Xapian query result (see http://xapian.org/docs/apidoc/html/classXapian_1_1Enquire.html).
35
+ # Pass nil to get an empty result set.
35
36
  # @param [Hash] options
36
37
  # @option options [Integer] :per_page (10) How many docs per page?
37
38
  # @option options [String] :spelling_suggestion (nil) The spelling corrected query (if a language is configured)
38
39
  def initialize(enquiry, options)
39
40
  @enquiry = enquiry
40
- # To get more accurate results, we pass the doc count to the mset method
41
- @size = enquiry.mset(0, options[:db_size]).matches_estimated
41
+ if @enquiry.nil?
42
+ @size = 0
43
+ else
44
+ # To get more accurate results, we pass the doc count to the mset method
45
+ @size = enquiry.mset(0, options[:db_size]).matches_estimated
46
+ end
42
47
  @spelling_suggestion = options[:spelling_suggestion]
43
48
  @per_page = options[:per_page]
44
- @total_pages = (@size / @per_page.to_f).ceil
49
+ @total_pages = @size == 0 ? 0 : (@size / @per_page.to_f).ceil
45
50
  @current_page = 1
46
51
  end
47
52
 
48
53
  # Paginate the result
49
- # @param [Hash] opts Options for the persistent database
50
- # @option opts [Integer] :page (1) The page to access
51
- def paginate(opts={})
52
- options = {:page => 1}.merge(opts)
53
- @current_page = options[:page].to_i
54
+ # @param [Hash] options Options for the persistent database
55
+ # @option options [Integer] :page (1) The page to access
56
+ def paginate(options={})
57
+ @current_page = options[:page] ? options[:page].to_i : 1
58
+ return [] if @current_page < 1 || @current_page > @total_pages
54
59
  build_page(@current_page)
55
60
  end
56
61
 
@@ -71,7 +76,6 @@ module XapianDb
71
76
  # Build a page of Xapian documents
72
77
  # @return [Array<Xapian::Document>] An array of xapian documents
73
78
  def build_page(page)
74
- page.nil? ? page = 1 : page = page.to_i
75
79
  docs = []
76
80
  offset = (page - 1) * @per_page
77
81
  return [] if offset > @size
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 0
9
- version: 0.4.0
8
+ - 1
9
+ version: 0.4.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Gernot Kogler
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-15 00:00:00 +01:00
17
+ date: 2010-12-16 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency