Module: Stepladder::Dsl
- Defined in:
- lib/stepladder/dsl.rb
Instance Method Summary collapse
- #batch_worker(options = {gathering: 1}, &block) ⇒ Object
- #filter_worker(argument = nil, &block) ⇒ Object
- #handoff(something) ⇒ Object
- #relay_worker(&block) ⇒ Object
- #side_worker(&block) ⇒ Object
- #source_worker(argument = nil, &block) ⇒ Object
Instance Method Details
#batch_worker(options = {gathering: 1}, &block) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/stepladder/dsl.rb', line 51 def batch_worker( = {gathering: 1}, &block) ensure_regular_arity(block) if block Worker.new.tap do |worker| worker.instance_variable_set(:@batch_size, [:gathering]) worker.instance_variable_set(:@batch_complete_block, block) def worker.task(value) if value @collection = [value] until batch_complete?(@collection.last) @collection << supplier.product end @collection.compact end end def worker.batch_complete?(value) return true if value.nil? if @batch_complete_block !! @batch_complete_block.call(value) else @collection.size >= @batch_size end end end end |
#filter_worker(argument = nil, &block) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/stepladder/dsl.rb', line 41 def filter_worker(argument=nil, &block) if (block && argument.respond_to?(:call)) throw_with 'You cannot supply two callables' end callable = argument.respond_to?(:call) ? argument : block ensure_callable(callable) Worker.new filter: block end |
#handoff(something) ⇒ Object
79 80 81 |
# File 'lib/stepladder/dsl.rb', line 79 def handoff(something) Fiber.yield something end |
#relay_worker(&block) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/stepladder/dsl.rb', line 23 def relay_worker(&block) ensure_regular_arity(block) Worker.new do |value| value && block.call(value) end end |
#side_worker(&block) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/stepladder/dsl.rb', line 31 def side_worker(&block) ensure_regular_arity(block) Worker.new do |value| value.tap do |v| v && block.call(v) end end end |
#source_worker(argument = nil, &block) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/stepladder/dsl.rb', line 5 def source_worker(argument=nil, &block) ensure_correct_arity_for!(argument, block) series = series_from(argument) callable = setup_callable_for(block, series) return Worker.new(&callable) if series.nil? Worker.new do series.each(&callable) while true do handoff nil end end end |