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.



6
7
8
9
10
# File 'lib/thinking_sphinx/collection.rb', line 6

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



81
82
83
84
85
# File 'lib/thinking_sphinx/collection.rb', line 81

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

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

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



58
59
60
61
62
63
64
65
66
67
# File 'lib/thinking_sphinx/collection.rb', line 58

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



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

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



12
13
14
15
16
17
18
19
20
21
# File 'lib/thinking_sphinx/collection.rb', line 12

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

.instance_from_match(match, options) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/thinking_sphinx/collection.rb', line 49

def self.instance_from_match(match, options)
  # puts "ARGS: #{match[:attributes]["sphinx_internal_id"].inspect}, {:include => #{options[:include].inspect}, :select => #{options[:select].inspect}}"
  class_from_crc(match[:attributes]["class_crc"]).find(
    match[:attributes]["sphinx_internal_id"],
    :include => options[:include],
    :select  => options[:select]
  )
end

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/thinking_sphinx/collection.rb', line 32

def self.instances_from_matches(matches, options = {})
  return matches.collect { |match|
    instance_from_match match, options
  } unless klass = options[:class]
  
  ids = matches.collect { |match| match[:attributes]["sphinx_internal_id"] }
  instances = ids.length > 0 ? klass.find(
    :all,
    :conditions => {klass.primary_key.to_sym => ids},
    :include    => options[:include],
    :select     => options[:select]
  ) : []
  ids.collect { |obj_id|
    instances.detect { |obj| obj.id == obj_id }
  }
end

Instance Method Details

#each_with_attribute(attribute, &block) ⇒ Object



93
94
95
96
97
# File 'lib/thinking_sphinx/collection.rb', line 93

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_group_and_count(&block) ⇒ Object



87
88
89
90
91
# File 'lib/thinking_sphinx/collection.rb', line 87

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

#each_with_weighting(&block) ⇒ Object



99
100
101
102
103
# File 'lib/thinking_sphinx/collection.rb', line 99

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

#next_pageObject



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

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

#offsetObject



77
78
79
# File 'lib/thinking_sphinx/collection.rb', line 77

def offset
  (current_page - 1) * @per_page
end

#previous_pageObject



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

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