lib/basquiat/adapters/base_adapter.rb in basquiat-1.2.0 vs lib/basquiat/adapters/base_adapter.rb in basquiat-1.3.0.pre.1

- old
+ new

@@ -6,56 +6,91 @@ # Base implementation for an adapter class Base using Basquiat::HashRefinements class << self + # A hash representing the registered requeue/acknowledge strategies + # @return [Hash] the registered RequeueStrategies def strategies @strategies ||= {} end + # Used to register a requeue/acknowledge strategy + # @param config_name [#to_sym] the named used on the config file for the Requeue Strategy + # @param klass [Class] the class name. def register_strategy(config_name, klass) - strategies.merge!(config_name.to_sym => klass) + strategies[config_name.to_sym] = klass end + + # Return the Strategy Class registered on key + # @param key [#to_sym] configured key for the wanted strategy + # @return [Class] return the strategy class + # @raise [Errors::StrategyNotRegistered] if it fails to find the key + def strategy(key) + strategies.fetch(key) + rescue KeyError + raise Basquiat::Errors::StrategyNotRegistered + end end - def initialize + # @param procs: [Object] + # It's a hash by default, but usually will be superseded by the adapter implementation + def initialize(procs: {}) @options = base_options - @procs = {} + @procs = procs @retries = 0 end + # Utility method to access the class instance variable def strategies self.class.strategies end - # Used to set the options for the adapter. It is merged in - # to the default_options hash. + # Allows the #base_options to be superseded on the local level + # + # You could have configured an exchange_name (on a config file) to +'awesome.sauce'+, + # but on this object you'd want to publish your messages to the +'killer.mailman'+ exchange. + # @example + # class Mailmail + # extend Basquiat::Base + # adapter_options {exchange: {name: 'killer.mailman'}} + # end + # # @param [Hash] opts an adapter dependant hash of options def adapter_options(opts) @options.deep_merge(opts) end - # Default options for the adapter - # @return [Hash] + # The default adapter options, merged with the {Basquiat::Configuration#adapter_options}. Used internally. + # @todo rename this method + # @return [Hash] the full options hash def base_options default_options.merge(Basquiat.configuration.adapter_options) end + # The adapter default options + # @return [Hash] def default_options {} end + # @!group Adapter specific implementations + # @abstract Publish an event to the event stream def publish fail Basquiat::Errors::SubclassResponsibility end + # @abstract subscribe_to the event stream def subscribe_to fail Basquiat::Errors::SubclassResponsibility end + # @abstract Disconnect from the message queue def disconnect fail Basquiat::Errors::SubclassResponsibility end + + # @!endgroup private attr_reader :procs, :options end