Sha256: c31a0ee182d8438ade396fb4ef2260543a9ddbec6fb280febbb9b6d54e0d753a

Contents?: true

Size: 1.88 KB

Versions: 21

Compression:

Stored size: 1.88 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

    def as_json(props = nil)
      table.as_json(props)
    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

21 entries across 21 versions & 1 rubygems

Version Path
blacklight-4.9.0 lib/blacklight/solr_response/facets.rb
blacklight-4.8.0 lib/blacklight/solr_response/facets.rb
blacklight-5.0.3 lib/blacklight/solr_response/facets.rb
blacklight-5.0.2 lib/blacklight/solr_response/facets.rb
blacklight-5.0.1 lib/blacklight/solr_response/facets.rb
blacklight-5.0.0 lib/blacklight/solr_response/facets.rb
blacklight-4.7.0 lib/blacklight/solr_response/facets.rb
blacklight-4.7.0.pre1 lib/blacklight/solr_response/facets.rb
blacklight-5.0.0.pre4 lib/blacklight/solr_response/facets.rb
blacklight-4.6.3 lib/blacklight/solr_response/facets.rb
blacklight-5.0.0.pre3 lib/blacklight/solr_response/facets.rb
blacklight-4.6.2 lib/blacklight/solr_response/facets.rb
blacklight-5.0.0.pre2 lib/blacklight/solr_response/facets.rb
blacklight-4.6.1 lib/blacklight/solr_response/facets.rb
blacklight-5.0.0.pre1 lib/blacklight/solr_response/facets.rb
blacklight-4.6.0 lib/blacklight/solr_response/facets.rb
blacklight-4.5.0 lib/blacklight/solr_response/facets.rb
blacklight-4.5.0.rc1 lib/blacklight/solr_response/facets.rb
blacklight-4.4.2 lib/blacklight/solr_response/facets.rb
blacklight-4.4.1 lib/blacklight/solr_response/facets.rb