Module: Karafka::Helpers::ConfigRetriever

Included in:
Routing::ConsumerGroup, Routing::Topic
Defined in:
lib/karafka/helpers/config_retriever.rb

Overview

Note:

Worth noticing, that the value might be equal to false, so even then we need to return it. That’s why we check for nil?

A helper method that allows us to build methods that try to get a given attribute from its instance value and if it fails, will fallback to the default config or config.kafka value for a given attribute. It is used to simplify the checks. class Test

extend Karafka::Helpers::ConfigRetriever
config_retriever_for :start_from_beginning

end

Test.new.start_from_beginning #=> false test_instance = Test.new test_instance.start_from_beginning = true test_instance.start_from_beginning #=> true

Examples:

Define config retried attribute for start_from_beginning

Instance Method Summary collapse

Instance Method Details

#config_retriever_for(attribute) ⇒ Object

Builds proper methods for setting and retrieving (with fallback) given attribute value

Parameters:

  • attribute (Symbol)

    attribute name based on which we will build accessor with fallback



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/karafka/helpers/config_retriever.rb', line 25

def config_retriever_for(attribute)
  attr_writer attribute unless method_defined? :"#{attribute}="

  # Don't redefine if we already have accessor for a given element
  return if method_defined? attribute

  define_method attribute do
    current_value = instance_variable_get(:"@#{attribute}")
    return current_value unless current_value.nil?

    value = if Karafka::App.config.respond_to?(attribute)
              Karafka::App.config.send(attribute)
            else
              Karafka::App.config.kafka.send(attribute)
            end

    instance_variable_set(:"@#{attribute}", value)
  end
end