Sha256: 4df4115a0f70a878c0dae7ef4dc9bcf3d79cef9ae8ff55cc500ee716639e450a

Contents?: true

Size: 1.14 KB

Versions: 10

Compression:

Stored size: 1.14 KB

Contents

module ThinkingSphinx
  class FacetCollection < Hash
    attr_accessor :arguments
    
    def initialize(arguments)
      @arguments        = arguments.clone
      @attribute_values = {}
      @facets           = []
    end
    
    def add_from_results(facet, results)
      self[facet.name]              = {}
      @attribute_values[facet.name] = {}
      @facets << facet
      
      results.each_with_groupby_and_count { |result, group, count|
        facet_value = facet.value(result, group)
        
        self[facet.name][facet_value]               = count
        @attribute_values[facet.name][facet_value]  = group
      }
    end
    
    def for(hash = {})
      arguments        = @arguments.clone
      options          = arguments.extract_options!
      options[:with] ||= {}
      
      hash.each do |key, value|
        attrib = facet_for_key(key).attribute_name
        options[:with][attrib] = @attribute_values[key][value]
      end
      
      arguments << options
      ThinkingSphinx::Search.search *arguments
    end
    
    private
    
    def facet_for_key(key)
      @facets.detect { |facet| facet.name == key }
    end
  end
end

Version data entries

10 entries across 10 versions & 4 rubygems

Version Path
freelancing-god-thinking-sphinx-1.1.4 lib/thinking_sphinx/facet_collection.rb
freelancing-god-thinking-sphinx-1.1.5 lib/thinking_sphinx/facet_collection.rb
freelancing-god-thinking-sphinx-1.1.6 lib/thinking_sphinx/facet_collection.rb
lostboy-thinking-sphinx-1.1.5.1 lib/thinking_sphinx/facet_collection.rb
lostboy-thinking-sphinx-1.1.5.2 lib/thinking_sphinx/facet_collection.rb
lostboy-thinking-sphinx-1.1.5.3 lib/thinking_sphinx/facet_collection.rb
lostboy-thinking-sphinx-1.1.5.4 lib/thinking_sphinx/facet_collection.rb
lostboy-thinking-sphinx-1.1.5.5 lib/thinking_sphinx/facet_collection.rb
pixeltrix-thinking-sphinx-1.1.5 lib/thinking_sphinx/facet_collection.rb
sherpa99-thinking-sphinx-1.1.4 lib/thinking_sphinx/facet_collection.rb