Class: Elastic::Results::GroupedResult

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/elastic/results/grouped_result.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#as_value

Methods included from Support::Traversable

#pick_nodes

Constructor Details

#initialize(_key_names, _groups) ⇒ GroupedResult

Returns a new instance of GroupedResult.



7
8
9
10
# File 'lib/elastic/results/grouped_result.rb', line 7

def initialize(_key_names, _groups)
  @key_names = _key_names.freeze
  @groups = _groups.to_a
end

Instance Attribute Details

#key_namesObject (readonly)

Returns the value of attribute key_names.



5
6
7
# File 'lib/elastic/results/grouped_result.rb', line 5

def key_names
  @key_names
end

Instance Method Details

#[](_key) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/elastic/results/grouped_result.rb', line 16

def [](_key)
  if _key.is_a? Hash
    mapped_results[_key]
  else
    raise ArgumentError, '' if @key_names.length > 1
    mapped_results[@key_names.first => _key]
  end
end

#countObject



34
35
36
# File 'lib/elastic/results/grouped_result.rb', line 34

def count
  @groups.count
end

#each(&_block) ⇒ Object



12
13
14
# File 'lib/elastic/results/grouped_result.rb', line 12

def each(&_block)
  @groups.map { |g| group_as_pair g }.each(&_block)
end

#each_group(&_block) ⇒ Object



25
26
27
# File 'lib/elastic/results/grouped_result.rb', line 25

def each_group(&_block)
  @groups.each(&_block)
end

#group_at(_idx) ⇒ Object



38
39
40
# File 'lib/elastic/results/grouped_result.rb', line 38

def group_at(_idx)
  @groups[_idx]
end

#lastObject



29
30
31
32
# File 'lib/elastic/results/grouped_result.rb', line 29

def last
  return nil if @groups.count == 0
  group_as_pair @groups.last
end

#map_to_group(&_block) ⇒ Object



47
48
49
50
51
# File 'lib/elastic/results/grouped_result.rb', line 47

def map_to_group(&_block)
  self.class.new(@key_names, @groups.map do |group|
    Elastic::Results::ResultGroup.new group.keys, _block.call(group.as_value)
  end)
end

#traverse(&_block) ⇒ Object



42
43
44
45
# File 'lib/elastic/results/grouped_result.rb', line 42

def traverse(&_block)
  super
  @groups.each { |h| h.traverse(&_block) }
end