Class: EM::Sequence

Inherits:
Object
  • Object
show all
Defined in:
lib/em-sequence.rb,
lib/em-sequence/block.rb,
lib/em-sequence/method.rb

Overview

EventMachine sequence runner.

Defined Under Namespace

Classes: Block, Method

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Sequence

Constructor.

Parameters:

  • target (Object)

    target instance for the sequence



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.

Parameters:

  • name (Symbol)

    method name

  • args (Array)

    input variables specification

  • block (Proc)

    block which should return array of returned variables names



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

#lastObject

Returns last run sequence item result.

Returns:

  • Object



52
53
54
# File 'lib/em-sequence.rb', line 52

def last
  @last
end

#stackArray

Holds required calls stack. (So in fact sequence itself.)

Returns:

  • (Array)


34
35
36
# File 'lib/em-sequence.rb', line 34

def stack
  @stack
end

#targetObject

Holds target instance of the sequencer.

Returns:

  • (Object)


26
27
28
# File 'lib/em-sequence.rb', line 26

def target
  @target
end

#varsHash

Holds array of variables available and defined during sequence run.

Returns:

  • (Hash)


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.

Parameters:

  • target (Object)

    target instance for the sequence

  • block (Proc)

    sequence declaration

See Also:

Since:

  • 0.1.1



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.

Parameters:

  • args (Array)

    array of arguments

  • block (Proc)

    body of block



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.

Examples:

bar.declare do

    # variable declaration
    variable :var, 3

    # method call declaration
    some_method(:var) { [:x, :y] }

    # inline block declaration and definition
    block(:x, :y) do |x, y|
        {:x => x + 1, :y => y + 1}
    end

    # some other methods
    other_method(:x, :y) { :x }
    another_method(:x)

end 

Parameters:

  • block (Proc)

    sequence declaration

Returns:



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.

Parameters:

  • callback (Proc)

    callback for giving back result of lat item of 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.

Parameters:

  • name (Symbol)

    name of the variable

  • value (Object)

    value of the variable



120
121
122
# File 'lib/em-sequence.rb', line 120

def variable(name, value)
    @vars[name] = value
end