Sha256: 77cdcf9daa9ad217c9c42a0dae44c9df31ced4ca80a2b4da91ee84501e9919e7

Contents?: true

Size: 1.46 KB

Versions: 17

Compression:

Stored size: 1.46 KB

Contents

module ThinkingSphinx
  class FacetCollection < Hash
    attr_accessor :arguments
    
    def initialize(arguments)
      @arguments    = arguments.clone
      @facet_names  = []
    end
    
    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
    
    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
    
    private
    
    def underlying_value(key, value)
      case value
      when Array
        value.collect { |item| underlying_value(key, item) }
      when String
        value.to_crc32
      else
        value
      end
    end
    
    def facet_from_object(object, name)
      object.sphinx_facets.detect { |facet| facet.attribute_name == name }
    end
  end
end

Version data entries

17 entries across 17 versions & 5 rubygems

Version Path
DrMark-thinking-sphinx-1.1.14 lib/thinking_sphinx/facet_collection.rb
DrMark-thinking-sphinx-1.1.15 lib/thinking_sphinx/facet_collection.rb
dpickett-thinking-sphinx-1.1.23 lib/thinking_sphinx/facet_collection.rb
ebeigarts-thinking-sphinx-1.1.21 lib/thinking_sphinx/facet_collection.rb
ebeigarts-thinking-sphinx-1.1.22 lib/thinking_sphinx/facet_collection.rb
freelancing-god-thinking-sphinx-1.1.14 lib/thinking_sphinx/facet_collection.rb
freelancing-god-thinking-sphinx-1.1.15 lib/thinking_sphinx/facet_collection.rb
freelancing-god-thinking-sphinx-1.1.16 lib/thinking_sphinx/facet_collection.rb
freelancing-god-thinking-sphinx-1.1.17 lib/thinking_sphinx/facet_collection.rb
freelancing-god-thinking-sphinx-1.1.18 lib/thinking_sphinx/facet_collection.rb
freelancing-god-thinking-sphinx-1.1.19 lib/thinking_sphinx/facet_collection.rb
freelancing-god-thinking-sphinx-1.1.20 lib/thinking_sphinx/facet_collection.rb
freelancing-god-thinking-sphinx-1.1.21 lib/thinking_sphinx/facet_collection.rb
freelancing-god-thinking-sphinx-1.1.22 lib/thinking_sphinx/facet_collection.rb
freelancing-god-thinking-sphinx-1.1.23 lib/thinking_sphinx/facet_collection.rb
freelancing-god-thinking-sphinx-1.1.24 lib/thinking_sphinx/facet_collection.rb
initforthe-thinking-sphinx-1.1.21 lib/thinking_sphinx/facet_collection.rb