Class: Sunspot::NullResult::GroupedCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sunspot/null_result/grouped_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection, group_by = nil) ⇒ GroupedCollection

Returns a new instance of GroupedCollection.



9
10
11
12
# File 'lib/sunspot/null_result/grouped_collection.rb', line 9

def initialize(collection, group_by = nil)
  @collection = group_by.nil? ? [] : Array(collection)
  @group_by = group_by || :itself
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



7
8
9
# File 'lib/sunspot/null_result/grouped_collection.rb', line 7

def collection
  @collection
end

#group_byObject (readonly)

Returns the value of attribute group_by.



7
8
9
# File 'lib/sunspot/null_result/grouped_collection.rb', line 7

def group_by
  @group_by
end

Instance Method Details

#each(*args, &block) ⇒ Object



22
23
24
# File 'lib/sunspot/null_result/grouped_collection.rb', line 22

def each(*args, &block)
  to_a.each(*args, &block)
end

#to_aObject



14
15
16
17
18
19
20
# File 'lib/sunspot/null_result/grouped_collection.rb', line 14

def to_a
  grouped = collection.group_by(&group_by)

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