Module: Strelka::WebSocketServer::Routing::ClassMethods

Defined in:
lib/strelka/websocketserver/routing.rb

Overview

Class methods to add to classes with routing.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#op_callbacksObject (readonly)

The list of routes to pass to the Router when the application is created



63
64
65
# File 'lib/strelka/websocketserver/routing.rb', line 63

def op_callbacks
  @op_callbacks
end

#opcode_mapObject (readonly)

The Hash of opcode names to numeric opcodes



68
69
70
# File 'lib/strelka/websocketserver/routing.rb', line 68

def opcode_map
  @opcode_map
end

#opcode_namesObject (readonly)

The Hash of numeric opcodes to opcode names



73
74
75
# File 'lib/strelka/websocketserver/routing.rb', line 73

def opcode_names
  @opcode_names
end

Class Method Details

.extended(mod) ⇒ Object

Extension callback – install default opcode declaratives when the plugin is registered.



122
123
124
125
# File 'lib/strelka/websocketserver/routing.rb', line 122

def self::extended( mod )
  super
  mod.opcodes( Mongrel2::WebSocket::Constants::OPCODE )
end

Instance Method Details

#inherited(subclass) ⇒ Object

Inheritance hook – inheriting classes inherit their parents’ routes table.



113
114
115
116
117
# File 'lib/strelka/websocketserver/routing.rb', line 113

def inherited( subclass )
  super
  subclass.instance_variable_set( :@opcode_map, self.opcode_map.dup )
  subclass.instance_variable_set( :@op_callbacks, self.op_callbacks.dup )
end

#make_declarative(opcode) ⇒ Object

Make a declarative method for setting the callback for requests with frames with the specified opcode (Symbol).



101
102
103
104
105
106
107
108
109
# File 'lib/strelka/websocketserver/routing.rb', line 101

def make_declarative( opcode )
  self.log.debug "Making a declarative for %p" % [ opcode ]
  return lambda do |&block|
    self.log.debug "Setting handler for %p frames to %p" % [ opcode, block ]
    methodname = "on_#{opcode}_request"
    define_method( methodname, &block )
    self.op_callbacks[ opcode ] = self.instance_method( methodname )
  end
end

#opcodes(hash) ⇒ Object

Declare one or more opcodes in the form:

<label> => <bit>,



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/strelka/websocketserver/routing.rb', line 82

def opcodes( hash )
  @opcode_map ||= {}
  @opcode_map.merge!( hash )

  @opcode_names = @opcode_map.invert

  @opcode_map.each do |label, bit|
    self.log.debug "Set opcode %p to %#0x" % [ label, bit ]
    declarative = "on_#{label}"
    block = self.make_declarative( label )
    self.log.debug "  declaring method %p on %p" % [ declarative, self ]
    self.class.remove_method( declarative ) if self.class.method_defined?( declarative )
    self.class.define_method( declarative, &block )
  end
end