Class: Handy::ConfigLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/handy/config_loader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ ConfigLoader

Returns a new instance of ConfigLoader.


11
12
13
# File 'lib/handy/config_loader.rb', line 11

def initialize(filename)
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename


9
10
11
# File 'lib/handy/config_loader.rb', line 9

def filename
  @filename
end

Instance Method Details

#load(key = Rails.env) ⇒ Object


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/handy/config_loader.rb', line 15

def load(key = Rails.env)
  global_file = Rails.root.join('config', filename)
  settings = read_settings_from_file(global_file)
  common_settings = settings['common'] || {}
  env_settings = settings[key.to_s] || {}

  env_file = Rails.root.join('config', File.basename(filename, '.yml'), "#{key}.yml")

  env_settings_from_file = read_settings_from_file(env_file)
  env_settings = env_settings.deep_merge(env_settings_from_file)

  result_settings = common_settings.deep_merge(env_settings)
  ::Hashr.new(result_settings)
end