lib/lita.rb in lita-3.1.0 vs lib/lita.rb in lita-3.2.0
- old
+ new
@@ -24,31 +24,10 @@
# @return [Hash] A map of adapter keys to adapter classes.
def adapters
@adapters ||= {}
end
- # Adds an adapter to the global registry under the provided key.
- # @param key [String, Symbol] The key that identifies the adapter.
- # @param adapter [Lita::Adapter] The adapter class.
- # @return [void]
- def register_adapter(key, adapter)
- adapters[key.to_sym] = adapter
- end
-
- # The global registry of handlers.
- # @return [Set] The set of handlers.
- def handlers
- @handlers ||= Set.new
- end
-
- # Adds a handler to the global registry.
- # @param handler [Lita::Handler] The handler class.
- # @return [void]
- def register_handler(handler)
- handlers << handler
- end
-
# The global configuration object. Provides user settings for the robot.
# @return [Lita::Config] The Lita configuration object.
def config
@config ||= Config.default_config
end
@@ -59,17 +38,23 @@
# @return [void]
def configure
yield config
end
- # Clears the global configuration object. The next call to {Lita.config}
- # will create a fresh config object.
- # @return [void]
- def clear_config
- @config = nil
+ # The global registry of handlers.
+ # @return [Set] The set of handlers.
+ def handlers
+ @handlers ||= Set.new
end
+ # The global registry of hook handler objects.
+ # @return [Hash] A hash mapping hook names to sets of objects that handle them.
+ # @since 3.2.0
+ def hooks
+ @hooks ||= Hash.new { |h, k| h[k] = Set.new }
+ end
+
# The global Logger object.
# @return [::Logger] The global Logger object.
def logger
@logger ||= Logger.get_logger(Lita.config.robot.log_level)
end
@@ -81,14 +66,76 @@
redis = Redis.new(config.redis)
Redis::Namespace.new(REDIS_NAMESPACE, redis: redis)
end
end
+ # Adds an adapter to the global registry under the provided key.
+ # @param key [String, Symbol] The key that identifies the adapter.
+ # @param adapter [Lita::Adapter] The adapter class.
+ # @return [void]
+ def register_adapter(key, adapter)
+ adapters[key.to_sym] = adapter
+ end
+
+ # Adds a handler to the global registry.
+ # @param handler [Lita::Handler] The handler class.
+ # @return [void]
+ def register_handler(handler)
+ handlers << handler
+ end
+
+ # Adds a hook handler object to the global registry for the given hook.
+ # @return [void]
+ # @since 3.2.0
+ def register_hook(name, hook)
+ hooks[name.to_s.downcase.strip.to_sym] << hook
+ end
+
+ # Clears the global configuration object and the global adapter, handler, and hook registries.
+ # @return [void]
+ # @since 3.2.0
+ def reset
+ reset_adapters
+ reset_config
+ reset_handlers
+ reset_hooks
+ end
+
+ # Resets the global adapter registry, removing all registered adapters.
+ # @return [void]
+ # @since 3.2.0
+ def reset_adapters
+ @adapters = nil
+ end
+
+ # Resets the global configuration object. The next call to {Lita.config}
+ # will create a fresh config object.
+ # @return [void]
+ def reset_config
+ @config = nil
+ end
+ alias_method :clear_config, :reset_config
+
+ # Resets the global handler registry, removing all registered handlers.
+ # @return [void]
+ # @since 3.2.0
+ def reset_handlers
+ @handlers = nil
+ end
+
+ # Resets the global hooks registry, removing all registered hook handlers.
+ # @return [void]
+ # @since 3.2.0
+ def reset_hooks
+ @hooks = nil
+ end
+
# Loads user configuration and starts the robot.
# @param config_path [String] The path to the user configuration file.
# @return [void]
def run(config_path = nil)
+ hooks[:before_run].each { |hook| hook.call(config_path: config_path) }
Config.load_user_config(config_path)
Lita.config.finalize
self.locale = Lita.config.robot.locale
Robot.new.run
end
@@ -110,9 +157,10 @@
require_relative "lita/timer"
require_relative "lita/robot"
require_relative "lita/adapter"
require_relative "lita/adapters/shell"
require_relative "lita/handler"
+require_relative "lita/route_validator"
require_relative "lita/handlers/authorization"
require_relative "lita/handlers/help"
require_relative "lita/handlers/info"
require_relative "lita/handlers/room"