Class: ThinkingSphinx::Collection

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page, per_page, entries, total_entries) ⇒ Collection

Returns a new instance of Collection.



9
10
11
12
13
# File 'lib/thinking_sphinx/collection.rb', line 9

def initialize(page, per_page, entries, total_entries)
  @current_page, @per_page, @total_entries = page, per_page, total_entries
  
  @total_pages = (entries / @per_page.to_f).ceil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



116
117
118
119
120
# File 'lib/thinking_sphinx/collection.rb', line 116

def method_missing(method, *args, &block)
  super unless method.to_s[/^each_with_.*/]
  
  each_with_attribute method.to_s.gsub(/^each_with_/, ''), &block
end

Instance Attribute Details

#current_pageObject (readonly)

Returns the value of attribute current_page.



3
4
5
# File 'lib/thinking_sphinx/collection.rb', line 3

def current_page
  @current_page
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



3
4
5
# File 'lib/thinking_sphinx/collection.rb', line 3

def per_page
  @per_page
end

#resultsObject

Returns the value of attribute results.



4
5
6
# File 'lib/thinking_sphinx/collection.rb', line 4

def results
  @results
end

#total_entriesObject (readonly)

Returns the value of attribute total_entries.



3
4
5
# File 'lib/thinking_sphinx/collection.rb', line 3

def total_entries
  @total_entries
end

#total_pagesObject (readonly) Also known as: page_count

Returns the value of attribute total_pages.



3
4
5
# File 'lib/thinking_sphinx/collection.rb', line 3

def total_pages
  @total_pages
end

Class Method Details

.class_from_crc(crc) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/thinking_sphinx/collection.rb', line 93

def self.class_from_crc(crc)
  @@models_by_crc ||= ThinkingSphinx.indexed_models.inject({}) do |hash, model|
    hash[model.constantize.to_crc32] = model
    model.constantize.subclasses.each { |subclass|
      hash[subclass.to_crc32] = subclass.name
    }
    hash
  end
  @@models_by_crc[crc].constantize
end

.create_from_results(results, page, limit, options) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/thinking_sphinx/collection.rb', line 26

def self.create_from_results(results, page, limit, options)
  collection = self.new(page, limit,
    results[:total] || 0, results[:total_found] || 0
  )
  collection.results = results
  collection.replace instances_from_matches(results[:matches], options)
  return collection
end

.ids_from_results(results, page, limit, options) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/thinking_sphinx/collection.rb', line 15

def self.ids_from_results(results, page, limit, options)
  collection = self.new(page, limit,
    results[:total] || 0, results[:total_found] || 0
  )
  collection.results = results
  collection.replace results[:matches].collect { |match|
    match[:attributes]["sphinx_internal_id"]
  }
  return collection
end

.instances_from_class(klass, matches, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/thinking_sphinx/collection.rb', line 43

def self.instances_from_class(klass, matches, options = {})
  index_options = klass.sphinx_index_options

  ids = matches.collect { |match| match[:attributes]["sphinx_internal_id"] }
  instances = ids.length > 0 ? klass.find(
    :all,
    :joins      => options[:joins],
    :conditions => {klass.primary_key.to_sym => ids},
    :include    => (options[:include] || index_options[:include]),
    :select     => (options[:select]  || index_options[:select]),
    :order      => (options[:sql_order] || index_options[:sql_order])
  ) : []

  # Raise an exception if we find records in Sphinx but not in the DB, so
  # the search method can retry without them. See 
  # ThinkingSphinx::Search.retry_search_on_stale_index.
  if options[:raise_on_stale] && instances.length < ids.length
    stale_ids = ids - instances.map {|i| i.id }
    raise StaleIdsException, stale_ids
  end

  # if the user has specified an SQL order, return the collection
  # without rearranging it into the Sphinx order
  return instances if options[:sql_order]

  ids.collect { |obj_id|
    instances.detect { |obj| obj.id == obj_id }
  }
end

.instances_from_classes(matches, options = {}) ⇒ Object

Group results by class and call #find(:all) once for each group to reduce the number of #find’s in multi-model searches.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/thinking_sphinx/collection.rb', line 76

def self.instances_from_classes(matches, options = {})
  groups = matches.group_by { |match| match[:attributes]["class_crc"] }
  groups.each do |crc, group|
    group.replace(
      instances_from_class(class_from_crc(crc), group, options)
    )
  end
  
  matches.collect do |match|
    groups.detect { |crc, group|
      crc == match[:attributes]["class_crc"]
    }[1].detect { |obj|
      obj.id == match[:attributes]["sphinx_internal_id"]
    }
  end
end

.instances_from_matches(matches, options = {}) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/thinking_sphinx/collection.rb', line 35

def self.instances_from_matches(matches, options = {})
  if klass = options[:class]
    instances_from_class klass, matches, options
  else
    instances_from_classes matches, options
  end
end

Instance Method Details

#each_with_attribute(attribute, &block) ⇒ Object



128
129
130
131
132
# File 'lib/thinking_sphinx/collection.rb', line 128

def each_with_attribute(attribute, &block)
  results[:matches].each_with_index do |match, index|
    yield self[index], (match[:attributes][attribute] || match[:attributes]["@#{attribute}"])
  end
end

#each_with_groupby_and_count(&block) ⇒ Object



122
123
124
125
126
# File 'lib/thinking_sphinx/collection.rb', line 122

def each_with_groupby_and_count(&block)
  results[:matches].each_with_index do |match, index|
    yield self[index], match[:attributes]["@groupby"], match[:attributes]["@count"]
  end
end

#each_with_weighting(&block) ⇒ Object



134
135
136
137
138
# File 'lib/thinking_sphinx/collection.rb', line 134

def each_with_weighting(&block)
  results[:matches].each_with_index do |match, index|
    yield self[index], match[:weight]
  end
end

#inject_with_groupby_and_count(initial = nil, &block) ⇒ Object



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

def inject_with_groupby_and_count(initial = nil, &block)
  index = -1
  results[:matches].inject(initial) do |memo, match|
    index += 1
    yield memo, self[index], match[:attributes]["@groupby"], match[:attributes]["@count"]
  end
end

#next_pageObject



108
109
110
# File 'lib/thinking_sphinx/collection.rb', line 108

def next_page
  current_page < total_pages ? (current_page + 1): nil
end

#offsetObject



112
113
114
# File 'lib/thinking_sphinx/collection.rb', line 112

def offset
  (current_page - 1) * @per_page
end

#previous_pageObject



104
105
106
# File 'lib/thinking_sphinx/collection.rb', line 104

def previous_page
  current_page > 1 ? (current_page - 1) : nil
end