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 collapse

CORE_METHODS =
%w( == class class_eval extend frozen? id instance_eval
instance_exec 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 )
KNOWN_OPTIONS =
(
  [
    :classes, :conditions, :excerpts, :geo, :group_by, :ids_only,
    :ignore_scopes, :indices, :limit, :masks, :max_matches, :middleware,
    :none, :offset, :order, :order_group_by, :page, :per_page, :populate,
    :retry_stale, :select, :skip_sti, :sql, :star, :with, :with_all, :without,
    :without_ids
  ] +
  ThinkingSphinx::Middlewares::SphinxQL::SELECT_OPTIONS
).uniq
DEFAULT_MASKS =
[
  ThinkingSphinx::Masks::PaginationMask,
  ThinkingSphinx::Masks::ScopesMask,
  ThinkingSphinx::Masks::GroupEnumeratorsMask
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Search.



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

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)



140
141
142
143
144
145
146
147
148
# File 'lib/thinking_sphinx/search.rb', line 140

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.



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

def options
  @options
end

#queryObject

Returns the value of attribute query.



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

def query
  @query
end

Class Method Details

.valid_optionsObject



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

def self.valid_options
  @valid_options
end

Instance Method Details

#contextObject



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

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

#current_pageObject



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

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

#marshal_dumpObject



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

def marshal_dump
  populate

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

#marshal_load(array) ⇒ Object



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

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

#masksObject



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

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

#metaObject



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

def meta
  populate
  context[:meta]
end

#offsetObject Also known as: offset_value



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

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

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



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

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

#populateObject



92
93
94
95
96
97
98
99
# File 'lib/thinking_sphinx/search.rb', line 92

def populate
  return self if @populated

  middleware.call [context] unless options[:none]
  @populated = true

  self
end

#populated!Object



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

def populated!
  @populated = true
end

#populated?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/thinking_sphinx/search.rb', line 105

def populated?
  @populated
end

#query_timeObject



109
110
111
# File 'lib/thinking_sphinx/search.rb', line 109

def query_time
  meta['time'].to_f
end

#rawObject



113
114
115
116
# File 'lib/thinking_sphinx/search.rb', line 113

def raw
  populate
  context[:raw]
end

#to_aObject



118
119
120
121
122
123
# File 'lib/thinking_sphinx/search.rb', line 118

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