Class: Skynet::TaskIterator

Inherits:
Object
  • Object
show all
Includes:
Enumerable, GuidGenerator, SkynetDebugger
Defined in:
lib/skynet/job.rb

Overview

class LocalMessageQueue

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#mapreduce

Methods included from GuidGenerator

#get_unique_id

Methods included from SkynetDebugger

#args_pp, #debug, #debug_header, #error, #fatal, included, #info, #log, #printlog, #stderr, #stdout, #warn

Constructor Details

#initialize(task_options, data) ⇒ TaskIterator

Returns a new instance of TaskIterator.



830
831
832
833
# File 'lib/skynet/job.rb', line 830

def initialize(task_options, data)            
  @task_options = task_options
  @data         = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



828
829
830
# File 'lib/skynet/job.rb', line 828

def data
  @data
end

#task_optionsObject

Returns the value of attribute task_options.



828
829
830
# File 'lib/skynet/job.rb', line 828

def task_options
  @task_options
end

Instance Method Details

#[](index) ⇒ Object



851
852
853
854
855
856
857
# File 'lib/skynet/job.rb', line 851

def [](index)
  if data.respond_to?(:[])
    Skynet::Task.new(task_options.merge(:data => data[index], :task_id => get_unique_id(1).to_i))
  else
    raise Error.new("#{data.class} does not implement '[]'")
  end
end

#eachObject



867
868
869
870
871
872
873
874
875
876
877
878
879
880
# File 'lib/skynet/job.rb', line 867

def each     
  iteration = 0        
  data.send(each_method) do |task_data|
    task = nil
    if @first and iteration == 0
      task = @first
    else
      task = Skynet::Task.new(task_options.merge(:data => task_data, :task_id => (get_unique_id(1).to_i)))
      @first = task if iteration == 0
    end
    iteration += 1
    yield task
  end
end

#each_methodObject



859
860
861
# File 'lib/skynet/job.rb', line 859

def each_method
  each_method = data.respond_to?(:next) ? :next : :each        
end

#firstObject



835
836
837
838
839
840
841
# File 'lib/skynet/job.rb', line 835

def first         
  if data.respond_to?(:first)
    @first ||= Skynet::Task.new(task_options.merge(:data => data.first, :task_id => get_unique_id(1).to_i))        
  else
    raise Error.new("#{data.class} does not implement 'first'")
  end
end

#sizeObject



843
844
845
846
847
848
849
# File 'lib/skynet/job.rb', line 843

def size         
  if data.respond_to?(:size)
    data.size
  else
    raise Error.new("#{data.class} does not implement 'size'")
  end
end

#to_aObject



863
864
865
# File 'lib/skynet/job.rb', line 863

def to_a
  self.collect { |task| task }
end