Sha256: e2abcb3e4a4c62683f8fd93471d8822066d4d8374c4be1605d520812edb954bb

Contents?: true

Size: 1.64 KB

Versions: 19

Compression:

Stored size: 1.64 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 defaults [Proc] defaults for target that should be applicable after the proper
      #   proxy context (if needed)
      # @param block [Proc, nil] block that we want to evaluate in the proxy context or nil if no
      #   proxy block context for example because whole context is taken from defaults
      def initialize(target, defaults = ->(_) {}, &block)
        @target = target
        instance_eval(&block) if block
        instance_eval(&defaults) if defaults
      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

19 entries across 19 versions & 1 rubygems

Version Path
karafka-2.4.17 lib/karafka/routing/proxy.rb
karafka-2.4.16 lib/karafka/routing/proxy.rb
karafka-2.4.15 lib/karafka/routing/proxy.rb
karafka-2.4.14 lib/karafka/routing/proxy.rb
karafka-2.4.13 lib/karafka/routing/proxy.rb
karafka-2.4.12 lib/karafka/routing/proxy.rb
karafka-2.4.11 lib/karafka/routing/proxy.rb
karafka-2.4.10 lib/karafka/routing/proxy.rb
karafka-2.4.9 lib/karafka/routing/proxy.rb
karafka-2.4.8 lib/karafka/routing/proxy.rb
karafka-2.4.7 lib/karafka/routing/proxy.rb
karafka-2.4.6 lib/karafka/routing/proxy.rb
karafka-2.4.5 lib/karafka/routing/proxy.rb
karafka-2.4.4 lib/karafka/routing/proxy.rb
karafka-2.4.3 lib/karafka/routing/proxy.rb
karafka-2.4.0 lib/karafka/routing/proxy.rb
karafka-2.4.0.rc1 lib/karafka/routing/proxy.rb
karafka-2.4.0.beta2 lib/karafka/routing/proxy.rb
karafka-2.4.0.beta1 lib/karafka/routing/proxy.rb