Sha256: 609256936be4c89a484b1bfa9f2e7792ee437ea368edf5d4c74fb4e2dda5349c

Contents?: true

Size: 1.53 KB

Versions: 1

Compression:

Stored size: 1.53 KB

Contents

# 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cotton-tail-0.3.0 lib/cotton_tail/configuration.rb