Class: MywayConfig::ConfigSection
- Inherits:
-
Object
- Object
- MywayConfig::ConfigSection
- 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.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Access a value by key.
-
#[]=(key, value) ⇒ Object
Set a value by key.
-
#dig(*keys) ⇒ Object?
Dig into nested values.
-
#each {|key, value| ... } ⇒ Object
Iterate over key-value pairs.
-
#empty? ⇒ Boolean
Check if the section is empty.
-
#fetch(key, *args) { ... } ⇒ Object
Fetch a value with optional default.
-
#initialize(hash = {}) ⇒ ConfigSection
constructor
A new instance of ConfigSection.
-
#key?(key) ⇒ Boolean
(also: #has_key?, #include?, #member?)
Check if a key exists.
-
#keys ⇒ Array<Symbol>
Get all keys.
-
#merge(other) ⇒ ConfigSection
Merge with another ConfigSection or hash.
- #method_missing(method, *args, &block) ⇒ Object
- #respond_to_missing?(method, include_private = false) ⇒ Boolean
-
#size ⇒ Integer
(also: #length)
Get the number of keys.
-
#to_h ⇒ Hash
Convert to a plain Ruby hash.
-
#values ⇒ Array
Get all values.
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
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
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
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
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
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
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
103 104 105 |
# File 'lib/myway_config/config_section.rb', line 103 def key?(key) @data.key?(key.to_sym) end |
#keys ⇒ Array<Symbol>
Get all 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
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
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 |
#size ⇒ Integer Also known as: length
Get the number of keys
124 125 126 |
# File 'lib/myway_config/config_section.rb', line 124 def size @data.size end |
#to_h ⇒ Hash
Convert to a plain Ruby 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 |
#values ⇒ Array
Get all values
117 118 119 |
# File 'lib/myway_config/config_section.rb', line 117 def values @data.values end |