Class: Ace::Support::Config::Models::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/support/config/models/config.rb

Overview

Configuration data structure

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}, source: nil, merge_strategy: :replace) ⇒ Config

Initialize configuration

Parameters:

  • data (Hash) (defaults to: {})

    Configuration data

  • source (String) (defaults to: nil)

    Source file or identifier

  • merge_strategy (Symbol) (defaults to: :replace)

    How to merge with other configs



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

#dataObject (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_strategyObject (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

#sourceObject (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

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

Examples:

Single hash wrapping

Config.wrap(defaults)
# => { "key" => "default_value" }

Merge two hashes

Config.wrap(defaults, overrides)
# => { "key" => "override_value", "other" => "default" }

With options

Config.wrap(defaults, overrides, source: "git_config", merge_strategy: :union)

Handling nil inputs (type coercion)

Config.wrap(nil)           # => {}
Config.wrap({}, nil)       # => {}
Config.wrap(nil, nil)      # => {}

Parameters:

  • base (Hash, nil)

    Base configuration data (nil coerced to empty hash)

  • overrides (Hash, nil) (defaults to: {})

    Data to merge on top of base (default: {}, nil coerced to empty hash)

  • source (String) (defaults to: "wrap")

    Source identifier for debugging (default: “wrap”)

  • merge_strategy (Symbol) (defaults to: :replace)

    How to merge arrays (default: :replace)

Returns:

  • (Hash)

    Merged configuration as a plain 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

Returns:

  • (Boolean)

    true if no configuration data



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

Parameters:

  • keys (Array<String,Symbol>)

    Path to value

Returns:

  • (Object)

    Value at path or nil



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

#inspectObject



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

Parameters:

  • keys (Array<String,Symbol>)

    Path to check

Returns:

  • (Boolean)

    true if path exists



37
38
39
# File 'lib/ace/support/config/models/config.rb', line 37

def key?(*keys)
  !get(*keys).nil?
end

#keysArray<String>

Get all keys at root level

Returns:

  • (Array<String>)

    Root level keys



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

Parameters:

  • other_data (Hash)

    Data to merge

Returns:

  • (Config)

    New configuration instance



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_hHash

Convert to hash

Returns:

  • (Hash)

    Configuration data



43
44
45
# File 'lib/ace/support/config/models/config.rb', line 43

def to_h
  data.dup
end