class Lolita::Search::Simple
Default search class for Lolita::Search
. Lolita::Configuration::Search
uses this by default. It accepts method name as constructor argument, when none is given it call Lolita::DBI#search.
Attributes
dbi[R]
search_method[RW]
Method in model used to run a search.
Public Class Methods
new(dbi, *args)
click to toggle source
Accepts search method as optional argument
# File lib/lolita/search/simple.rb, line 13 def initialize(dbi, *args) @dbi = dbi @options = args.extract_options! @search_method = args[0] end
Public Instance Methods
run(query,*args)
click to toggle source
Require dbi (Lolita::DBI
instance), query (String) and request and dbi as optional argument. Also you can pass options.
Example¶ ↑
search.run("query",:fields => [:name]) # this will search only in :name field search.run("query",nil, Lolita::DBI::Base.create(Category)) # this will use Category dbi for search
When there is search method defined, it uses that otherwise run default search.
# File lib/lolita/search/simple.rb, line 27 def run(query,*args) with_query(query,*args) do if self.search_method run_custom_search else run_default_search end end end
Private Instance Methods
run_custom_search()
click to toggle source
# File lib/lolita/search/simple.rb, line 56 def run_custom_search search_method_arity = @dbi.klass.method(self.search_method).arity args = [@query,@request,@options] if search_method_arity < 0 @dbi.klass.send(self.search_method.to_sym,@query,@request,@options) elsif search_method_arity == 0 raise ArgumentError, "#{@dbi.klass.to_s} method #{search_method} must accept at least 1 argument." else arity_limit = search_method_arity > args.size ? args.size : search_method_arity @dbi.klass.send(self.search_method.to_sym,*(args.slice(0..(arity_limit-1)))) end end
run_default_search()
click to toggle source
# File lib/lolita/search/simple.rb, line 69 def run_default_search @dbi.search(@query,@options || {}) end
with_query(query,*args) { || ... }
click to toggle source
# File lib/lolita/search/simple.rb, line 39 def with_query(query,*args) begin options = args.extract_options! @old_dbi = self.dbi @old_options = @options @options = options if options.any? @dbi = args[1] if args[1] @query = query @request = args[0] yield ensure @dbi = @old_dbi @options = @old_options @query,@request = nil,nil end end