Class: FlossFunding::Configuration

Inherits:
Object
  • Object
show all
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.

Examples:

Basic usage

cfg = FlossFunding::Configuration.new({"a" => 1, "b" => [2, 3]})
cfg["a"] #=> [1]
cfg["b"] #=> [2, 3]
cfg.keys   #=> ["a", "b"]
cfg.to_h   #=> {"a"=>[1], "b"=>[2, 3]}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Configuration

Build from a hash-like object. Keys are converted to Strings, values to Arrays.

Parameters:

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


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.

Parameters:

Returns:



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.

Parameters:

  • key (String, Symbol)

Returns:

  • (Array)


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

Returns:

  • (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?

Returns:

  • (Boolean)


79
80
81
# File 'lib/floss_funding/configuration.rb', line 79

def key?(key)
  @data.key?(key.to_s)
end

#keysArray<String>

Returns:

  • (Array<String>)


74
75
76
# File 'lib/floss_funding/configuration.rb', line 74

def keys
  @data.keys
end

#sizeInteger

Returns:

  • (Integer)


92
93
94
# File 'lib/floss_funding/configuration.rb', line 92

def size
  @data.size
end

#to_hHash{String=>Array}

Returns:

  • (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