Class: Ace::Support::Config::Models::Config
- Inherits:
-
Object
- Object
- Ace::Support::Config::Models::Config
- Defined in:
- lib/ace/support/config/models/config.rb
Overview
Configuration data structure
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#merge_strategy ⇒ Object
readonly
Returns the value of attribute merge_strategy.
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
-
.wrap(base, overrides = {}, source: "wrap", merge_strategy: :replace) ⇒ Hash
Factory method to wrap a hash and merge additional data, returning a hash Provides a convenient one-liner for the common pattern: Config.new(base, source: “…”).merge(overrides).to_h.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#each(&block) ⇒ Object
Iterate over root level key-value pairs.
-
#empty? ⇒ Boolean
Check if configuration is empty.
-
#get(*keys) ⇒ Object
Get configuration value by key path.
-
#initialize(data = {}, source: nil, merge_strategy: :replace) ⇒ Config
constructor
Initialize configuration.
- #inspect ⇒ Object
-
#key?(*keys) ⇒ Boolean
Check if configuration has key path.
-
#keys ⇒ Array<String>
Get all keys at root level.
-
#merge(other_data) ⇒ Config
(also: #with)
Create new config with additional data merged in.
-
#to_h ⇒ Hash
Convert to hash.
Constructor Details
#initialize(data = {}, source: nil, merge_strategy: :replace) ⇒ Config
Initialize configuration
15 16 17 18 19 20 |
# File 'lib/ace/support/config/models/config.rb', line 15 def initialize(data = {}, source: nil, merge_strategy: :replace) @data = data || {} @source = source @merge_strategy = merge_strategy freeze end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
9 10 11 |
# File 'lib/ace/support/config/models/config.rb', line 9 def data @data end |
#merge_strategy ⇒ Object (readonly)
Returns the value of attribute merge_strategy.
9 10 11 |
# File 'lib/ace/support/config/models/config.rb', line 9 def merge_strategy @merge_strategy end |
#source ⇒ Object (readonly)
Returns the value of attribute source.
9 10 11 |
# File 'lib/ace/support/config/models/config.rb', line 9 def source @source end |
Class Method Details
.wrap(base, overrides = {}, source: "wrap", merge_strategy: :replace) ⇒ Hash
110 111 112 113 114 115 116 117 118 |
# File 'lib/ace/support/config/models/config.rb', line 110 def self.wrap(base, overrides = {}, source: "wrap", merge_strategy: :replace) # Type coercion: ensure base and overrides are hashes to prevent unexpected behavior base_hash = base.is_a?(Hash) ? base : {} overrides_hash = overrides.is_a?(Hash) ? overrides : {} new(base_hash, source: source, merge_strategy: merge_strategy) .merge(overrides_hash) .to_h end |
Instance Method Details
#==(other) ⇒ Object
120 121 122 123 124 125 |
# File 'lib/ace/support/config/models/config.rb', line 120 def ==(other) other.is_a?(self.class) && other.data == data && other.source == source && other.merge_strategy == merge_strategy end |
#each(&block) ⇒ Object
Iterate over root level key-value pairs
60 61 62 |
# File 'lib/ace/support/config/models/config.rb', line 60 def each(&block) data.each(&block) end |
#empty? ⇒ Boolean
Check if configuration is empty
55 56 57 |
# File 'lib/ace/support/config/models/config.rb', line 55 def empty? data.empty? end |
#get(*keys) ⇒ Object
Get configuration value by key path
25 26 27 28 29 30 31 32 |
# File 'lib/ace/support/config/models/config.rb', line 25 def get(*keys) keys = keys.flatten.map(&:to_s) keys.reduce(data) do |current, key| return nil unless current.is_a?(Hash) current[key] end end |
#inspect ⇒ Object
127 128 129 |
# File 'lib/ace/support/config/models/config.rb', line 127 def inspect "#<#{self.class.name} source=#{source.inspect} keys=#{keys.inspect}>" end |
#key?(*keys) ⇒ Boolean
Check if configuration has key path
37 38 39 |
# File 'lib/ace/support/config/models/config.rb', line 37 def key?(*keys) !get(*keys).nil? end |
#keys ⇒ Array<String>
Get all keys at root level
49 50 51 |
# File 'lib/ace/support/config/models/config.rb', line 49 def keys data.keys end |
#merge(other_data) ⇒ Config Also known as: with
Create new config with additional data merged in
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ace/support/config/models/config.rb', line 67 def merge(other_data) merged_data = Atoms::DeepMerger.merge( data, other_data, array_strategy: merge_strategy ) self.class.new( merged_data, source: "#{source}+merged", merge_strategy: merge_strategy ) end |
#to_h ⇒ Hash
Convert to hash
43 44 45 |
# File 'lib/ace/support/config/models/config.rb', line 43 def to_h data.dup end |