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



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/lunr/search.rb', line 50

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

#as_json(options = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/lunr/search.rb', line 24

def as_json options = nil
  {}.tap do |j|
    j[:page]  = page
    j[:total] = total

    key = options.delete(:key) if Hash === options
    j[key || :entries] = map { |e| e.as_json options }
  end
end

#each(&block) ⇒ Object



34
35
36
# File 'lib/lunr/search.rb', line 34

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

#empty?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/lunr/search.rb', line 38

def empty?
  0 == total
end

#executable!Object



42
43
44
# File 'lib/lunr/search.rb', line 42

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

#executed?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/lunr/search.rb', line 46

def executed?
  @executed
end

#pageObject Also known as: current_page



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

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

#pagesObject Also known as: total_pages



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

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

#paramsObject



75
76
77
# File 'lib/lunr/search.rb', line 75

def params
  @search.query.to_params
end

#perObject Also known as: per_page



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

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

#respond_to(name, include_private = false) ⇒ Object



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

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

#resultsObject



87
88
89
# File 'lib/lunr/search.rb', line 87

def results
  execute && @results
end

#scope(&block) ⇒ Object



91
92
93
94
95
96
# File 'lib/lunr/search.rb', line 91

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

  self
end

#totalObject Also known as: size, total_entries



98
99
100
# File 'lib/lunr/search.rb', line 98

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