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



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

#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



76
77
78
# File 'lib/em-sequence.rb', line 76

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



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

def last
  @last
end

#stackArray

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

Returns:

  • (Array)


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

def stack
  @stack
end

#targetObject

Holds target instance of the sequencer.

Returns:

  • (Object)


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

def target
  @target
end

#varsHash

Holds array of variables available and defined during sequence run.

Returns:

  • (Hash)


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.

Parameters:

  • target (Object)

    target instance for the sequence

  • block (Proc)

    sequence declaration

See Also:

Since:

  • 0.1.1



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.

Parameters:

  • args (Array)

    array of arguments

  • block (Proc)

    body of block



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.

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:



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.

Parameters:

  • callback (Proc)

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

Parameters:

  • name (Symbol)

    name of the variable

  • value (Object)

    value of the variable



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

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