Module: Karafka::Params::Dsl

Defined in:
lib/karafka/params/dsl.rb

Overview

Dsl for Karafka params. We don’t provide the params class here as we want to allow users to use either hash (default) or Rails hash with indifferent access as a base for their params

We do that because both of them have their own advantages and we don’t want to enforce users to handle things differently if they already use any of those

It provides lazy loading not only until the first usage, but also allows us to skip using parser until we execute our logic. That way we can operate with heavy-parsing data without slowing down the whole application.

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

SYSTEM_KEYS =

Params keys that are “our” and internal. We use this list for additional backends that somehow operatae on those keys

%w[
  parser
  value
  partition
  offset
  key
  create_time
  receive_time
  topic
  parsed
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(params_klass) ⇒ Object

Includes and extends the base params klass with everything that is needed by Karafka to

fully work in any conditions.

Parameters:

  • params_klass (Karafka::Params::Params)

    initialized params class that we will use for a given Karafka process



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/karafka/params/dsl.rb', line 103

def self.included(params_klass)
  params_klass.extend(Dsl::ClassMethods)

  METHOD_ATTRIBUTES.each do |attr|
    # Defines a method call accessor to a particular hash field.
    # @note Won't work for complex key names that contain spaces, etc
    # @param key [Symbol] name of a field that we want to retrieve with a method call
    # @example
    #   key_attr_reader :example
    #   params.example #=> 'my example value'
    params_klass.send :define_method, attr do
      self[attr]
    end
  end

  params_klass.send :private, :merge!
  params_klass.send :private, :parse
end

Instance Method Details

#retrieve!Karafka::Params::Params

Returns this will trigger parser execution. If we decide to retrieve data, parser will be executed to parse data. Output of parsing will be merged to the current object. This object will be also marked as already parsed, so we won’t parse it again.

Returns:

  • (Karafka::Params::Params)

    this will trigger parser execution. If we decide to retrieve data, parser will be executed to parse data. Output of parsing will be merged to the current object. This object will be also marked as already parsed, so we won’t parse it again.



92
93
94
95
96
97
# File 'lib/karafka/params/dsl.rb', line 92

def retrieve!
  return self if self['parsed']
  self['parsed'] = true

  merge!(parse(delete('value')))
end