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.



37
38
39
40
41
# File 'lib/config_mapper/config_dict.rb', line 37

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

Instance Method Details

#[](key) ⇒ Object



43
44
45
46
# File 'lib/config_mapper/config_dict.rb', line 43

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

#config_errorsObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/config_mapper/config_dict.rb', line 56

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



48
49
50
51
52
53
54
# File 'lib/config_mapper/config_dict.rb', line 48

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