Class: Balancer::Setting

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Defined in:
lib/balancer/setting.rb

Instance Method Summary collapse

Instance Method Details

#dataObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/balancer/setting.rb', line 8

def data
  settings_file = Balancer.profile || 'default'
  settings_file += ".yml"

  profile = yaml_file("#{Balancer.root}/.balancer/profiles/#{settings_file}")
  user = yaml_file("#{home}/.balancer/#{settings_file}")
  default_file = File.expand_path("../default/settings.yml", __FILE__)
  default = yaml_file(default_file)

  data = merge(default, user, profile)

  if ENV['DEBUG_SETTINGS']
    puts "settings data:"
    pp data
  end
  data
end

#homeObject



42
43
44
45
# File 'lib/balancer/setting.rb', line 42

def home
  # hack but fast
  ENV['TEST'] ? "spec/fixtures/home" : ENV['HOME']
end

#merge(*hashes) ⇒ Object



27
28
29
30
31
32
# File 'lib/balancer/setting.rb', line 27

def merge(*hashes)
  hashes.inject({}) do |result, hash|
    # note: important to compact for keys with nil value
    result.deep_merge(hash.compact)
  end
end

#yaml_file(path) ⇒ Object

Any empty file will result in “false”. Lets ensure that an empty file loads an empty hash instead.



36
37
38
39
40
# File 'lib/balancer/setting.rb', line 36

def yaml_file(path)
  # puts "yaml_file #{path}"
  return {} unless File.exist?(path)
  YAML.load_file(path) || {}
end