Class: ConfigMapper::ConfigDict

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/config_mapper/config_dict.rb

Defined Under Namespace

Classes: Factory

Instance Method Summary collapse

Constructor Details

#initialize(entry_factory, key_validator = nil) ⇒ ConfigDict

Returns a new instance of ConfigDict.



34
35
36
37
38
# File 'lib/config_mapper/config_dict.rb', line 34

def initialize(entry_factory, key_validator = nil)
  @entry_factory = entry_factory
  @key_validator = key_validator
  @entries = {}
end

Instance Method Details

#[](key) ⇒ Object



40
41
42
43
# File 'lib/config_mapper/config_dict.rb', line 40

def [](key)
  key = @key_validator.call(key) if @key_validator
  @entries[key] ||= @entry_factory.new
end

#config_errorsObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/config_mapper/config_dict.rb', line 53

def config_errors
  {}.tap do |errors|
    each do |key, value|
      prefix = "[#{key.inspect}]"
      next unless value.respond_to?(:config_errors)
      value.config_errors.each do |path, path_errors|
        errors["#{prefix}#{path}"] = path_errors
      end
    end
  end
end

#to_hObject



45
46
47
48
49
50
51
# File 'lib/config_mapper/config_dict.rb', line 45

def to_h
  {}.tap do |result|
    @entries.each do |key, value|
      result[key] = value.to_h
    end
  end
end