ConfigSection¶
Hash-like wrapper for nested configuration with method access and Enumerable support.
Overview¶
ConfigSection wraps Hash values from YAML, providing:
- Method access (
config.database.host) - Bracket access (
config.database[:host]) - Enumerable iteration
- Hash-like methods (
keys,values,fetch,dig)
Constructor¶
new¶
Create a new ConfigSection from a Hash.
Parameters:
hash(Hash) - The hash to wrap (default:{})
Behavior:
- Keys are symbolized
- Nested Hashes become nested ConfigSections
Access Methods¶
Method Access¶
Bracket Access¶
Setting Values¶
Hash Methods¶
keys¶
Get all keys.
Returns:
Array<Symbol>- All keys
values¶
Get all values.
Returns:
Array- All values
size / length¶
Get the number of keys.
Returns:
Integer- Number of keys
key? / has_key? / include? / member?¶
Check if a key exists.
section.key?(:host) # => true
section.has_key?(:host) # => true
section.include?(:host) # => true
section.member?(:host) # => true
Parameters:
key(Symbol, String) - The key to check
Returns:
Boolean- true if key exists
empty?¶
Check if the section has no keys.
Returns:
Boolean- true if no keys
fetch¶
Fetch a value with optional default.
# With key
section.fetch(:host) # => "localhost"
# With default value
section.fetch(:missing, "default") # => "default"
# With block
section.fetch(:missing) { |k| "no #{k}" } # => "no missing"
# Without default (raises KeyError)
section.fetch(:missing) # => KeyError: key not found: :missing
Parameters:
key(Symbol, String) - The key to fetchdefault(Object) - Optional default value
Yields:
key- When block provided and key missing
Returns:
Object- The value or default
Raises:
KeyError- If key not found and no default
dig¶
Access nested values safely.
config.api.dig(:headers, :content_type) # => "application/json"
config.api.dig(:missing, :nested) # => nil
Parameters:
keys(Array) - Keys to dig through
Returns:
Object, nil- The value or nil if not found
[] and []=¶
Bracket access and assignment.
Enumerable Methods¶
ConfigSection includes Enumerable, providing all iteration methods.
each¶
Iterate over key-value pairs.
map¶
Transform key-value pairs.
select / reject¶
Filter key-value pairs.
section.select { |k, v| v.is_a?(Integer) }
# => [[:port, 5432], [:pool, 5]]
section.reject { |k, v| v.nil? }
find / detect¶
Find first matching pair.
any? / all? / none?¶
Check conditions.
section.any? { |k, v| v.nil? } # => false
section.all? { |k, v| v } # => true
section.none? { |k, v| v.nil? } # => true
Other Enumerable Methods¶
All standard Enumerable methods work:
count,first,take,dropmin,max,minmaxsort,sort_bygroup_by,partitionreduce,inject- And more...
Conversion Methods¶
to_h¶
Convert to a plain Ruby Hash.
Nested ConfigSections are also converted:
config.api.to_h
# => {
# base_url: "https://api.example.com",
# timeout: 30,
# headers: {content_type: "application/json"}
# }
Returns:
Hash- Plain Ruby Hash
merge¶
Merge with another ConfigSection or Hash.
overrides = { host: "new-host", pool: 10 }
merged = section.merge(overrides)
merged.host # => "new-host"
merged.pool # => 10
merged.port # => 5432 (from original)
Parameters:
other(ConfigSection, Hash) - Values to merge
Returns:
ConfigSection- New merged ConfigSection
Examples¶
Building Connection Strings¶
db = config.database
connection_string = "postgres://#{db.host}:#{db.port}/#{db.name}"
# => "postgres://localhost:5432/myapp_db"