class GxG::Enumerator

Public Class Methods

new(object=nil,method=:each,*args, &block) click to toggle source

this is a bit inefficient, but it gets the job done. It is essentially an each wrapper to do a cooperative pauses for non-blocking goodness. :) TODO: inspiration: what about just leveraging 'super' after a pause operation on all overrides. (thx)

# File lib/gxg/gxg_elements.rb, line 282
def initialize(object=nil,method=:each,*args, &block)
  # Without the call to super() it works, but will be uninitialized
  # super(object,method,*args)
  @collection = object.stock_to_enum(method,*args, &block)
  # @collection = ::Enumerator.new(object,method,*args,&block)
  self
end

Public Instance Methods

each(&block) click to toggle source
# File lib/gxg/gxg_elements.rb, line 290
def each(&block)
  # reference = {:object => self, :method => __callee__, :fiber => Fiber.current, :thread => Thread.current}
  if block.respond_to?(:call)
    if ::GxG::EventManager::manager_running?
      @collection.each do |*args|
        block.call(*args)
        pause
      end
      self
    else
      @collection.each(&block)
      self
    end
  else
    self
  end
end
each_with_index(offset=0,&block)
Alias for: with_index
next() click to toggle source
# File lib/gxg/gxg_elements.rb, line 308
def next()
  pause
  @collection.next()
end
next_values() click to toggle source
# File lib/gxg/gxg_elements.rb, line 313
def next_values()
  pause
  @collection.next_values()
end
peek() click to toggle source
# File lib/gxg/gxg_elements.rb, line 318
def peek()
  pause
  @collection.peek()
end
peek_values() click to toggle source
# File lib/gxg/gxg_elements.rb, line 323
def peek_values()
  pause
  @collection.peek_values()
end
rewind() click to toggle source
# File lib/gxg/gxg_elements.rb, line 328
def rewind()
  if @collection.respond_to?(:rewind)
    @collection.rewind
  end
  self
end
with_index(offset=0,&block) click to toggle source
# File lib/gxg/gxg_elements.rb, line 335
def with_index(offset=0,&block)
  enumerator = GxG::Enumerator.new(@collection.with_index(offset))
  if block.respond_to?(:call)
    enumerator.each do |entry,index|
      block.call(entry,index)
    end
  else
    enumerator
  end
end
Also aliased as: each_with_index
with_object(an_object=nil,&block) click to toggle source
# File lib/gxg/gxg_elements.rb, line 347
def with_object(an_object=nil,&block)
  enumerator = GxG::Enumerator.new(@collection.with_object(an_object))
  if block.respond_to?(:call)
    enumerator.each do |entry,the_object|
      block.call(entry,the_object)
    end
    an_object
  else
    enumerator
  end
end