Class: RedGrape::Pipe::GroupByPipe

Inherits:
Base show all
Defined in:
lib/red_grape/pipe/group_by_pipe.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#it, #next, #opts, #prev, #value

Instance Method Summary collapse

Methods inherited from Base

#copy, #done?, #dup, #first?, #first_pipe, #initialize, #last?, #method_missing, #pass_next, #pipe_eval, #pipe_name, #size, #take, #to_a, #to_ary, #to_s

Constructor Details

This class inherits a constructor from RedGrape::Pipe::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RedGrape::Pipe::Base

Instance Attribute Details

#reduce_functionObject

Returns the value of attribute reduce_function.



6
7
8
# File 'lib/red_grape/pipe/group_by_pipe.rb', line 6

def reduce_function
  @reduce_function
end

Instance Method Details

#pass(obj, context) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/red_grape/pipe/group_by_pipe.rb', line 8

def pass(obj, context)
  key_function, value_function, @reduce_function = *self.opts
  value_function ||= proc{it}
  @reduce_function ||= proc{it}
  if self.opts.empty?
    context.group self.next, obj
  elsif self.opts.first.is_a? Proc
    context.group(
      self.next, 
      pipe_eval(it:obj, &key_function), 
      pipe_eval(it:obj, &value_function)
    )
  else
    self.opts.first[obj] ||= []
    self.opts.first[obj] << obj 
  end
  obj
end