Module: Coolio::DSL

Defined in:
lib/cool.io/dsl.rb

Overview

A DSL for defining Cool.io connection types and servers

Defined Under Namespace

Classes: ConnectionBuilder

Class Method Summary collapse

Class Method Details

.[](connection_name) ⇒ Object

Look up a connection class by its name



70
71
72
73
74
75
76
77
78
# File 'lib/cool.io/dsl.rb', line 70

def [](connection_name)
  class_name = connection_name.to_s.split('_').map { |s| s.capitalize }.join

  begin
    Coolio::Connections.const_get class_name
  rescue NameError
    raise NameError, "No connection type registered for #{connection_name.inspect}"
  end
end

.connect(host, port, connection_name = nil, *initializer_args, &block) ⇒ Object

Connect to the given host and port using the given connection class



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cool.io/dsl.rb', line 22

def connect(host, port, connection_name = nil, *initializer_args, &block)
  if block_given?
    initializer_args.unshift connection_name if connection_name

    klass = Class.new Cool.io::TCPSocket
    connection_builder = ConnectionBuilder.new klass
    connection_builder.instance_eval(&block)
  else
    raise ArgumentError, "no connection name or block given" unless connection_name
    klass = self[connection_name]
  end

  client = klass.connect host, port, *initializer_args
  client.attach Cool.io::Loop.default
  client
end

.connection(name, &block) ⇒ Object

Create a new Cool.io::TCPSocket class



58
59
60
61
62
63
64
65
66
67
# File 'lib/cool.io/dsl.rb', line 58

def connection(name, &block)
  # Camelize class name
  class_name = name.to_s.split('_').map { |s| s.capitalize }.join

  connection = Class.new Cool.io::TCPSocket
  connection_builder = ConnectionBuilder.new connection
  connection_builder.instance_eval(&block)

  Coolio::Connections.const_set class_name, connection
end

.runObject

Run the default Cool.io event loop



17
18
19
# File 'lib/cool.io/dsl.rb', line 17

def run
  Cool.io::Loop.default.run
end

.server(host, port, connection_name = nil, *initializer_args, &block) ⇒ Object

Create a new Cool.io::TCPServer



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cool.io/dsl.rb', line 40

def server(host, port, connection_name = nil, *initializer_args, &block)
  if block_given?
    initializer_args.unshift connection_name if connection_name

    klass = Class.new Cool.io::TCPSocket
    connection_builder = ConnectionBuilder.new klass
    connection_builder.instance_eval(&block)
  else
    raise ArgumentError, "no connection name or block given" unless connection_name
    klass = self[connection_name]
  end

  server = Cool.io::TCPServer.new host, port, klass, *initializer_args
  server.attach Cool.io::Loop.default
  server
end