Class: Sunspot::Search::FieldJsonFacet

Inherits:
Object
  • Object
show all
Defined in:
lib/sunspot/search/field_json_facet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, search, options) ⇒ FieldJsonFacet

Returns a new instance of FieldJsonFacet.



7
8
9
10
# File 'lib/sunspot/search/field_json_facet.rb', line 7

def initialize(field, search, options)
  @name, @search, @options = (options[:name] || field.name), search, options
  @field = field
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/sunspot/search/field_json_facet.rb', line 5

def name
  @name
end

Instance Method Details

#no_data?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/sunspot/search/field_json_facet.rb', line 31

def no_data?
  @search.json_facet_response[@field.name.to_s].nil?
end

#other_count(type) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/sunspot/search/field_json_facet.rb', line 35

def other_count(type)
  json_facet_for_field = @search.json_facet_response[@field.name.to_s]
  return 0 if json_facet_for_field.nil?

  other = json_facet_for_field[type.to_s] || {}
  other['count']
end

#rowsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sunspot/search/field_json_facet.rb', line 12

def rows
  @rows ||=
    begin
      data = no_data? ? [] : @search.json_facet_response[@field.name.to_s]['buckets']
      rows = []
      data.each do |d|
        rows << JsonFacetRow.new(d, self)
      end

      if @options[:sort] == :count
        rows.sort! { |lrow, rrow| rrow.count <=> lrow.count }
      else
        rows.sort! { |lrow, rrow| lrow.value <=> rrow.value }
      end
      rows
    end

end