Module: EventMachine::UCEngine::Brick

Defined in:
lib/em-ucengine/brick.rb,
lib/em-ucengine/brick_test.rb

Overview

This module allows to create a new brick for UCE. A brick may be either an independent entity or a composition of several bricks, resulting in a “meta-brick”. On this case, only the meta-brick should be runned and only one connection to UCE will be used.

When creating a brick, one would provide a bootstrap block and declare at least one event handler for the brick to manage. Handlers will be executed in the context of the Brick instance, so one may also write arbitrary code to be used in handlers blocks.

See examples/bricks.rb and specs for more details.

Defined Under Namespace

Modules: ClassMethods, Test

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#uceObject (readonly)

Returns the value of attribute uce.



65
66
67
# File 'lib/em-ucengine/brick.rb', line 65

def uce
  @uce
end

Class Method Details

.included(klass) ⇒ Object



17
18
19
# File 'lib/em-ucengine/brick.rb', line 17

def self.included(klass)
  klass.extend(ClassMethods)
end

.run(config = {}) ⇒ Object

Start EventMachine, initialize and start the brick.

When composing several bricks, only the meta-brick should be runned.

Parameters:

  • config (Hash) (defaults to: {})


81
82
83
84
85
86
# File 'lib/em-ucengine/brick.rb', line 81

def self.run(config={})
  brick = self.new config
  EM::run do
    brick.start
  end
end

Instance Method Details

#after(n) { ... } ⇒ Object

Wrapper around EventMachine’s Timer. Pass it a block to be executed after a time range.

Parameters:

  • n (Integer)

    time range in milliseconds

Yields:



131
132
133
# File 'lib/em-ucengine/brick.rb', line 131

def after(n, &block)
  EventMachine::Timer.new(n, &block)
end

#bootstrapObject

Accessor for @@bootstrap.



112
113
114
# File 'lib/em-ucengine/brick.rb', line 112

def bootstrap
  self.class.class_variable_get("@@bootstrap")
end

#bricksObject

Accessor for @@bricks.



122
123
124
# File 'lib/em-ucengine/brick.rb', line 122

def bricks
  self.class.class_variable_get("@@bricks")
end

#every(n) { ... } ⇒ Object

Wrapper around EventMachine’s PeriodicTimer. Pass it a block to be executed on a defined time frame.

Parameters:

  • n (Integer)

    time range in milliseconds

Yields:



140
141
142
# File 'lib/em-ucengine/brick.rb', line 140

def every(n, &block)
  EventMachine::PeriodicTimer.new(n, &block)
end

#initialize(config = {}) ⇒ Object

Create a new brick.

Parameters:

  • config (Hash) (defaults to: {})


70
71
72
73
# File 'lib/em-ucengine/brick.rb', line 70

def initialize(config={})
  @uce = nil
  @config = config
end

#routesObject

Accessor for @@routes.



117
118
119
# File 'lib/em-ucengine/brick.rb', line 117

def routes
  self.class.class_variable_get("@@routes")
end

#start(uce = nil) ⇒ Object

Start the brick.

Parameters:

  • uce (EM::UCEngine::Client) (defaults to: nil)

    (nil) shared instance of the client, used by sub-bricks when composing so that only one UCE connection is made and used



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/em-ucengine/brick.rb', line 93

def start(uce=nil)
  # Not in a sub-brick, connect to UCE.
  if uce.nil?
    @uce = EM::UCEngine::Client.new(@config[:host], @config[:port])
    @uce.connect(@config[:name], @config[:credential]) do |err, uce|
      @uce = uce
      ready(bricks.map do |brick|
        b = brick.new
        b.start @uce
        b
      end)
    end
  else # Sub-brick, init with the shared UCE client instance.
    @uce = uce
    self.instance_eval &bootstrap
  end
end