Class: RedGrape::Pipe::LoopPipe

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

Defined Under Namespace

Classes: LoopContext

Instance Attribute Summary

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 Method Details

#pass(obj, context) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/red_grape/pipe/loop_pipe.rb', line 8

def pass(obj, context)
  if self.opts.first.is_a? Proc
    loop_condition, label = *self.opts
  else
    label, loop_condition, emit_condition = *self.opts
  end

  anchor_pipe = self;
  case label
  when Integer
    label.times {anchor_pipe = anchor_pipe.prev}
  when String, Symbol
    begin
      anchor_pipe = anchor_pipe.prev
    end until anchor_pipe.is_a?(AsPipe) && anchor_pipe.opts.first == label
  else
    raise 'label should be an integer or a string.'
  end
  context.loops += 1
  it = LoopContext.new context.loops, context.history, obj
  if context.eval :it => it, &loop_condition
    obj.pass_through anchor_pipe, context
  else
    context.loops = 1
    if emit_condition && !context.eval(:it => it, &emit_condition)
      nil
    else
      pass_next context, nil, obj
    end
  end
end