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:



116
117
118
119
# File 'lib/sunspot/facet_data.rb', line 116

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

Use the name defined by the user



124
125
126
# File 'lib/sunspot/facet_data.rb', line 124

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



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/sunspot/facet_data.rb', line 136

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