Class: Karafka::Helpers::ConfigImporter

Inherits:
Module
  • Object
show all
Defined in:
lib/karafka/helpers/config_importer.rb

Overview

Module allowing for configuration injections. By default injects whole app config Allows for granular config injection

Instance Method Summary collapse

Constructor Details

#initialize(attributes = { config: %i[itself] }) ⇒ ConfigImporter

Returns a new instance of ConfigImporter.

Parameters:

  • attributes (Hash<Symbol, Array<Symbol>>) (defaults to: { config: %i[itself] })

    map defining what we want to inject. The key is the name under which attribute will be visible and the value is the full path to the attribute



11
12
13
14
# File 'lib/karafka/helpers/config_importer.rb', line 11

def initialize(attributes = { config: %i[itself] })
  super()
  @attributes = attributes
end

Instance Method Details

#extended(model) ⇒ Object

Parameters:

  • model (Object)

    object to which we want to add the config fetcher on a class level



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/karafka/helpers/config_importer.rb', line 30

def extended(model)
  super

  @attributes.each do |name, path|
    model.class_eval <<~RUBY, __FILE__, __LINE__ + 1
      def self.#{name}
        @#{name} ||= ::Karafka::App.config.#{path.join('.')}
      end
    RUBY
  end
end

#included(model) ⇒ Object

Parameters:

  • model (Object)

    object to which we want to add the config fetcher



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/karafka/helpers/config_importer.rb', line 17

def included(model)
  super

  @attributes.each do |name, path|
    model.class_eval <<~RUBY, __FILE__, __LINE__ + 1
      def #{name}
        @#{name} ||= ::Karafka::App.config.#{path.join('.')}
      end
    RUBY
  end
end