class Sunspot::NullResult::GroupedCollection

Attributes

collection[R]
current_page[R]
group_by[R]
total_pages[R]

Public Class Methods

new(collection:, group_by: nil, current_page: 1, total_pages: 1) click to toggle source
# File lib/sunspot/null_result/grouped_collection.rb, line 10
def initialize(collection:, group_by: nil, current_page: 1, total_pages: 1)
  @collection = group_by.nil? ? [] : Array(collection)
  @group_by = group_by || :itself
  @current_page = current_page
  @total_pages = total_pages
end

Public Instance Methods

each(*args, &block) click to toggle source
# File lib/sunspot/null_result/grouped_collection.rb, line 29
def each(*args, &block)
  to_a.each(*args, &block)
end
empty?() click to toggle source
# File lib/sunspot/null_result/grouped_collection.rb, line 25
def empty?
  to_a.empty?
end
next_page() click to toggle source
# File lib/sunspot/null_result/grouped_collection.rb, line 33
def next_page
  [current_page + 1, total_pages].min
end
prev_page() click to toggle source
# File lib/sunspot/null_result/grouped_collection.rb, line 37
def prev_page
  [current_page - 1, 0].max
end
to_a() click to toggle source
# File lib/sunspot/null_result/grouped_collection.rb, line 17
def to_a
  grouped = collection.group_by(&group_by)

  grouped.keys.map do |group_key|
    Group.new(group_key, grouped[group_key])
  end
end