lib/lita/adapter.rb in lita-3.3.1 vs lib/lita/adapter.rb in lita-4.0.0.rc1

- old
+ new

@@ -1,32 +1,29 @@ module Lita # Adapters are the glue between Lita's API and a chat service. class Adapter + extend Namespace + extend Configurable + # The instance of {Lita::Robot}. # @return [Lita::Robot] attr_reader :robot class << self + # @!attribute [r] required_configs # A list of configuration keys that are required for the adapter to boot. # @return [Array] - attr_reader :required_configs - - # The namespace for the adapter, used for registry and for I18n. If the handler is an - # anonymous class, it must explicitly define +self.name+. - # @return [String] The adapter's namespace. - # @raise [RuntimeError] If +self.name+ is not defined. - def namespace - if name - Util.underscore(name.split("::").last) - else - raise I18n.t("lita.adapter.name_required") - end + # @deprecated Will be removed in Lita 5.0. Use {Lita::Adapter#configuration} instead. + def required_configs + Lita.logger.warn(I18n.t("lita.adapter.required_configs_deprecated")) + @required_configs end # Defines configuration keys that are requried for the adapter to boot. # @param keys [String, Symbol] The required keys. # @return [void] + # @deprecated Will be removed in Lita 5.0. Use {Lita::Adapter#config} instead. def require_config(*keys) @required_configs ||= [] @required_configs.concat(keys.flatten.map(&:to_sym)) end @@ -46,10 +43,17 @@ # @param robot [Lita::Robot] The currently running robot. def initialize(robot) @robot = robot ensure_required_configs end + # + # The handler's config object. + # @return [Lita::Configuration] The adapter's configuration object. + # @since 4.0.0 + def config + robot.config.adapters.public_send(self.class.namespace) + end # @!method join # Joins the room with the specified ID. # @param room_id [String] The ID of the room. # @return [void] @@ -111,23 +115,40 @@ alias_method :t, :translate private + # Returns the object used for the adapter's config. + def adapter_config + if Lita.version_3_compatibility_mode? + Lita.config.adapter + else + robot.config.adapter + end + end + # Logs a fatal message and aborts if a required config key is not set. def ensure_required_configs - required_configs = self.class.required_configs return if required_configs.nil? - missing_keys = [] + Lita.logger.warn(I18n.t("lita.adapter.require_config_deprecated")) - required_configs.each do |key| - missing_keys << key unless Lita.config.adapter[key] - end + missing_keys = missing_config_keys unless missing_keys.empty? Lita.logger.fatal(I18n.t("lita.adapter.missing_configs", configs: missing_keys.join(", "))) abort end + end + + # Finds all missing config keys. + def missing_config_keys + required_configs.select do |key| + key unless adapter_config[key] + end + end + + def required_configs + self.class.instance_variable_get(:@required_configs) end end end