Methods to add to including classes
Install the given block
as an instance method of the receiver, using the given pattern
to derive the name, and return it as an UnboundMethod object.
# File lib/symphony/routing.rb, line 49
def make_handler_method( pattern, &block )
methname = self.make_handler_method_name( pattern, block )
self.log.info "Setting up #%s as a handler for %s" % [ methname, pattern ]
define_method( methname, &block )
return self.instance_method( methname )
end
Return the name of the method that the given block
should be installed as, derived from the specified pattern
.
# File lib/symphony/routing.rb, line 59
def make_handler_method_name( pattern, block )
_, line = block.source_location
pattern = pattern.
gsub( /#/, 'hash' ).
gsub( /\*/, 'star' ).
gsub( /\./, '_' )
return "on_%s_%d" % [ pattern, line ]
end
Return a regular expression that will match messages matching the given routing_key
.
# File lib/symphony/routing.rb, line 72
def make_routing_pattern( routing_key )
re_string = Regexp.escape( routing_key ).
gsub( /\\\*/, '[^\.]*' ).
gsub( /\\.\\#/, '(\..*)?' ).
gsub( /\\#\\./, '(.*\.)?' )
return Regexp.compile( '^' + re_string + '$' )
end
Register an event pattern and a block to execute when an event matching that pattern is received.
# File lib/symphony/routing.rb, line 32
def on( *route_patterns, &block )
raise LocalJumpError, "no block given" unless block
options = route_patterns.pop if route_patterns.last.is_a?( Hash )
route_patterns.each do |pattern|
methodobj = self.make_handler_method( pattern, &block )
self.routing_keys << pattern
pattern_re = self.make_routing_pattern( pattern )
self.routes[ pattern_re ] << methodobj
self.route_options[ pattern ].merge!( options ) if options
end
end