Module: Shifty::DSL
- Defined in:
- lib/shifty/dsl.rb
Defined Under Namespace
Classes: BatchContext
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(mode = :normal, &block) ⇒ Object
- #source_worker(argument = nil, &block) ⇒ Object
- #splitter_worker(&block) ⇒ Object
- #trailing_worker(trail_length = 2) ⇒ Object
Instance Method Details
#batch_worker(options = {gathering: 1}, &block) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/shifty/dsl.rb', line 65 def batch_worker( = {gathering: 1}, &block) ensure_regular_arity(block) if block batch_full = block || proc { |_, batch| batch.size >= [:gathering] } batch_context = BatchContext.new({batch_full: batch_full}) Worker.new(context: batch_context) do |value, supply, context| if value context.collection = [value] until context.batch_complete?( context.collection.last, context.collection ) context.collection << supply.shift end context.collection.compact end end end |
#filter_worker(argument = nil, &block) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/shifty/dsl.rb', line 43 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 do |value, supply| while value && !callable.call(value) value = supply.shift end value end end |
#handoff(something) ⇒ Object
121 122 123 |
# File 'lib/shifty/dsl.rb', line 121 def handoff(something) Fiber.yield something end |
#relay_worker(&block) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/shifty/dsl.rb', line 22 def relay_worker(&block) ensure_regular_arity(block) Worker.new do |value| value && block.call(value) end end |
#side_worker(mode = :normal, &block) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/shifty/dsl.rb', line 30 def side_worker(mode = :normal, &block) ensure_regular_arity(block) Worker.new(tags: [:side_effect]) do |value| value.tap do |v| used_value = mode == :hardened ? Marshal.load(Marshal.dump(v)) : v v && block.call(used_value) 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 |
# File 'lib/shifty/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) loop do handoff nil end end end |
#splitter_worker(&block) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/shifty/dsl.rb', line 86 def splitter_worker(&block) ensure_regular_arity(block) Worker.new do |value| if value.nil? value else parts = [block.call(value)].flatten while parts.size > 1 handoff parts.shift end parts.shift end end end |
#trailing_worker(trail_length = 2) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/shifty/dsl.rb', line 102 def trailing_worker(trail_length = 2) trail = [] Worker.new do |value, supply| if value trail.unshift value if trail.size >= trail_length trail.pop end while trail.size < trail_length trail.unshift supply.shift end trail else value end end end |