Class: ParallelEnumerable

Inherits:
Object
  • Object
show all
Defined in:
lib/parallel_enumerable.rb

Instance Method Summary collapse

Constructor Details

#initialize(enumerable) ⇒ ParallelEnumerable

Returns a new instance of ParallelEnumerable.



11
12
13
# File 'lib/parallel_enumerable.rb', line 11

def initialize(enumerable)
  @enumerable = enumerable
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



45
46
47
48
49
50
# File 'lib/parallel_enumerable.rb', line 45

def method_missing(method_name, *args, &block)
  return super unless enumerable.respond_to?(method_name)

  $stderr.puts "[parallel-enumerable] ##{method_name} is not defined"
  enumerable.public_send(method_name, *args, &block)
end

Instance Method Details

#eachObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/parallel_enumerable.rb', line 17

def each
  enumerable.map do |item|
    Thread.new do
      begin
        yield item
      rescue Exception # rescues StandardError by default; but we want to rescue and report all errors
        Houston.report_exception $!
      ensure
        ActiveRecord::Base.clear_active_connections!
      end
    end
  end.each(&:join)
end

#mapObject



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

def map
  queue = Queue.new

  each do |item|
    queue << yield(item)
  end

  [].tap do |results|
    results.push queue.pop until queue.empty?
  end
end