Class: AppConfigLoader::ConfigMap

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/app_config_loader/config_map.rb

Instance Method Summary collapse

Constructor Details

#initializeConfigMap

Returns a new instance of ConfigMap.



5
6
7
# File 'lib/app_config_loader/config_map.rb', line 5

def initialize
  @key_map = {}
end

Instance Method Details

#<<(entry) ⇒ Object



21
22
23
# File 'lib/app_config_loader/config_map.rb', line 21

def <<(entry)
  add entry, false
end

#add(entry, overwrite = false) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/app_config_loader/config_map.rb', line 9

def add(entry, overwrite = false)
  deep_add(@key_map, entry.key_components) do |existing|
    if existing
      # only override the existing entry if 'force' is true or
      # the new entry has a higher specificity
      overwrite || entry.specificity >= existing.specificity ? entry : existing
    else
      entry
    end
  end
end

#each(&block) ⇒ Object



43
44
45
# File 'lib/app_config_loader/config_map.rb', line 43

def each(&block)
  self.to_a.each(&block)
end

#get(key) ⇒ Object Also known as: []



25
26
27
28
29
30
31
32
# File 'lib/app_config_loader/config_map.rb', line 25

def get(key)
  components = key.split('.')
  components.reduce(@key_map) do |parent, comp|
    # return nil if the parent is not a Hash
    break nil unless parent.is_a?(Hash)
    parent[comp.to_sym]
  end
end

#merge(config_map) ⇒ Object



39
40
41
# File 'lib/app_config_loader/config_map.rb', line 39

def merge(config_map)
  config_map.to_a.each { |override| self.add override, true }
end

#to_aObject



35
36
37
# File 'lib/app_config_loader/config_map.rb', line 35

def to_a
  deep_list @key_map
end