Class: InThreads
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- InThreads
- Defined in:
- lib/in_threads.rb
Overview
Run Enumerable methods with blocks in threads
Defined Under Namespace
Constant Summary collapse
- DEPENDENT_BLOCK_CALLS =
%w[ inject reduce max min minmax sort ].map(&:to_sym)
- ENUMERATOR_RETURNED =
%w[ chunk chunk_while slice_before slice_after slice_when ].map(&:to_sym)
- BLOCKLESS_METHODS =
%w[ entries to_a drop take first include? member? lazy chain tally compact ].map(&:to_sym)
- INCOMPATIBLE_METHODS =
DEPENDENT_BLOCK_CALLS + ENUMERATOR_RETURNED + BLOCKLESS_METHODS
Instance Attribute Summary collapse
-
#enumerable ⇒ Object
readonly
Returns the value of attribute enumerable.
-
#thread_count ⇒ Object
readonly
Returns the value of attribute thread_count.
Class Method Summary collapse
- .new(enumerable, thread_count = 10, &block) ⇒ Object
-
.use(runner, options) ⇒ Object
Specify runner to use.
Instance Method Summary collapse
-
#grep(*args, &block) ⇒ Object
Special case method, works by applying ‘run_in_threads_use_block_result` with map on enumerable returned by blockless run.
-
#grep_v(*args, &block) ⇒ Object
Special case method, works by applying ‘run_in_threads_use_block_result` with map on enumerable returned by blockless run.
-
#in_threads(thread_count = 10, &block) ⇒ Object
Creates new instance using underlying enumerable and new thread_count.
-
#initialize(enumerable, thread_count = 10) ⇒ InThreads
constructor
A new instance of InThreads.
-
#with_progress(title = nil, length = nil, &block) ⇒ Object
befriend with progress gem.
Constructor Details
#initialize(enumerable, thread_count = 10) ⇒ InThreads
Returns a new instance of InThreads.
46 47 48 49 50 51 52 53 54 |
# File 'lib/in_threads.rb', line 46 def initialize(enumerable, thread_count = 10) super(enumerable) @enumerable, @thread_count = enumerable, thread_count.to_i fail ArgumentError, '`enumerable` should include Enumerable.' unless enumerable.is_a?(Enumerable) fail ArgumentError, '`thread_count` can\'t be less than 2.' if thread_count < 2 end |
Instance Attribute Details
#enumerable ⇒ Object (readonly)
Returns the value of attribute enumerable.
36 37 38 |
# File 'lib/in_threads.rb', line 36 def enumerable @enumerable end |
#thread_count ⇒ Object (readonly)
Returns the value of attribute thread_count.
36 37 38 |
# File 'lib/in_threads.rb', line 36 def thread_count @thread_count end |
Class Method Details
.new(enumerable, thread_count = 10, &block) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/in_threads.rb', line 38 def self.new(enumerable, thread_count = 10, &block) if block super.each(&block) else super end end |
.use(runner, options) ⇒ Object
Specify runner to use
use :run_in_threads_use_block_result, :for => %w[all? any? none? one?]
‘:for` is required `:ignore_undefined` ignores methods which are not present in `Enumerable.instance_methods`
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/in_threads.rb', line 69 def use(runner, ) methods = Array([:for]) fail 'no methods provided using :for option' if methods.empty? ignore_undefined = [:ignore_undefined] methods.each do |method| next if ignore_undefined && !enumerable_method?(method) class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{method}(*args, &block) # def foo_bar(*args, &block) if block # if block #{runner}(:#{method}, *args, &block) # run_in_threads_method(:foo_bar, *args, &block) else # else enumerable.#{method}(*args) # enumerable.foo_bar(*args) end # end end # end RUBY end end |
Instance Method Details
#grep(*args, &block) ⇒ Object
Special case method, works by applying ‘run_in_threads_use_block_result` with map on enumerable returned by blockless run
146 147 148 149 150 151 152 |
# File 'lib/in_threads.rb', line 146 def grep(*args, &block) if block self.class.new(enumerable.grep(*args), thread_count).map(&block) else enumerable.grep(*args) end end |
#grep_v(*args, &block) ⇒ Object
Special case method, works by applying ‘run_in_threads_use_block_result` with map on enumerable returned by blockless run
157 158 159 160 161 162 163 |
# File 'lib/in_threads.rb', line 157 def grep_v(*args, &block) if block self.class.new(enumerable.grep_v(*args), thread_count).map(&block) else enumerable.grep_v(*args) end end |
#in_threads(thread_count = 10, &block) ⇒ Object
Creates new instance using underlying enumerable and new thread_count
57 58 59 |
# File 'lib/in_threads.rb', line 57 def in_threads(thread_count = 10, &block) self.class.new(enumerable, thread_count, &block) end |
#with_progress(title = nil, length = nil, &block) ⇒ Object
befriend with progress gem
167 168 169 |
# File 'lib/in_threads.rb', line 167 def with_progress(title = nil, length = nil, &block) ::Progress::WithProgress.new(self, title, length, &block) end |