Class: MywayConfig::ConfigSection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/myway_config/config_section.rb

Overview

ConfigSection provides method access to nested configuration hashes

This allows configuration values to be accessed using method syntax instead of hash bracket notation, making config access more readable. Includes Enumerable for full Hash-like iteration support.

Examples:

Basic usage

section = ConfigSection.new(host: 'localhost', port: 5432)
section.host        # => 'localhost'
section[:host]      # => 'localhost'
section['host']     # => 'localhost'

Nested sections

section = ConfigSection.new(database: { host: 'localhost', port: 5432 })
section.database.host  # => 'localhost'

Hash-like behavior

section.keys           # => [:host, :port]
section.values         # => ['localhost', 5432]
section.fetch(:host)   # => 'localhost'
section.map { |k, v| "#{k}=#{v}" }  # => ['host=localhost', 'port=5432']

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ ConfigSection

Returns a new instance of ConfigSection.



28
29
30
31
32
33
# File 'lib/myway_config/config_section.rb', line 28

def initialize(hash = {})
  @data = {}
  (hash || {}).each do |key, value|
    @data[key.to_sym] = value.is_a?(Hash) ? ConfigSection.new(value) : value
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



35
36
37
38
39
40
41
42
43
44
# File 'lib/myway_config/config_section.rb', line 35

def method_missing(method, *args, &block)
  key = method.to_s
  if key.end_with?('=')
    @data[key.chomp('=').to_sym] = args.first
  elsif @data.key?(method)
    @data[method]
  else
    nil
  end
end

Instance Method Details

#[](key) ⇒ Object

Access a value by key

Parameters:

  • key (Symbol, String)

    the key to access

Returns:

  • (Object)

    the value



64
65
66
# File 'lib/myway_config/config_section.rb', line 64

def [](key)
  @data[key.to_sym]
end

#[]=(key, value) ⇒ Object

Set a value by key

Parameters:

  • key (Symbol, String)

    the key to set

  • value (Object)

    the value to set



72
73
74
# File 'lib/myway_config/config_section.rb', line 72

def []=(key, value)
  @data[key.to_sym] = value
end

#dig(*keys) ⇒ Object?

Dig into nested values

Parameters:

  • keys (Array<Symbol, String>)

    the keys to dig through

Returns:

  • (Object, nil)

    the value or nil if not found



144
145
146
147
148
149
# File 'lib/myway_config/config_section.rb', line 144

def dig(*keys)
  keys.reduce(self) do |obj, key|
    return nil unless obj.respond_to?(:[])
    obj[key]
  end
end

#each {|key, value| ... } ⇒ Object

Iterate over key-value pairs

Yields:

  • (key, value)

    each key-value pair



95
96
97
# File 'lib/myway_config/config_section.rb', line 95

def each(&block)
  @data.each(&block)
end

#empty?Boolean

Check if the section is empty

Returns:

  • (Boolean)

    true if no keys are present



110
111
112
# File 'lib/myway_config/config_section.rb', line 110

def empty?
  @data.empty?
end

#fetch(key, *args) { ... } ⇒ Object

Fetch a value with optional default

Parameters:

  • key (Symbol, String)

    the key to fetch

  • default (Object)

    optional default value

Yields:

  • optional block for default value

Returns:

  • (Object)

    the value or default

Raises:

  • (KeyError)

    if key not found and no default given



136
137
138
# File 'lib/myway_config/config_section.rb', line 136

def fetch(key, *args, &block)
  @data.fetch(key.to_sym, *args, &block)
end

#key?(key) ⇒ Boolean Also known as: has_key?, include?, member?

Check if a key exists

Parameters:

  • key (Symbol, String)

    the key to check

Returns:

  • (Boolean)

    true if the key exists



103
104
105
# File 'lib/myway_config/config_section.rb', line 103

def key?(key)
  @data.key?(key.to_sym)
end

#keysArray<Symbol>

Get all keys

Returns:

  • (Array<Symbol>)

    the keys



88
89
90
# File 'lib/myway_config/config_section.rb', line 88

def keys
  @data.keys
end

#merge(other) ⇒ ConfigSection

Merge with another ConfigSection or hash

Parameters:

Returns:



80
81
82
83
# File 'lib/myway_config/config_section.rb', line 80

def merge(other)
  other_hash = other.is_a?(ConfigSection) ? other.to_h : other
  ConfigSection.new(deep_merge(to_h, other_hash || {}))
end

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

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/myway_config/config_section.rb', line 46

def respond_to_missing?(method, include_private = false)
  key = method.to_s.chomp('=').to_sym
  @data.key?(key) || super
end

#sizeInteger Also known as: length

Get the number of keys

Returns:

  • (Integer)

    the number of keys



124
125
126
# File 'lib/myway_config/config_section.rb', line 124

def size
  @data.size
end

#to_hHash

Convert to a plain Ruby hash

Returns:

  • (Hash)

    the configuration as a hash



54
55
56
57
58
# File 'lib/myway_config/config_section.rb', line 54

def to_h
  @data.transform_values do |v|
    v.is_a?(ConfigSection) ? v.to_h : v
  end
end

#valuesArray

Get all values

Returns:

  • (Array)

    the values



117
118
119
# File 'lib/myway_config/config_section.rb', line 117

def values
  @data.values
end