Class: Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/deimos/ext/routing_defaults.rb

Overview

This monkey patch was provided by Maciej, the maintainer of Karafka. This allows configs to override each other on a more granular basis rather than each ‘configure` call blowing away all fields. It also supports multiple default blocks.

Unfortunately this can’t be merged into Karafka as of now because it will be a major breaking change. As a compromise, it has been added to the test coverage of Karafka to ensure that other changes don’t break this. github.com/karafka/karafka/issues/2344

Instance Method Summary collapse

Constructor Details

#initializeMatcher

Returns a new instance of Matcher.



10
11
12
# File 'lib/deimos/ext/routing_defaults.rb', line 10

def initialize
  @applications = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, **kwargs) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/deimos/ext/routing_defaults.rb', line 48

def method_missing(m, *args, **kwargs)
  if args.empty?
    @applications << [m, kwargs]
  else
    @applications << [m, args]
  end
end

Instance Method Details

#replay_on(topic_node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/deimos/ext/routing_defaults.rb', line 14

def replay_on(topic_node)
  @applications.each do |method, kwargs|
    if method == :kafka
      topic_node.kafka = kwargs.is_a?(Array) ? kwargs[0] : kwargs
      next
    end
    if kwargs.is_a?(Hash)
      ref = topic_node.public_send(method)

      kwargs.each do |arg, val|
        if ref.respond_to?("#{arg}=")
          ref.public_send("#{arg}=", val)
        else
          if ref.respond_to?(:details)
            ref.details.merge!(kwargs)
          elsif ref.is_a?(Hash)
            ref.merge!(kwargs)
          else
            raise 'No idea if such case exists, if so, similar handling as config'
          end
        end
      end
    end

    if kwargs.is_a?(Array) && kwargs.size == 1
      if topic_node.respond_to?("#{method}=")
        topic_node.public_send(:"#{method}=", kwargs.first)
      else
        topic_node.public_send(method, *kwargs)
      end
    end
  end
end