Class: ThinkingSphinx::FacetCollection

Inherits:
Hash
  • Object
show all
Defined in:
lib/thinking_sphinx/facet_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ FacetCollection

Returns a new instance of FacetCollection.



5
6
7
8
# File 'lib/thinking_sphinx/facet_collection.rb', line 5

def initialize(arguments)
  @arguments    = arguments.clone
  @facet_names  = []
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



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

def arguments
  @arguments
end

Instance Method Details

#add_from_results(facet, results) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/thinking_sphinx/facet_collection.rb', line 10

def add_from_results(facet, results)
  name = ThinkingSphinx::Facet.name_for(facet)
  
  self[name]  ||= {}
  @facet_names << name
  
  return if results.empty?
  
  facet = facet_from_object(results.first, facet) if facet.is_a?(String)
  
  results.each_with_groupby_and_count { |result, group, count|
    facet_value = facet.value(result, group)
    
    self[name][facet_value] ||= 0
    self[name][facet_value]  += count
  }
end

#for(hash = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/thinking_sphinx/facet_collection.rb', line 28

def for(hash = {})
  arguments        = @arguments.clone
  options          = arguments.extract_options!
  options[:with] ||= {}
  
  hash.each do |key, value|
    attrib = ThinkingSphinx::Facet.attribute_name_from_value(key, value)
    options[:with][attrib] = underlying_value key, value
  end
  
  arguments << options
  ThinkingSphinx::Search.search *arguments
end