Class: EM::Sequence
- Inherits:
-
Object
- Object
- EM::Sequence
- Defined in:
- lib/em-sequence.rb,
lib/em-sequence/block.rb,
lib/em-sequence/method.rb
Overview
EventMachine sequence runner.
Defined Under Namespace
Instance Attribute Summary collapse
-
#last ⇒ Object
Returns last run sequence item result.
-
#stack ⇒ Array
Holds required calls stack.
-
#target ⇒ Object
Holds target instance of the sequencer.
-
#vars ⇒ Hash
Holds array of variables available and defined during sequence run.
Class Method Summary collapse
-
.run(target, &block) ⇒ Object
Declares and runs specified block in one call.
Instance Method Summary collapse
-
#block(*args, &block) ⇒ Object
(also: #b, #blk)
Declares block.
-
#declare(&block) ⇒ Sequence
(also: #decl)
Receives the sequence declaration.
-
#initialize(target) ⇒ Sequence
constructor
Constructor.
-
#method_missing(name, *args, &block) ⇒ Object
Handles method definitions in sequence declaration.
-
#run!(&callback) ⇒ Object
Runs the sequence.
-
#variable(name, value) ⇒ Object
(also: #var)
Declares variable.
Constructor Details
#initialize(target) ⇒ Sequence
Constructor.
60 61 62 63 64 |
# File 'lib/em-sequence.rb', line 60 def initialize(target) @target = target @stack = [ ] @vars = { } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
Handles method definitions in sequence declaration.
75 76 77 |
# File 'lib/em-sequence.rb', line 75 def method_missing(name, *args, &block) @stack << Method::new(@target, name, args, block) end |
Instance Attribute Details
#last ⇒ Object
Returns last run sequence item result.
52 53 54 |
# File 'lib/em-sequence.rb', line 52 def last @last end |
#stack ⇒ Array
Holds required calls stack. (So in fact sequence itself.)
34 35 36 |
# File 'lib/em-sequence.rb', line 34 def stack @stack end |
#target ⇒ Object
Holds target instance of the sequencer.
26 27 28 |
# File 'lib/em-sequence.rb', line 26 def target @target end |
#vars ⇒ Hash
Holds array of variables available and defined during sequence run.
44 45 46 |
# File 'lib/em-sequence.rb', line 44 def vars @vars end |
Class Method Details
.run(target, &block) ⇒ Object
Declares and runs specified block in one call. Useful if you don’t expect any results.
177 178 179 |
# File 'lib/em-sequence.rb', line 177 def self.run(target, &block) self::new(target).declare(&block).run! end |
Instance Method Details
#block(*args, &block) ⇒ Object Also known as: b, blk
Declares block.
Given block must return Hash with variable names and values. If block is last item of the sequence, return value will be used as return value of the sequence.
136 137 138 |
# File 'lib/em-sequence.rb', line 136 def block(*args, &block) @stack << Block::new(block, args) end |
#declare(&block) ⇒ Sequence Also known as: decl
Receives the sequence declaration.
106 107 108 109 |
# File 'lib/em-sequence.rb', line 106 def declare(&block) self.instance_eval(&block) return self end |
#run!(&callback) ⇒ Object
Runs the sequence.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/em-sequence.rb', line 150 def run!(&callback) worker = Proc::new do if not @stack.empty? @stack.shift.call(@vars) do |result, returning| @vars.merge! result @last = returning EM::next_tick { worker.call() } end elsif not callback.nil? EM::next_tick { callback.call(@last) } end end worker.call() end |
#variable(name, value) ⇒ Object Also known as: var
Declares variable.
120 121 122 |
# File 'lib/em-sequence.rb', line 120 def variable(name, value) @vars[name] = value end |