Skip to content

Commit

Permalink
[REFACTOR] Changed that the Search::Search#perform method is lazy loa…
Browse files Browse the repository at this point in the history
…ded (allows chainability)

DSL#search and Search::Search lazy load data and are chainable, until #results or #response called.

This allows something like this:

    s = Tire.search('articles') { query { string 'title:T*' } }
    s.filter :terms, :tags => ['ruby']
    p s.results
  • Loading branch information
greglearns authored and karmi committed Jan 7, 2012
1 parent 89bc663 commit 9196141
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/tire/dsl.rb
Expand Up @@ -7,7 +7,7 @@ def configure(&block)

def search(indices=nil, options={}, &block)
if block_given?
Search::Search.new(indices, options, &block).perform
Search::Search.new(indices, options, &block)
else
payload = case options
when Hash then options.to_json
Expand Down
6 changes: 3 additions & 3 deletions lib/tire/model/persistence/finders.rb
Expand Up @@ -21,7 +21,7 @@ def find *args
query.ids(args, document_type)
end
search.size args.size
end.perform.results
end.results
else
case args = args.pop
when Fixnum, String
Expand All @@ -41,7 +41,7 @@ def all
old_wrapper = Tire::Configuration.wrapper
Tire::Configuration.wrapper self
s = Tire::Search::Search.new(index.name).query { all }
s.perform.results
s.results
ensure
Tire::Configuration.wrapper old_wrapper
end
Expand All @@ -51,7 +51,7 @@ def first
old_wrapper = Tire::Configuration.wrapper
Tire::Configuration.wrapper self
s = Tire::Search::Search.new(index.name).query { all }.size(1)
s.perform.results.first
s.results.first
ensure
Tire::Configuration.wrapper old_wrapper
end
Expand Down
2 changes: 1 addition & 1 deletion lib/tire/model/search.rb
Expand Up @@ -94,7 +94,7 @@ def search(*args, &block)
s.fields Array(options[:fields]) if options[:fields]
end

s.perform.results
s.results
end

# Returns a Tire::Index instance for this model.
Expand Down
10 changes: 9 additions & 1 deletion lib/tire/search.rb
Expand Up @@ -4,7 +4,7 @@ class SearchRequestFailed < StandardError; end

class Search

attr_reader :indices, :results, :response, :json, :query, :facets, :filters, :options
attr_reader :indices, :json, :query, :facets, :filters, :options

def initialize(indices=nil, options = {}, &block)
@indices = Array(indices)
Expand All @@ -16,6 +16,14 @@ def initialize(indices=nil, options = {}, &block)
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
end

def results
@results || (perform; @results)
end

def response
@response || (perform; @response)
end

def url
Configuration.url + @path
end
Expand Down
35 changes: 35 additions & 0 deletions test/unit/tire_test.rb
Expand Up @@ -53,6 +53,41 @@ class TireTest < Test::Unit::TestCase
end
end

context "#search is lazy loaded and" do
def a_query
Tire.search 'dummy' do
query do
string "dummy"
end
end
end

should "not be called immediately" do
s = a_query
s.expects(:perform).never
end

should "be called by #perform" do
s = a_query
s.expects(:perform).once
s.perform
end

should "be called by #results" do
s = a_query
s.expects(:perform).once
s.results
end

should "allow search criteria to be chained" do
s = a_query
s.expects(:perform).once
s.filter :term, other_field: 'another dummy'
s.results
end

end

end

end
Expand Down

0 comments on commit 9196141

Please sign in to comment.