From 597840b750de571ee87dd34806fe5f188f45d667 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 18 Aug 2014 22:29:55 -0700 Subject: Cleanup and less duplication. --- bocelli/base.rb | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/bocelli/base.rb b/bocelli/base.rb index 990a819..bc7e617 100644 --- a/bocelli/base.rb +++ b/bocelli/base.rb @@ -34,8 +34,6 @@ module Bocelli end def on(route, &block) - name = "route #{route.inspect}" - @routes[route] = block end @@ -43,7 +41,7 @@ module Bocelli @modules[mod.name[/[^:]+$/].downcase.intern] ||= mod end - def match(str, route) + def match?(str, route) case route when Regexp str =~ route @@ -52,6 +50,18 @@ module Bocelli end end + def match(str) + @routes.detect { |k, _| match?(str, k) } + end + + def mod_match(str) + if str =~ /\A(\S+) (.*)/ + if (mod = Hash[@modules.map { |k, v| [k.to_s, v] }][$1]) + mod.match($2) + end + end + end + def process(str) if str =~ /\A:?(\S+) PRIVMSG (\S+) :?(.*)/ metadata = { @@ -60,17 +70,7 @@ module Bocelli message: $3 } - if $3 =~ /\A(\S+) (.*)/ - if (mod = Hash[@modules.map { |k, v| [k.to_s, v] }][$1]) - if (match = mod.match($2)) - route, block = match - - return new(str, route, block, metadata).execute - end - end - end - - if (match = @routes.detect { |k, _| match(metadata[:message], k) }) + if (match = match($3)) || (match = mod_match($3)) route, block = match new(str, route, block, metadata).execute -- cgit v1.2.3-70-g09d2