Class: AppConfigLoader::ConfigWithIndifferentAccess

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

Instance Method Summary collapse

Constructor Details

#initialize(map, prefix = nil) ⇒ ConfigWithIndifferentAccess

Returns a new instance of ConfigWithIndifferentAccess.



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

def initialize(map, prefix = nil)
  @config_map = map
  @prefix     = prefix
end

Instance Method Details

#each(&block) ⇒ Object



40
41
42
# File 'lib/app_config_loader/config_with_indifferent_access.rb', line 40

def each(&block)
  @config_map.each(&block)
end

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

Get value for a specified config key

Examples:

Getting key value

app_config['some_service.host']      # => 'dev.someservice.com'
app_config.get('some_service.host')  # => 'dev.someservice.com'

Parameters:

  • key (String)

    app config key

Returns:

  • the value for the key or another ConfigWithIndifferentAccess; nil if there is no value at the key



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/app_config_loader/config_with_indifferent_access.rb', line 19

def get(key)
  # append prefix to the key if needed
  target_key = @prefix ? "#{@prefix}.#{key}" : key.to_s

  # return either nil, the value or another ConfigWithIndifferentAccess depending on
  # what is at the key
  case (entry = @config_map[target_key])
    when ConfigEntry
      entry.value
    when Hash
      self.class.new @config_map, target_key
    else
      nil
  end
end

#to_aObject



36
37
38
# File 'lib/app_config_loader/config_with_indifferent_access.rb', line 36

def to_a
  @config_map.to_a
end