Class: FlossFunding::Configuration
- Inherits:
-
Object
- Object
- FlossFunding::Configuration
- Includes:
- Enumerable
- Defined in:
- lib/floss_funding/configuration.rb
Overview
Immutable, read-only configuration wrapper for a library/namespace.
Normalizes all values to arrays to simplify downstream processing.
Provides a hash-like interface for reading values.
Class Method Summary collapse
-
.merged_config(cfgs) ⇒ FlossFunding::Configuration
Merge an array of Configuration objects into a single Configuration by concatenating array values for identical keys.
Instance Method Summary collapse
-
#[](key) ⇒ Array
Fetch the array value for the given key; returns [] when the key is missing.
-
#each(&block) ⇒ Object
Iterate over key, value pairs (values are arrays).
-
#empty? ⇒ Boolean
-
#fetch(key, default = nil, &block) ⇒ Object
Fetch with default/blk compatibility; mirrors Hash#fetch for read-only access.
-
#initialize(data = {}) ⇒ Configuration
constructor
Build from a hash-like object.
-
#key?(key) ⇒ Boolean
(also: #include?, #has_key?)
-
#keys ⇒ Array<String>
-
#size ⇒ Integer
-
#to_h ⇒ Hash{String=>Array}
Constructor Details
#initialize(data = {}) ⇒ Configuration
Build from a hash-like object. Keys are converted to Strings, values to Arrays.
40 41 42 43 44 45 46 47 |
# File 'lib/floss_funding/configuration.rb', line 40 def initialize(data = {}) normalized = {} (data || {}).each do |k, v| normalized[k.to_s] = ::FlossFunding::Config.normalize_to_array(v) end @data = normalized.freeze freeze end |
Class Method Details
.merged_config(cfgs) ⇒ FlossFunding::Configuration
Merge an array of Configuration objects into a single Configuration by
concatenating array values for identical keys. Assumes each input
configuration has already normalized values to arrays.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/floss_funding/configuration.rb', line 23 def self.merged_config(cfgs) cfgs = Array(cfgs).compact return ::FlossFunding::Configuration.new({}) if cfgs.empty? merged = {} cfgs.each do |cfg| next unless cfg.respond_to?(:each) cfg.each do |k, v| merged[k.to_s] ||= [] merged[k.to_s].concat(Array(v)) end end ::FlossFunding::Configuration.new(merged) end |
Instance Method Details
#[](key) ⇒ Array
Fetch the array value for the given key; returns [] when the key is missing.
52 53 54 |
# File 'lib/floss_funding/configuration.rb', line 52 def [](key) @data[key.to_s] || [] end |
#each(&block) ⇒ Object
Iterate over key, value pairs (values are arrays)
68 69 70 71 |
# File 'lib/floss_funding/configuration.rb', line 68 def each(&block) return enum_for(:each) unless block_given? @data.each(&block) end |
#empty? ⇒ Boolean
97 98 99 |
# File 'lib/floss_funding/configuration.rb', line 97 def empty? @data.empty? end |
#fetch(key, default = nil, &block) ⇒ Object
Fetch with default/blk compatibility; mirrors Hash#fetch for read-only access.
57 58 59 60 61 62 63 64 65 |
# File 'lib/floss_funding/configuration.rb', line 57 def fetch(key, default = nil, &block) if @data.key?(key.to_s) @data[key.to_s] elsif block yield(key) else default end end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?
79 80 81 |
# File 'lib/floss_funding/configuration.rb', line 79 def key?(key) @data.key?(key.to_s) end |
#keys ⇒ Array<String>
74 75 76 |
# File 'lib/floss_funding/configuration.rb', line 74 def keys @data.keys end |
#size ⇒ Integer
92 93 94 |
# File 'lib/floss_funding/configuration.rb', line 92 def size @data.size end |
#to_h ⇒ Hash{String=>Array}
86 87 88 89 |
# File 'lib/floss_funding/configuration.rb', line 86 def to_h # Already frozen; dup to prevent external mutation of internal state @data.dup end |