Sha256: 29de8856dca8ded09cc1b898a8506944ce3f0ce084bf81da4ef93c054f5b33d7

Contents?: true

Size: 1.54 KB

Versions: 6

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true

require 'middleware'

module CottonTail
  # Configuration options
  class Configuration
    attr_reader :connection_args

    def initialize(middleware: nil)
      @connection_args = nil
      @middleware = middleware
      @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 if @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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cotton-tail-0.7.0 lib/cotton_tail/configuration.rb
cotton-tail-0.6.1 lib/cotton_tail/configuration.rb
cotton-tail-0.6.0 lib/cotton_tail/configuration.rb
cotton-tail-0.5.0 lib/cotton_tail/configuration.rb
cotton-tail-0.4.1 lib/cotton_tail/configuration.rb
cotton-tail-0.4.0 lib/cotton_tail/configuration.rb