Class: Rogalik::Configuration::BaseConfig

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

Overview

A lightweight dynamic configuration struct backed by a Hash.

Subclasses (or instances used directly) gain arbitrary getter/setter pairs without declaring each attribute explicitly. Useful for ad-hoc config namespaces where the key set is not known ahead of time.

Examples:

cfg = BaseConfig.new
cfg.timeout = 30
cfg.timeout  # => 30
cfg.to_h     # => { timeout: 30 }

Direct Known Subclasses

Embeddings, VectorStore

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ BaseConfig

Returns a new instance of BaseConfig.

Parameters:

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

    optional initial key/value pairs



38
39
40
# File 'lib/rogalik/configuration.rb', line 38

def initialize(settings = {})
  @settings = settings.transform_keys(&:to_sym)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Supports key (getter) and key= (setter) for any symbol name.



43
44
45
46
47
48
49
50
51
# File 'lib/rogalik/configuration.rb', line 43

def method_missing(name, *args, &block)
  key = name.to_s.delete_suffix("=").to_sym

  if name.to_s.end_with?("=")
    @settings[key] = args.first
  else
    @settings.fetch(key, nil)
  end
end

Instance Method Details

#inspectString

Returns:

  • (String)


78
79
80
# File 'lib/rogalik/configuration.rb', line 78

def inspect
  "#<#{self.class.name} #{@settings.inspect}>"
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Parameters:

  • include_private (Boolean) (defaults to: false)

Returns:

  • (Boolean)


55
56
57
58
# File 'lib/rogalik/configuration.rb', line 55

def respond_to_missing?(name, include_private = false)
  key = name.to_s.delete_suffix("=").to_sym
  @settings.key?(key) || name.to_s.end_with?("=") || super
end

#to_hHash{Symbol => Object}

Returns the underlying settings as a plain Hash.

Returns:

  • (Hash{Symbol => Object})


63
64
65
# File 'lib/rogalik/configuration.rb', line 63

def to_h
  @settings.dup
end

#to_json(*args) ⇒ String

Returns JSON representation of the settings hash.

Returns:

  • (String)

    JSON representation of the settings hash



68
69
70
# File 'lib/rogalik/configuration.rb', line 68

def to_json(*args)
  @settings.to_json(*args)
end

#to_sString

Returns:

  • (String)


73
74
75
# File 'lib/rogalik/configuration.rb', line 73

def to_s
  @settings.to_s
end