Sha256: da2b38c2ee8f8fcc6dd2c1697e1ba99b52eec33b26c4507c1af81360c1aa6d61

Contents?: true

Size: 1.77 KB

Versions: 6

Compression:

Stored size: 1.77 KB

Contents

module Sunspot
  module Search
    class StatsJsonRow
      attr_reader :data, :value, :nested
      attr_writer :instance #:nodoc:

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

      def min
        data['min']
      end

      def max
        data['max']
      end

      def count
        data['count']
      end

      def sum
        data['sum']
      end

      def missing
        data['missing']
      end

      def sum_of_squares
        data['sumsq']
      end
      alias :sumsq :sum_of_squares

      def mean
        data['avg']
      end
      alias :avg :mean

      def standard_deviation
        data['stddev']
      end

      def facet name
        facets.find { |facet| facet.field.name == name.to_sym }
      end

      def facets
        @facets ||= @facet_fields.map do |field|
          StatsFacet.new(field, data['facets'][field.indexed_name])
        end
      end

      def instance
        if !defined?(@instance)
          @facet.populate_instances
        end
        @instance
      end

      def inspect
        "<Sunspot::Search::StatsJsonRow:#{value.inspect} min=#{min} max=#{max}"\
        " count=#{count} sum=#{sum} missing=#{missing} sum_of_squares=#{sum_of_squares}"\
        " mean=#{mean} standard_deviation=#{standard_deviation}"\
        " #{nested.nil? ? '' : "nested_count=#{nested.size}"}>"
      end

      private

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

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

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