Class: SyncEnumerator

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

Overview

SyncEnumerator creates an Enumerable object from multiple Enumerable objects and enumerates them synchronously.

Example

require 'generator'

s = SyncEnumerator.new([1,2,3], ['a', 'b', 'c'])

# Yields [1, 'a'], [2, 'b'], and [3,'c']
s.each { |row| puts row.join(', ') }

Instance Method Summary collapse

Constructor Details

#initialize(*enums) ⇒ SyncEnumerator

Creates a new SyncEnumerator which enumerates rows of given Enumerable objects.



189
190
191
# File 'lib/s3sync/thread_generator.rb', line 189

def initialize(*enums)
  @gens = enums.map { |e| Generator.new(e) }
end

Instance Method Details

#eachObject

Enumerates rows of the Enumerable objects.



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/s3sync/thread_generator.rb', line 217

def each
  @gens.each { |g| g.rewind }

  loop do
    count = 0

    ret = @gens.map { |g|
	if g.end?
 count += 1
 nil
	else
 g.next
	end
    }

    if count == @gens.size
	break
    end

    yield ret
  end

  self
end

#end?(i = nil) ⇒ Boolean

Returns true if the given nth Enumerable object has reached the end. If no argument is given, returns true if any of the Enumerable objects has reached the end.

Returns:

  • (Boolean)


208
209
210
211
212
213
214
# File 'lib/s3sync/thread_generator.rb', line 208

def end?(i = nil)
  if i.nil?
    @gens.detect { |g| g.end? } ? true : false
  else
    @gens[i].end?
  end
end

#lengthObject

Returns the number of enumerated Enumerable objects, i.e. the size of each row.



201
202
203
# File 'lib/s3sync/thread_generator.rb', line 201

def length
  @gens.length
end

#sizeObject

Returns the number of enumerated Enumerable objects, i.e. the size of each row.



195
196
197
# File 'lib/s3sync/thread_generator.rb', line 195

def size
  @gens.size
end