Class: Prosody::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/prosody/configuration.rb

Overview

Configuration class for the Prosody messaging client.

This class provides a flexible configuration system for the Prosody client, with strong typing, validation, and convenient default values. It handles various parameter types including strings, arrays, durations, integers, and special case configurations like operation mode and health probe settings.

Examples:

Basic usage

config = Prosody::Configuration.new(
  bootstrap_servers: "kafka:9092",
  group_id: "my-consumer-group"
)

With block syntax

config = Prosody::Configuration.new do |c|
  c.bootstrap_servers = ["kafka1:9092", "kafka2:9092"]
  c.group_id = "my-consumer-group"
  c.max_concurrency = 10
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kwargs = {}) {|self| ... } ⇒ Configuration

Initializes a new Configuration instance.

Parameters:

  • kwargs (Hash) (defaults to: {})

    Initial configuration parameters as keyword arguments

Yields:

  • (self)

    Yields self for block-based configuration



63
64
65
66
67
# File 'lib/prosody/configuration.rb', line 63

def initialize(kwargs = {})
  @config = {}
  kwargs.each { |k, v| public_send(:"#{k}=", v) }
  yield self if block_given?
end

Class Method Details

.config_param(name, converter: ->(v) { v }, default: nil) ⇒ void

This method returns an undefined value.

Defines a configuration parameter with type conversion and optional default value.

This DSL helper creates getter and setter methods for a configuration parameter, with the setter applying type conversion. The getter returns the default value when the parameter hasn’t been set.

Parameters:

  • name (Symbol, String)

    The parameter name

  • converter (Proc) (defaults to: ->(v) { v })

    A lambda that converts and validates the input value

  • default (Object, nil) (defaults to: nil)

    The default value returned when parameter is unset



34
35
36
37
38
# File 'lib/prosody/configuration.rb', line 34

def self.config_param(name, converter: ->(v) { v }, default: nil)
  name_sym = name.to_sym
  define_method(name) { @config.key?(name_sym) ? @config[name_sym] : default }
  define_method(:"#{name}=") { |value| @config[name_sym] = value.nil? ? nil : converter.call(value) }
end

.duration_converter(v) ⇒ Float

Converts a duration value to float seconds.

Handles ActiveSupport::Duration objects (if available) and numeric values, converting them to floating-point seconds.

Parameters:

  • v (Numeric, ActiveSupport::Duration)

    The duration value to convert

Returns:

  • (Float)

    The duration in seconds as a float

Raises:

  • (ArgumentError)

    If the input is not a valid duration type



48
49
50
51
52
53
54
55
56
# File 'lib/prosody/configuration.rb', line 48

def self.duration_converter(v)
  if defined?(ActiveSupport::Duration) && v.is_a?(ActiveSupport::Duration)
    v.to_f
  elsif v.is_a?(Numeric)
    v.to_f
  else
    raise ArgumentError, "Invalid type for duration: #{v.inspect}"
  end
end

Instance Method Details

#to_hashHash

Returns a Ruby hash with only non-nil values, suitable for Rust deserialization.

Returns:

  • (Hash)

    Configuration hash with all nil values removed



329
330
331
# File 'lib/prosody/configuration.rb', line 329

def to_hash
  @config.dup.compact
end