Module: YamlParams

Defined in:
lib/hash_params/yaml_params.rb

Constant Summary collapse

ENVIRONMENT =
ENV['YAML_PARAMS_ENV'] || (defined?(HashParams) && HashParams::ENVIRONMENT) || 'development'

Class Method Summary collapse

Class Method Details

.autoconfig(opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/hash_params/yaml_params.rb', line 5

def self.autoconfig(opts={})

  script_name = File.basename($0)
  script_dir = File.dirname($0)
  home_dir = File.expand_path('~')
  host_name = Socket.gethostname
  special_file_names = opts.delete(:files)
  special_file_names = Array(special_file_names && special_file_names.is_a?(String) && special_file_names.split(','))
  special_roots = opts.delete(:roots)
  special_roots = Array(special_roots && special_roots.is_a?(String) && special_roots.split(','))
  app_name = opts.delete(:app_name) || script_name
  env = opts.delete(:env) || opts.delete(:environment) || ENVIRONMENT
  generated_hash = {}
  all_file_names = []


  #Sequence is important when constructing this list as later files will override the earlier ones
  generic_file_names = %W(
                settings.yml
                config.yml
                 default.yml
                #{env}.yml
                #{host_name}.yml
                #{host_name}_#{env}.yml
                local.yml
                local_#{env}.yml
                settings.local.yml
                settings.local_#{env}.yml
                config.local.yml
                config.local_#{env}.yml
  )
  #prepend the app name to the default file names
  app_file_names = generic_file_names.map { |f| "#{app_name}_#{f}" }

  default_roots = if defined?(Rails)
                    [
                        Rails.root.join('config'),
                        Rails.root.join('config', env),
                        Rails.root.join('config', 'settings'),
                        Rails.root.join('config', 'settings', env),
                        Rails.root.join('config', 'environments'),
                        Rails.root.join('config', 'environments', env),
                        Rails.root.join('config', 'config'),
                        Rails.root.join('config', 'config', env)
                    ]
                  else
                    [
                        script_dir,
                        File.join('/etc', app_name.to_s),
                        File.join('/usr', 'local', 'etc', app_name.to_s),
                        File.join(home_dir, 'etc', app_name.to_s),
                        File.join(home_dir, ".#{app_name}"),
                        File.join(home_dir, '.hash_params', app_name.to_s),
                        File.join(script_dir, 'config'),
                        File.join(script_dir, 'settings')
                    ]
                  end


  #process the  /etc/app_name* files
  app_file_names.each do |fname|
    all_file_names << File.join('/etc', fname)
  end
  #now process the default roots which will override the above
  (default_roots + special_roots).each do |root|
    (generic_file_names + app_file_names + special_file_names).each do |fname|
      all_file_names << File.join(root, fname)
    end
  end

  all_file_names.each do |file|
    generated_hash = deep_merge(generated_hash, hash_from_yaml_file(file)) if File.exists?(file)
  end

  if block_given?
    HashParams::HashValidator.new.validate_hash(generated_hash, opts, &Proc.new)
  else
    HashParams::HashValidator.new.validate_hash(generated_hash, opts)
  end
end

.deep_merge(hash, other_hash) ⇒ Object



103
104
105
106
107
108
109
110
111
112
# File 'lib/hash_params/yaml_params.rb', line 103

def self.deep_merge(hash, other_hash)
  if other_hash.is_a?(::Hash) && hash.is_a?(::Hash)
    other_hash.each do |k, v|
      hash[k] = hash.key?(k) ? deep_merge(hash[k], v) : v
    end
    hash
  else
    other_hash
  end
end

.hash_from_yaml_file(filename, env = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/hash_params/yaml_params.rb', line 86

def self.hash_from_yaml_file(filename, env=nil)
  env ||= ENVIRONMENT
  contents = ERB.new(File.read(filename)).result if File.exists?(filename)

  return {} if contents.nil? || contents.empty?

  r = YAML::load(contents)

  #yaml load returns false for empty files
  if r
    r[env] || r
  else
    {}
  end

end