Sha256: d6ad63f9ca5601eada77fb873d0c69eaf5a48e1a24e7da9b8f93adac0c4f814f

Contents?: true

Size: 1.81 KB

Versions: 9

Compression:

Stored size: 1.81 KB

Contents

require 'ostruct'

module Blacklight::SolrResponse::Facets
  
  # represents a facet value; which is a field value and its hit count
  class FacetItem < OpenStruct
    def initialize *args
      options = args.extract_options!

      # Backwards-compat method signature
      value = args.shift
      hits = args.shift

      options[:value] = value if value
      options[:hits] = hits if hits
      
      super(options)
    end

    def label
      super || value
    end
  end
  
  # represents a facet; which is a field and its values
  class FacetField
    attr_reader :name, :items
    def initialize name, items
      @name, @items = name, items
    end
  end
  
  # @response.facets.each do |facet|
  #   facet.name
  #   facet.items
  # end
  # "caches" the result in the @facets instance var
  def facets
    @facets ||= (
      facet_fields.map do |(facet_field_name,values_and_hits)|
        items = []
        values_and_hits.each_slice(2) do |k,v|
          items << FacetItem.new(:value => k, :hits => v)
        end
        FacetField.new(facet_field_name, items)
      end
    )
  end
  
  # pass in a facet field name and get back a Facet instance
  def facet_by_field_name(name)
    @facets_by_field_name ||= {}
    @facets_by_field_name[name] ||= (
      facets.detect{|facet|facet.name.to_s == name.to_s}
    )
  end
  
  def facet_counts
    @facet_counts ||= self['facet_counts'] || {}
  end

  # Returns the hash of all the facet_fields (ie: {'instock_b' => ['true', 123, 'false', 20]}
  def facet_fields
    @facet_fields ||= facet_counts['facet_fields'] || {}
  end

  # Returns all of the facet queries
  def facet_queries
    @facet_queries ||= facet_counts['facet_queries'] || {}
  end
  
  # Returns all of the facet queries
  def facet_pivot
    @facet_pivot ||= facet_counts['facet_pivot'] || {}
  end
  
end # end Facets

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
blacklight-4.3.0 lib/blacklight/solr_response/facets.rb
blacklight-4.2.2 lib/blacklight/solr_response/facets.rb
blacklight-4.2.1 lib/blacklight/solr_response/facets.rb
blacklight-4.2.0 lib/blacklight/solr_response/facets.rb
blacklight-4.1.0 lib/blacklight/solr_response/facets.rb
blacklight-4.0.1 lib/blacklight/solr_response/facets.rb
blacklight-4.0.0 lib/blacklight/solr_response/facets.rb
blacklight-4.0.0.rc2 lib/blacklight/solr_response/facets.rb
blacklight-4.0.0.rc1 lib/blacklight/solr_response/facets.rb