Class: Prosody::Configuration
- Inherits:
-
Object
- Object
- Prosody::Configuration
- 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.
Class Method Summary collapse
-
.config_param(name, converter: ->(v) { v }, default: nil) ⇒ void
Defines a configuration parameter with type conversion and optional default value.
-
.duration_converter(v) ⇒ Float
Converts a duration value to float seconds.
Instance Method Summary collapse
-
#initialize(kwargs = {}) {|self| ... } ⇒ Configuration
constructor
Initializes a new Configuration instance.
-
#to_hash ⇒ Hash
Returns a Ruby hash with only non-nil values, suitable for Rust deserialization.
Constructor Details
#initialize(kwargs = {}) {|self| ... } ⇒ Configuration
Initializes a new Configuration instance.
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.
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.
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_hash ⇒ Hash
Returns a Ruby hash with only non-nil values, suitable for Rust deserialization.
329 330 331 |
# File 'lib/prosody/configuration.rb', line 329 def to_hash @config.dup.compact end |