Class: XapianDb::Adapters::BaseAdapter
- Inherits:
-
Object
- Object
- XapianDb::Adapters::BaseAdapter
- Defined in:
- lib/xapian_db/adapters/base_adapter.rb
Overview
base class for all adapters. This adapter does the following:
-
adds the class method
search(expression)to an indexed class
Direct Known Subclasses
Class Method Summary collapse
-
.add_class_helper_methods_to(klass) ⇒ Object
Implement the class helper methods.
Class Method Details
.add_class_helper_methods_to(klass) ⇒ Object
Implement the class helper methods
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/xapian_db/adapters/base_adapter.rb', line 17 def add_class_helper_methods_to(klass) klass.class_eval do # Add a method to search models of this class # Options: # - :order (Array<Symbol>) Accepts an array of attribute names for sorting # - :sort_decending (Boolean) Allows to reverse the sorting define_singleton_method(:search) do |expression, ={}| # return an empty search if no search expression is given return XapianDb.database.search(nil) if expression.nil? || expression.strip.empty? = {:sort_decending => false}.merge class_scope = "indexed_class:#{klass.name.downcase}" order = .delete :order if order attr_names = [order].flatten undefined_attrs = attr_names - XapianDb::DocumentBlueprint.attributes raise ArgumentError.new "invalid order clause: attributes #{undefined_attrs.inspect} are not defined" unless undefined_attrs.empty? [:sort_indices] = attr_names.map {|attr_name| XapianDb::DocumentBlueprint.value_number_for(attr_name) } end result = XapianDb.database.search "#{class_scope} AND (#{expression})", # Remove the class scope from the spelling suggestion (if any) if result.spelling_suggestion scope_length = "#{class_scope} AND (".size result.spelling_suggestion = result.spelling_suggestion.slice scope_length..-2 end result end define_singleton_method(:find_similar_to) do |reference| return XapianDb.database.find_similar_to reference, :class => klass end # Add a method to search atribute facets of this class define_singleton_method(:facets) do |attribute, expression| class_scope = "indexed_class:#{klass.name.downcase}" XapianDb.database.facets attribute, "#{class_scope} and (#{expression})" end end end |