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.



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

def initialize(filename)
  @filename = filename
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



6
7
8
# File 'lib/handy/config_loader.rb', line 6

def filename
  @filename
end

Instance Method Details

#load(key = Rails.env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/handy/config_loader.rb', line 12

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