Class: Thimble::Thimble

Inherits:
ThimbleQueue show all
Defined in:
lib/thimble.rb

Instance Attribute Summary

Attributes inherited from ThimbleQueue

#size

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ThimbleQueue

#+, #close, #closed?, #each, #length, #next, #push, #push_flat, #set_logger, #to_a

Constructor Details

#initialize(array, manager = Manager.new, result = nil, name = 'Main') ⇒ Thimble

Returns a new instance of Thimble.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/thimble.rb', line 12

def initialize(array, manager = Manager.new, result = nil, name = 'Main')
  raise ArgumentError, 'You need to pass a manager to Thimble!' unless manager.instance_of?(Manager)

  unless array.respond_to? :each
    raise ArgumentError,
          'There needs to be an iterable object passed to Thimble to start.'
  end

  @result = if result.nil?
              ThimbleQueue.new(array.size, 'Result')
            else
              result
            end
  unless @result.instance_of?(ThimbleQueue) && !@result.closed?
    raise ArgumentError,
          'result needs to be an open ThimbleQueue'
  end

  @manager = manager
  @running = true
  super(array.size, name)
  @logger.debug("loading thimble #{name}")
  array.each { |item| push(item) }
  @logger.debug("finished loading thimble #{name}")
  close
end

Class Method Details

.async(&block) ⇒ Thread

Will perform anything handed to this asynchronously. Requires a block

Returns:

  • (Thread)


69
70
71
# File 'lib/thimble.rb', line 69

def self.async(&block)
  Thread.new(&block)
end

Instance Method Details

#map(&block) ⇒ ThimbleQueue

This will use the manager and transform your thimble queue. requires a block

Returns:



42
43
44
45
46
47
48
49
# File 'lib/thimble.rb', line 42

def map(&block)
  @logger.debug("starting map in #{@name} with id #{Thread.current.object_id}")
  @running = true
  manage_workers(&block) while @running
  @result.close
  @logger.debug("finishing map in #{@name} with id #{Thread.current.object_id}")
  @result
end

#map_async(&block) ⇒ ThimbleQueue

This will use the manager and transform the thimble queue asynchronously. Will return the result instantly, so you can use it for next stage processing. requires a block

Parameters:

  • block (Proc)

Returns:



56
57
58
59
60
61
62
63
64
# File 'lib/thimble.rb', line 56

def map_async(&block)
  @logger.debug("starting async map in #{@name} with id #{Thread.current.object_id}")
  @logger.debug("queue: #{@queue}")
  Thimble.async do
    map(&block)
  end
  @logger.debug("finished async map in #{@name} with id #{Thread.current.object_id}")
  @result
end