Sha256: 25ce0770c39120febafc5c40e1fe0362a2856fb57972d4b397492bdceb010caf

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 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

      # @param target [Object] target object to which we proxy any DSL call
      # @param block [Proc] block that we want to evaluate 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
      # @param method_name [Symbol] name of the missing method
      def method_missing(method_name, ...)
        return super unless respond_to_missing?(method_name)

        if @target.respond_to?(:"#{method_name}=")
          @target.public_send(:"#{method_name}=", ...)
        else
          @target.public_send(method_name, ...)
        end
      end

      # Tells whether or not a given element exists on the target
      # @param method_name [Symbol] name of the missing method
      # @param include_private [Boolean] should we include private in the check as well
      def respond_to_missing?(method_name, include_private = false)
        @target.respond_to?(:"#{method_name}=", include_private) ||
          @target.respond_to?(method_name, include_private) ||
          super
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
karafka-2.0.10 lib/karafka/routing/proxy.rb
karafka-2.0.9 lib/karafka/routing/proxy.rb
karafka-2.0.8 lib/karafka/routing/proxy.rb
karafka-2.0.7 lib/karafka/routing/proxy.rb