Sha256: 21ec50b9ec12e31213d9c54b72f016140a595b95a3dcb27c43f416628042d069

Contents?: true

Size: 1.19 KB

Versions: 28

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module Karafka
  module Routing
    # Proxy is used as a translation layer in between the DSL and raw topic and consumer group
    # objects.
    class Proxy
      attr_reader :target

      # We should proxy only non ? and = methods as we want to have a regular dsl
      IGNORED_POSTFIXES = %w[
        ?
        =
        !
      ].freeze

      # @param target [Object] target object to which we proxy any DSL call
      # @yield Evaluates block in the proxy context
      def initialize(target, &block)
        @target = target
        instance_eval(&block)
      end

      # Translates the no "=" DSL of routing into elements assignments on target
      def method_missing(method_name, *arguments, &block)
        return super unless respond_to_missing?(method_name)
        @target.public_send(:"#{method_name}=", *arguments, &block)
      end

      # Tells whether or not a given element exists on the target
      def respond_to_missing?(method_name, include_private = false)
        return false if IGNORED_POSTFIXES.any? { |postfix| method_name.to_s.end_with?(postfix) }
        @target.respond_to?(:"#{method_name}=", include_private) || super
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
karafka-1.2.13 lib/karafka/routing/proxy.rb
karafka-1.2.12 lib/karafka/routing/proxy.rb
karafka-1.2.11 lib/karafka/routing/proxy.rb
karafka-1.2.10 lib/karafka/routing/proxy.rb
karafka-1.2.9 lib/karafka/routing/proxy.rb
karafka-1.2.8 lib/karafka/routing/proxy.rb
karafka-1.2.7 lib/karafka/routing/proxy.rb
karafka-1.2.6 lib/karafka/routing/proxy.rb
karafka-1.2.5 lib/karafka/routing/proxy.rb
karafka-1.2.4 lib/karafka/routing/proxy.rb
karafka-1.2.3 lib/karafka/routing/proxy.rb
karafka-1.2.2 lib/karafka/routing/proxy.rb
karafka-1.2.1 lib/karafka/routing/proxy.rb
karafka-1.2.0 lib/karafka/routing/proxy.rb
karafka-1.2.0.beta4 lib/karafka/routing/proxy.rb
karafka-1.2.0.beta3 lib/karafka/routing/proxy.rb
karafka-1.2.0.beta2 lib/karafka/routing/proxy.rb
karafka-1.2.0.beta1 lib/karafka/routing/proxy.rb
karafka-1.1.2 lib/karafka/routing/proxy.rb
karafka-1.1.1 lib/karafka/routing/proxy.rb