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
9
# File 'lib/thinking_sphinx/facet_collection.rb', line 5

def initialize(arguments)
  @arguments        = arguments.clone
  @attribute_values = {}
  @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



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

def add_from_results(facet, results)
  name = ThinkingSphinx::Facet.name_for(facet)

  self[name]              ||= {}
  @attribute_values[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
    @attribute_values[name][facet_value]  = group
  }
end

#for(hash = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/thinking_sphinx/facet_collection.rb', line 31

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