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.

Constant Summary collapse

IGNORED_POSTFIXES =

We should proxy only non ? and = methods as we want to have a regular dsl

%w[
  ?
  =
  !
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) { ... } ⇒ Proxy

Returns a new instance of Proxy.

Parameters:

  • target (Object)

    target object to which we proxy any DSL call

Yields:

  • Evaluates block in the proxy context



19
20
21
22
# File 'lib/karafka/routing/proxy.rb', line 19

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



25
26
27
28
# File 'lib/karafka/routing/proxy.rb', line 25

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

Returns:

  • (Boolean)


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

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