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
# 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

  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)



122
123
124
125
126
127
128
129
130
# File 'lib/thinking_sphinx/search.rb', line 122

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

#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



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

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

#current_pageObject



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

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

#marshal_dumpObject



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

def marshal_dump
  populate

  [@populated, @query, @options, @context]
end

#marshal_load(array) ⇒ Object



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

def marshal_load(array)
  @populated, @query, @options, @context = array
end

#masksObject



51
52
53
# File 'lib/thinking_sphinx/search.rb', line 51

def masks
  @masks ||= @options[:masks] || DEFAULT_MASKS.clone
end

#metaObject



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

def meta
  populate
  context[:meta]
end

#offsetObject Also known as: offset_value



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

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

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



66
67
68
69
70
# File 'lib/thinking_sphinx/search.rb', line 66

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

#populateObject



74
75
76
77
78
79
80
81
# File 'lib/thinking_sphinx/search.rb', line 74

def populate
  return self if @populated

  middleware.call [context]
  @populated = true

  self
end

#populated!Object



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

def populated!
  @populated = true
end

#populated?Boolean

Returns:

  • (Boolean)


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

def populated?
  @populated
end

#query_timeObject



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

def query_time
  meta['time'].to_f
end

#rawObject



95
96
97
98
# File 'lib/thinking_sphinx/search.rb', line 95

def raw
  populate
  context[:raw]
end

#to_aObject



100
101
102
103
104
105
# File 'lib/thinking_sphinx/search.rb', line 100

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