Class: Sunspot::FacetData::QueryFacetData

Inherits:
Abstract
  • Object
show all
Defined in:
lib/sunspot/facet_data.rb

Overview

QueryFacetData encapsulates the data returned by a query facet.

Instance Attribute Summary

Attributes inherited from Abstract

#field

Instance Method Summary collapse

Methods inherited from Abstract

#cast, #reference, #row_value

Constructor Details

#initialize(outgoing_query_facet, row_data) ⇒ QueryFacetData

:nodoc:



102
103
104
105
# File 'lib/sunspot/facet_data.rb', line 102

def initialize(outgoing_query_facet, row_data) #:nodoc:
  @outgoing_query_facet, @row_data = outgoing_query_facet, row_data
  @field = @outgoing_query_facet.field
end

Instance Method Details

#nameObject



107
108
109
# File 'lib/sunspot/facet_data.rb', line 107

def name
  outgoing_query_facet.name
end

#rowsObject

Get the rows associated with this query facet. Returned rows are always ordered by count.

Returns

Array

Collection of QueryFacetRow objects, ordered by count



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/sunspot/facet_data.rb', line 119

def rows
  @rows ||=
    begin
      rows = []
      options = @outgoing_query_facet.options
      minimum_count =
        if options[:zeros] then 0
        elsif options[:minimum_count] then options[:minimum_count]
        else 1
        end
      for outgoing_row in  @outgoing_query_facet.rows
        row_query = outgoing_row.to_boolean_phrase
        if @row_data.has_key?(row_query)
          row = yield(outgoing_row.label, @row_data[row_query])
          rows << row if row.count >= minimum_count
        end
      end
      if options[:sort] == :index || !options[:limit] && options[:sort] != :count
        if rows.all? { |row| row.value.respond_to?(:<=>) }
          rows.sort! { |x, y| x.value <=> y.value }
        end
      else
        rows.sort! { |x, y| y.count <=> x.count }
      end
      if limit = options[:limit]
        rows[0, limit]
      else
        rows
      end
    end
end