diff options
Diffstat (limited to 'bocelli')
-rw-r--r-- | bocelli/base.rb | 22 | ||||
-rw-r--r-- | bocelli/core/router.rb | 28 | ||||
-rw-r--r-- | bocelli/module/base.rb | 25 |
3 files changed, 36 insertions, 39 deletions
diff --git a/bocelli/base.rb b/bocelli/base.rb index bc7e617..1b4ffa9 100644 --- a/bocelli/base.rb +++ b/bocelli/base.rb @@ -1,4 +1,5 @@ require_relative 'core/irc' +require_relative 'core/router' module Bocelli class Base @@ -21,9 +22,11 @@ module Bocelli class << self include Bocelli::Core::IRC + include Bocelli::Core::Router def setup - @routes = {} + setup_router + @modules = {} end @@ -33,27 +36,10 @@ module Bocelli subclass.setup end - def on(route, &block) - @routes[route] = block - end - def register(mod) @modules[mod.name[/[^:]+$/].downcase.intern] ||= mod end - def match?(str, route) - case route - when Regexp - str =~ route - when String - str == route - 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]) diff --git a/bocelli/core/router.rb b/bocelli/core/router.rb new file mode 100644 index 0000000..16b4371 --- /dev/null +++ b/bocelli/core/router.rb @@ -0,0 +1,28 @@ +module Bocelli + module Core + module Router + def setup_router + @routes = {} + end + + def on(route, &block) + raise 'no block given' if block.nil? + + @routes[route] = block + end + + def match?(str, route) + case route + when Regexp + str =~ route + when String + str == route + end + end + + def match(str) + @routes.detect { |k, _| match?(str, k) } + end + end + end +end diff --git a/bocelli/module/base.rb b/bocelli/module/base.rb index 45b09d7..f338c34 100644 --- a/bocelli/module/base.rb +++ b/bocelli/module/base.rb @@ -1,29 +1,12 @@ +require_relative '../core/router' + module Bocelli module Module module Base - attr_reader :routes + include Bocelli::Core::Router def setup - @routes = {} - end - - def on(route, &block) - raise 'no block given' if block.nil? - - @routes[route] = block - end - - def match?(str, route) - case route - when Regexp - str =~ route - when String - str == route - end - end - - def match(str) - @routes.detect { |k, _| match?(str, k) } + setup_router end class << self |