Class: Kdep::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/kdep/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(deploy_dir, env = nil) ⇒ Config

Returns a new instance of Config.



7
8
9
10
# File 'lib/kdep/config.rb', line 7

def initialize(deploy_dir, env = nil)
  @deploy_dir = deploy_dir
  @env = env
end

Instance Attribute Details

#deploy_dirObject (readonly)

Returns the value of attribute deploy_dir.



5
6
7
# File 'lib/kdep/config.rb', line 5

def deploy_dir
  @deploy_dir
end

#envObject (readonly)

Returns the value of attribute env.



5
6
7
# File 'lib/kdep/config.rb', line 5

def env
  @env
end

Instance Method Details

#deep_merge(base, overlay) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/kdep/config.rb', line 26

def deep_merge(base, overlay)
  base.merge(overlay) do |_key, old_val, new_val|
    if old_val.is_a?(Hash) && new_val.is_a?(Hash)
      deep_merge(old_val, new_val)
    else
      new_val
    end
  end
end

#loadObject



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

def load
  base = load_yaml(File.join(@deploy_dir, "app.yml"))

  if @env
    overlay_path = File.join(@deploy_dir, "app_#{@env}.yml")
    if File.exist?(overlay_path)
      overlay = load_yaml(overlay_path)
      base = deep_merge(base, overlay)
    end
  end

  apply_defaults(base)
end