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.
61 62 63 64 65 |
# File 'lib/em-sequence.rb', line 61 def initialize(target) @target = target @stack = [ ] @vars = { } end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
Instance Attribute Details
#last ⇒ Object
Returns last run sequence item result.
53 54 55 |
# File 'lib/em-sequence.rb', line 53 def last @last end |
#stack ⇒ Array
Holds required calls stack. (So in fact sequence itself.)
35 36 37 |
# File 'lib/em-sequence.rb', line 35 def stack @stack end |
#target ⇒ Object
Holds target instance of the sequencer.
27 28 29 |
# File 'lib/em-sequence.rb', line 27 def target @target end |
#vars ⇒ Hash
Holds array of variables available and defined during sequence run.
45 46 47 |
# File 'lib/em-sequence.rb', line 45 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.
178 179 180 |
# File 'lib/em-sequence.rb', line 178 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.
137 138 139 |
# File 'lib/em-sequence.rb', line 137 def block(*args, &block) @stack << Block::new(block, args) end |
#declare(&block) ⇒ Sequence Also known as: decl
Receives the sequence declaration.
107 108 109 110 |
# File 'lib/em-sequence.rb', line 107 def declare(&block) self.instance_eval(&block) return self end |
#run!(&callback) ⇒ Object
Runs the sequence.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/em-sequence.rb', line 151 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.
121 122 123 |
# File 'lib/em-sequence.rb', line 121 def variable(name, value) @vars[name] = value end |