Class: Marso::FiberFilter

Inherits:
Enumerate show all
Defined in:
lib/marso/toolbelt/fiberpiping.rb

Constant Summary

Constants inherited from Enumerate

Enumerate::STOP_MSG

Instance Attribute Summary

Attributes inherited from Enumerate

#process, #src

Instance Method Summary collapse

Methods inherited from Enumerate

#clone, #execute, from, #select, #select_many, #to_a, #where, #|

Constructor Details

#initialize(process, source = {}) ⇒ FiberFilter

Returns a new instance of FiberFilter.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/marso/toolbelt/fiberpiping.rb', line 174

def initialize(process, source={})
  @src = source
  @process = process

  @fiber_delegate = Fiber.new do
    output = nil
    while input = Fiber.yield(output)
      if process.call(input)
        output = input
      else
        output = :invalid_input
      end
    end
  end

  @fiber_delegate.resume
end

Instance Method Details

#resumeObject



192
193
194
195
196
197
198
199
200
201
# File 'lib/marso/toolbelt/fiberpiping.rb', line 192

def resume
  v = nil
  if @src.is_a?(Marso::Enumerate)
    v = @fiber_delegate.resume(@src.resume)
    v = self.resume if v == :invalid_input
  else
    v = @fiber_delegate.resume
  end
  return v
end

#source(other_source) ⇒ Object



203
204
205
# File 'lib/marso/toolbelt/fiberpiping.rb', line 203

def source(other_source)
  FiberFilter.new(@process, other_source)
end