Class: Sonic::Setting

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

Instance Method Summary collapse

Instance Method Details

#dataObject



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

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

  project = yaml_file("#{Sonic.root}/.sonic/#{settings_file}")
  user = yaml_file("#{home}/.sonic/#{settings_file}")
  default_file = File.expand_path("../default/settings.yml", __FILE__)
  default = yaml_file(default_file)

  data = merge(default, user, project)

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

#homeObject



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

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

#merge(*hashes) ⇒ Object



28
29
30
31
32
33
# File 'lib/sonic/setting.rb', line 28

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.



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

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