Permalink
Browse files

Move code into lib/

  • Loading branch information...
1 parent 6e0fc5b commit 3b09d62f2ca27f46b810f1d3dc74cbfd0fce0a9e @mloughran committed Oct 21, 2009
Showing with 51 additions and 33 deletions.
  1. +5 −0 lib/noodler.rb
  2. +46 −0 lib/noodler/node.rb
  3. +0 −33 node.rb
View
@@ -0,0 +1,5 @@
+module Noodler
+
+end
+
+require 'noodler/node'
View
@@ -0,0 +1,46 @@
+module Noodler
+ class Node
+ attr_accessor :parent
+
+ def initialize(type, strategy = nil, &strategy_block)
+ unless type == :sync || type == :async
+ raise ArgumentError, "type must be :sync or :async"
+ end
+ @synchronous = (type == :sync)
+ @children = []
+ @strategy = strategy || strategy_block
+ end
+
+ def <<(child)
+ child.parent = self
+ @children << child
+ end
+
+ def run
+ @synchronous ? run_sync : run_async
+ end
+
+ def run_async
+ deferrable = @strategy.call
+ deferrable.callback do
+ puts "Deferrable strategy succeeded"
+ EM.next_tick(method(:run_children))
+ end
+ deferrable.errback do
+ puts "Deferrable strategy failed"
+ end
+ end
+
+ def run_sync
+ EM.defer \
+ lambda { @strategy.call },
+ lambda { EM.next_tick(method(:run_children)) }
+ end
+
+ def run_children
+ @children.each do |child|
+ child.run
+ end
+ end
+ end
+end
View
33 node.rb
@@ -1,33 +0,0 @@
-require 'rubygems'
-require 'eventmachine'
-
-class Node
- attr_accessor :parent
-
- def initialize(&strategy)
- @children = []
- @strategy = strategy
- end
-
- def <<(child)
- child.parent = self
- @children << child
- end
-
- def run
- deferrable = @strategy.call
- deferrable.callback do
- puts "Strategy succeeded"
- EM.next_tick(method(:run_children))
- end
- deferrable.errback do
- puts "Strategy failed"
- end
- end
-
- def run_children
- @children.each do |child|
- child.run
- end
- end
-end

0 comments on commit 3b09d62

Please sign in to comment.