Class: Karafka::Routing::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/routing/proxy.rb

Overview

Proxy is used as a translation layer in between the DSL and raw topic and consumer group objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, &block) ⇒ Proxy

Returns a new instance of Proxy.

Parameters:

  • target (Object)

    target object to which we proxy any DSL call

  • block (Proc)

    block that we want to evaluate in the proxy context



21
22
23
24
# File 'lib/karafka/routing/proxy.rb', line 21

def initialize(target, &block)
  @target = target
  instance_eval(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Translates the no “=” DSL of routing into elements assignments on target

Parameters:

  • method_name (Symbol)

    name of the missing method

  • arguments (Array)

    array with it’s arguments

  • block (Proc)

    block provided to the method



30
31
32
33
34
# File 'lib/karafka/routing/proxy.rb', line 30

def method_missing(method_name, *arguments, &block)
  return super unless respond_to_missing?(method_name)

  @target.public_send(:"#{method_name}=", *arguments, &block)
end

Instance Attribute Details

#targetObject (readonly)

Returns the value of attribute target.



8
9
10
# File 'lib/karafka/routing/proxy.rb', line 8

def target
  @target
end

Instance Method Details

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Tells whether or not a given element exists on the target

Parameters:

  • method_name (Symbol)

    name of the missing method

  • include_private (Boolean) (defaults to: false)

    should we include private in the check as well

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/karafka/routing/proxy.rb', line 39

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