Class: ThinkingSphinx::Search

Inherits:
Array
  • Object
show all
Defined in:
lib/thinking_sphinx/search.rb

Defined Under Namespace

Classes: BatchInquirer, Context, Glaze, Merger, Query, StaleIdsException

Constant Summary

CORE_METHODS =
%w( == class class_eval extend frozen? id instance_eval
instance_of? instance_values instance_variable_defined?
instance_variable_get instance_variable_set instance_variables is_a?
kind_of? member? method methods nil? object_id respond_to?
respond_to_missing? send should should_not type )
SAFE_METHODS =
%w( partition private_methods protected_methods public_methods
send class )
DEFAULT_MASKS =
[
  ThinkingSphinx::Masks::PaginationMask,
  ThinkingSphinx::Masks::ScopesMask,
  ThinkingSphinx::Masks::GroupEnumeratorsMask
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query = nil, options = {}) ⇒ Search

Returns a new instance of Search



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

def initialize(query = nil, options = {})
  query, options   = nil, query if query.is_a?(Hash)
  @query, @options = query, options
  @masks           = @options.delete(:masks) || DEFAULT_MASKS.clone
  @middleware      = @options.delete(:middleware)

  populate if options[:populate]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



101
102
103
104
105
106
107
108
109
# File 'lib/thinking_sphinx/search.rb', line 101

def method_missing(method, *args, &block)
  mask_stack.each do |mask|
    return mask.send(method, *args, &block) if mask.can_handle?(method)
  end

  populate if !SAFE_METHODS.include?(method.to_s)

  context[:results].send(method, *args, &block)
end

Instance Attribute Details

#masksObject (readonly)

Returns the value of attribute masks



21
22
23
# File 'lib/thinking_sphinx/search.rb', line 21

def masks
  @masks
end

#optionsObject (readonly)

Returns the value of attribute options



21
22
23
# File 'lib/thinking_sphinx/search.rb', line 21

def options
  @options
end

#queryObject

Returns the value of attribute query



22
23
24
# File 'lib/thinking_sphinx/search.rb', line 22

def query
  @query
end

Instance Method Details

#contextObject



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

def context
  @context ||= ThinkingSphinx::Search::Context.new self,
    ThinkingSphinx::Configuration.instance
end

#current_pageObject



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

def current_page
  options[:page] = 1 if options[:page].blank?
  options[:page].to_i
end

#metaObject



43
44
45
46
# File 'lib/thinking_sphinx/search.rb', line 43

def meta
  populate
  context[:meta]
end

#offsetObject Also known as: offset_value



48
49
50
# File 'lib/thinking_sphinx/search.rb', line 48

def offset
  @options[:offset] || ((current_page - 1) * per_page)
end

#per_page(value = nil) ⇒ Object Also known as: limit_value



54
55
56
57
58
# File 'lib/thinking_sphinx/search.rb', line 54

def per_page(value = nil)
  @options[:limit] = value unless value.nil?
  @options[:limit] ||= (@options[:per_page] || 20)
  @options[:limit].to_i
end

#populateObject



62
63
64
65
66
67
68
69
# File 'lib/thinking_sphinx/search.rb', line 62

def populate
  return self if @populated

  middleware.call [context]
  @populated = true

  self
end

#populated!Object



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

def populated!
  @populated = true
end

#query_timeObject



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

def query_time
  meta['time'].to_f
end

#rawObject



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

def raw
  populate
  context[:raw]
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(method, include_private = false)
  super || context[:results].respond_to?(method, include_private)
end

#to_aObject



88
89
90
91
92
93
# File 'lib/thinking_sphinx/search.rb', line 88

def to_a
  populate
  context[:results].collect { |result|
    result.respond_to?(:unglazed) ? result.unglazed : result
  }
end