Class: ThreadParty
- Inherits:
-
Object
- Object
- ThreadParty
- Defined in:
- lib/threadparty.rb
Overview
Handles the connection between PartyProxy classes, and how data is passed between them.
Thanks to Gabe Berke-Williams and his article at robots.thoughtbot.com/writing-a-domain-specific-language-in-ruby for providing the examples and groundwork.
Instance Attribute Summary collapse
-
#to_process ⇒ Object
A list of PartyProxies that need to happen.
Instance Method Summary collapse
-
#add(&block) ⇒ Object
Append a series PartyProxies to the to_process list.
-
#initialize(&block) ⇒ ThreadParty
constructor
A new instance of ThreadParty.
-
#iteratively ⇒ Object
(also: #polka)
Perform all XYZ using the products of X to call Y to call Z, chaining and iterating on the resultes.
-
#pooled ⇒ Object
Perform XYZ in different threads for maximum threadability.
-
#sequentially ⇒ Object
(also: #conga)
Perform XYZ one after another, returning nothing.
Constructor Details
#initialize(&block) ⇒ ThreadParty
Returns a new instance of ThreadParty.
17 18 19 20 21 |
# File 'lib/threadparty.rb', line 17 def initialize(&block) @to_process = Array.new add(&block) if block self end |
Instance Attribute Details
#to_process ⇒ Object
A list of PartyProxies that need to happen
15 16 17 |
# File 'lib/threadparty.rb', line 15 def to_process @to_process end |
Instance Method Details
#add(&block) ⇒ Object
Append a series PartyProxies to the to_process list.
25 26 27 28 |
# File 'lib/threadparty.rb', line 25 def add(&block) proxy = ThreadPartyProxy.new(self) proxy.instance_eval(&block) end |
#iteratively ⇒ Object Also known as: polka
Perform all XYZ using the products of X to call Y to call Z, chaining and iterating on the resultes.
Example:
ThreadParty.new do
ProcessQueue do
queue s3_objects
perform do |obj|
download(obj)
end
end
ProcessQueue do
perform fiddle_method
end
ProcessQueue do
#implicit "queue downloaded_and_fiddled_s3_objects"
perform do |obj|
upload_after_fiddle(obj)
end
end
end
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/threadparty.rb', line 81 def iteratively #lets get the party rolling result = @to_process.first.execute() #Now after the party is started, #do everything else. @to_process[1..-1].each do |party| #If a PartyProxy doesn't implement :queue, #it's not a party we want to give our innards to. if party.respond_to?(:queue) result = party.execute(result) else result = party.execute() end end result end |
#pooled ⇒ Object
Perform XYZ in different threads for maximum threadability.
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/threadparty.rb', line 43 def pooled #WHY NOT USE OURSELF? #BWAHAHAHAHAHA ThreadParty.new do ProcessQueue do collection to_process threads to_process.length perform do |partyproxy| partyproxy.execute() end end end.sequentially end |
#sequentially ⇒ Object Also known as: conga
Perform XYZ one after another, returning nothing.
32 33 34 35 36 37 38 |
# File 'lib/threadparty.rb', line 32 def sequentially result = [] @to_process.each do |party| result << party.execute end result.flatten end |