Class: Karafka::Declaratives::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/declaratives/builder.rb

Overview

The main entry point for the declaratives subsystem. Returned by ‘Karafka::App.declaratives`. Provides the `draw` DSL and programmatic access to topic declarations and operations.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

Returns a new instance of Builder.



10
11
12
13
# File 'lib/karafka/declaratives/builder.rb', line 10

def initialize
  @repository = Repository.new
  @defaults = ->(_) {}
end

Instance Attribute Details

#repositoryObject (readonly)

Returns the value of attribute repository.



8
9
10
# File 'lib/karafka/declaratives/builder.rb', line 8

def repository
  @repository
end

Instance Method Details

#defaults(&block) ⇒ Object

Sets default values applied to every topic declared after this call. Defaults are evaluated before the topic block, so topic-specific values override them.

Parameters:

  • block (Proc)

    block evaluated in each topic’s context before the topic block



35
36
37
# File 'lib/karafka/declaratives/builder.rb', line 35

def defaults(&block)
  @defaults = block
end

#draw(&block) ⇒ Object

DSL entry point for declaring topics outside of routing blocks.

Examples:

Karafka::App.declaratives.draw do
  topic :orders do
    partitions 10
    replication_factor 3
    config 'retention.ms' => 604_800_000
  end
end

Parameters:

  • block (Proc)

    block to evaluate in builder context



27
28
29
# File 'lib/karafka/declaratives/builder.rb', line 27

def draw(&block)
  instance_eval(&block)
end

#find_topic(name) ⇒ Karafka::Declaratives::Topic?

Returns specific topic declaration.

Parameters:

  • name (String, Symbol)

    topic name

Returns:



60
61
62
# File 'lib/karafka/declaratives/builder.rb', line 60

def find_topic(name)
  @repository.find(name)
end

#topic(name, &block) ⇒ Karafka::Declaratives::Topic

Declares a topic with the given name. If a topic with this name was already declared (via routing bridge or a prior draw call), the existing declaration is returned and the block is evaluated on it (allowing additive configuration).

Parameters:

  • name (String, Symbol)

    topic name

  • block (Proc)

    optional block evaluated in the topic’s context

Returns:



46
47
48
49
50
51
# File 'lib/karafka/declaratives/builder.rb', line 46

def topic(name, &block)
  declaration = @repository.find_or_create(name)
  declaration.instance_eval(&@defaults)
  declaration.instance_eval(&block) if block
  declaration
end

#topicsArray<Karafka::Declaratives::Topic>

Returns all active topic declarations.

Returns:



54
55
56
# File 'lib/karafka/declaratives/builder.rb', line 54

def topics
  @repository.active
end