lib/cotton_tail/configuration.rb in cotton-tail-0.2.1 vs lib/cotton_tail/configuration.rb in cotton-tail-0.3.0
- old
+ new
@@ -1,17 +1,65 @@
# frozen_string_literal: true
+require 'middleware'
+
module CottonTail
# Configuration options
class Configuration
attr_reader :connection_args
def initialize
@connection_args = nil
+ @middleware = Middleware::DEFAULT_STACK
+ @user_configs = {}
end
+ # Sets the RabbitMQ connection params. Arguments are eventually passed
+ # to Bunny.new. Any valid params for Bunny.new are accepted.
+ #
+ # @see http://rubybunny.info/articles/connecting.html
def connection_args=(*args, **kwargs)
url, = args
@connection_args = url ? Bunny::Session.parse_uri(url) : kwargs
+ end
+
+ # Modify or retrieve the application middleware stack.
+ #
+ # @see https://github.com/Ibsciss/ruby-middleware
+ def middleware
+ return @middleware unless block_given?
+
+ @middleware = ::Middleware::Builder.new do |b|
+ b.use @middleware
+ yield b
+ end
+ end
+
+ def method_missing(method_id, *arguments, &block)
+ if user_config? method_id
+ @user_configs[method_id]
+ elsif setter?(method_id) && arguments.length == 1
+ @user_configs[getter_name(method_id)] = arguments.first
+ else
+ super
+ end
+ end
+
+ def respond_to_missing?(method_id, include_private = false)
+ user_config?(method_id) || super
+ end
+
+ private
+
+ def setter?(method_id)
+ method_id.to_s.end_with? '='
+ end
+
+ def user_config?(method_id)
+ @user_configs.key?(method_id)
+ end
+
+ def getter_name(setter)
+ setter.to_s.sub('=', '').to_sym
end
end
end