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, defaults = ->(_) {}, &block) ⇒ Proxy

Returns a new instance of Proxy.

Parameters:

  • target (Object)

    target object to which we proxy any DSL call

  • defaults (Proc) (defaults to: ->(_) {})

    defaults for target that should be applicable after the proper proxy context (if needed)

  • 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



15
16
17
18
19
# File 'lib/karafka/routing/proxy.rb', line 15

def initialize(target, defaults = ->(_) {}, &block)
  @target = target
  instance_eval(&block) if block
  instance_eval(&defaults) if defaults
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object

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

Parameters:

  • method_name (Symbol)

    name of the missing method



23
24
25
26
27
28
29
30
31
# File 'lib/karafka/routing/proxy.rb', line 23

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

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)


36
37
38
39
40
# File 'lib/karafka/routing/proxy.rb', line 36

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