Class: Lunr::Search
- Inherits:
-
Object
show all
- Includes:
- Enumerable
- Defined in:
- lib/lunr/search.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(klass, &block) ⇒ Search
Returns a new instance of Search.
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/lunr/search.rb', line 11
def initialize klass, &block
raise Lunr::BadModel.new(klass) unless klass < Lunr::Model
@executed = false
@klass = klass
@search = Sunspot.new_search klass
all = @klass.scopes[:all]
scope(&all) if all
scope(&block) if block_given?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/lunr/search.rb', line 44
def method_missing name, *args
return super unless scope = klass.scopes[name]
executable!
dsl = @search.send :dsl
if args.empty?
dsl.instance_eval(&scope)
else
scope.call dsl, args
end
self
end
|
Instance Attribute Details
#klass ⇒ Object
Returns the value of attribute klass.
9
10
11
|
# File 'lib/lunr/search.rb', line 9
def klass
@klass
end
|
Instance Method Details
#as_json(options = nil) ⇒ Object
24
25
26
|
# File 'lib/lunr/search.rb', line 24
def as_json options = nil
results.map { |r| r.as_json options }
end
|
#each(&block) ⇒ Object
28
29
30
|
# File 'lib/lunr/search.rb', line 28
def each &block
execute && @results.each(&block)
end
|
#empty? ⇒ Boolean
32
33
34
|
# File 'lib/lunr/search.rb', line 32
def empty?
0 == total
end
|
#executable! ⇒ Object
36
37
38
|
# File 'lib/lunr/search.rb', line 36
def executable!
raise Lunr::AlreadyExecuted.new(self) if executed?
end
|
#executed? ⇒ Boolean
40
41
42
|
# File 'lib/lunr/search.rb', line 40
def executed?
@executed
end
|
#page ⇒ Object
Also known as:
current_page
60
61
62
|
# File 'lib/lunr/search.rb', line 60
def page
@page ||= execute && @search.query.page
end
|
#pages ⇒ Object
Also known as:
total_pages
64
65
66
67
|
# File 'lib/lunr/search.rb', line 64
def pages
@pages ||= total / per +
((total_entries % per_page) > 0 ? 1 : 0)
end
|
#params ⇒ Object
69
70
71
|
# File 'lib/lunr/search.rb', line 69
def params
@search.query.to_params
end
|
#per ⇒ Object
Also known as:
per_page
73
74
75
|
# File 'lib/lunr/search.rb', line 73
def per
@per ||= execute && @search.query.per_page
end
|
#respond_to(name, include_private = false) ⇒ Object
77
78
79
|
# File 'lib/lunr/search.rb', line 77
def respond_to name, include_private = false
klass.scopes.key?(name) || super
end
|
#results ⇒ Object
81
82
83
|
# File 'lib/lunr/search.rb', line 81
def results
execute && @results
end
|
#scope(&block) ⇒ Object
85
86
87
88
89
90
|
# File 'lib/lunr/search.rb', line 85
def scope &block
executable!
@search.build(&block)
self
end
|
#total ⇒ Object
Also known as:
size, total_entries
92
93
94
|
# File 'lib/lunr/search.rb', line 92
def total
@total ||= execute && @search.total
end
|