Class: Stages::Stage

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/stage_base.rb

Direct Known Subclasses

Cache, Count, Each, Emit, Exhaust, ExhaustCount, Feeder, Limit, Map, Select, Unique, Wrap

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Stage

Returns a new instance of Stage.



6
7
8
9
# File 'lib/stage_base.rb', line 6

def initialize(&block)
  @block = block
  initialize_loop
end

Instance Attribute Details

#sourceObject

Returns the value of attribute source.



4
5
6
# File 'lib/stage_base.rb', line 4

def source
  @source
end

Instance Method Details

#dieObject



48
49
50
51
52
# File 'lib/stage_base.rb', line 48

def die
  loop do
    output :stages_eos
  end
end

#done?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/stage_base.rb', line 30

def done?
  return true if @done
  return false if @cached_value != :stages_empty_cache
  next_value = @fiber_delegate.resume
  if next_value == :stages_eos
    @done = true
    @cached_value = :stages_empty_cache
    return true
  end
  @cached_value = next_value
  return false
end

#drop_leftmost!Object



101
102
103
104
105
106
107
# File 'lib/stage_base.rb', line 101

def drop_leftmost!
  if @source.end?
    @source = nil
  else
    @source.drop_leftmost!
  end
end

#each(&block) ⇒ Object



68
69
70
# File 'lib/stage_base.rb', line 68

def each(&block)
  to_enum.each(&block)
end

#end?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/stage_base.rb', line 109

def end?
  @source.nil?
end

#handle_value(value) ⇒ Object



72
73
74
# File 'lib/stage_base.rb', line 72

def handle_value(value)
  output value
end

#initialize_loopObject



11
12
13
14
15
16
17
18
19
# File 'lib/stage_base.rb', line 11

def initialize_loop
  @done = false
  @cached_value = :stages_empty_cache
  @fiber_delegate = Fiber.new do
    process
    @done = true
    die
  end
end

#inputObject



80
81
82
# File 'lib/stage_base.rb', line 80

def input
  source.nil? ? :stages_eos : source.run
end

#lengthObject



113
114
115
116
117
118
119
# File 'lib/stage_base.rb', line 113

def length
  if source
    source.length + 1
  else
    1
  end
end

#output(value) ⇒ Object



84
85
86
# File 'lib/stage_base.rb', line 84

def output(value)
  Fiber.yield value
end

#processObject



54
55
56
57
58
# File 'lib/stage_base.rb', line 54

def process
  while !source_empty?
    handle_value input
  end
end

#resetObject



43
44
45
46
# File 'lib/stage_base.rb', line 43

def reset
  initialize_loop
  @source.reset if @source
end

#root_sourceObject

root_source lets you add to existing pipelines the result is always the rightmost stage so adding things to it is problematic



97
98
99
# File 'lib/stage_base.rb', line 97

def root_source
  source.nil? ? self : source.root_source
end

#runObject



21
22
23
24
25
26
27
28
# File 'lib/stage_base.rb', line 21

def run
  if @cached_value != :stages_empty_cache
    v = @cached_value
    @cached_value = :stages_empty_cache
    return v
  end
  @fiber_delegate.resume
end

#source_empty?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/stage_base.rb', line 76

def source_empty?
  (source.nil? || source.done?)
end

#to_enumObject



60
61
62
63
64
65
66
# File 'lib/stage_base.rb', line 60

def to_enum
  Enumerator.new do |y|
    while !done?
      y << run
    end
  end
end

#|(other) ⇒ Object



88
89
90
91
92
# File 'lib/stage_base.rb', line 88

def |(other)
  return self if other.nil?
  other.root_source.source = self
  other
end