Class: Lunr::Search

Inherits:
Object
  • 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.

Raises:



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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lunr/search.rb', line 40

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

#klassObject (readonly)

Returns the value of attribute klass.



9
10
11
# File 'lib/lunr/search.rb', line 9

def klass
  @klass
end

Instance Method Details

#each(&block) ⇒ Object



24
25
26
# File 'lib/lunr/search.rb', line 24

def each &block
  execute && @results.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/lunr/search.rb', line 28

def empty?
  0 == total
end

#executable!Object



32
33
34
# File 'lib/lunr/search.rb', line 32

def executable!
  raise Lunr::AlreadyExecuted.new(self) if executed?
end

#executed?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/lunr/search.rb', line 36

def executed?
  @executed
end

#pageObject Also known as: current_page



56
57
58
# File 'lib/lunr/search.rb', line 56

def page
  @page ||= execute && @search.query.page
end

#pagesObject Also known as: total_pages



60
61
62
63
# File 'lib/lunr/search.rb', line 60

def pages
  @pages ||= total / per +
    ((total_entries % per_page) > 0 ? 1 : 0)
end

#paramsObject



65
66
67
# File 'lib/lunr/search.rb', line 65

def params
  @search.query.to_params
end

#perObject Also known as: per_page



69
70
71
# File 'lib/lunr/search.rb', line 69

def per
  @per ||= execute && @search.query.per_page
end

#respond_to(name, include_private = false) ⇒ Object



73
74
75
# File 'lib/lunr/search.rb', line 73

def respond_to name, include_private = false
  klass.scopes.key?(name) || super
end

#scope(&block) ⇒ Object



77
78
79
80
81
82
# File 'lib/lunr/search.rb', line 77

def scope &block
  executable!
  @search.build(&block)

  self
end

#totalObject Also known as: size, total_entries



84
85
86
# File 'lib/lunr/search.rb', line 84

def total
  @total ||= execute && @search.total
end