Sha256: 120fe9da5a30cf96e8610202bb1e11f0b02e5595be6ddb27a486bf7d899d9169

Contents?: true

Size: 1.05 KB

Versions: 6

Compression:

Stored size: 1.05 KB

Contents

module Sunspot
  module Search
    class JsonFacetRow
      attr_reader :value, :count, :nested
      attr_writer :instance #:nodoc:

      def initialize(data, facet) #:nodoc:
        @value = data['val']
        @count = data['distinct'] || data['count']
        @facet = facet
        @nested_key = data.keys.select { |k| data[k].is_a?(Hash) }.first
        @nested = recursive_nested_initialization(data) unless @nested_key.nil?
      end

      # 
      # Return the instance referenced by this facet row. Only valid for field
      # facets whose fields are defined with the :references key.
      #
      def instance
        if !defined?(@instance)
          @facet.populate_instances
        end
        @instance
      end

      def inspect
        "<Sunspot::Search::FacetRow:#{value.inspect} (#{count}) #{nested.nil? ? '' : " nested_count=#{nested.size}"}>"
      end

      private

      def recursive_nested_initialization(data)
        data[@nested_key]['buckets'].map do |d|
          JsonFacetRow.new(d, @facet)
        end
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
sunspot-2.7.1 lib/sunspot/search/json_facet_row.rb
sunspot-2.7.0 lib/sunspot/search/json_facet_row.rb
sunspot-2.6.0 lib/sunspot/search/json_facet_row.rb
sunspot-2.5.0 lib/sunspot/search/json_facet_row.rb
sunspot-2.4.0 lib/sunspot/search/json_facet_row.rb
sunspot-2.3.0 lib/sunspot/search/json_facet_row.rb