Method: CmdTools::Config.load

Defined in:
lib/cmd_tools/config.rb

.loadObject

(Re)load config file.



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
# File 'lib/cmd_tools/config.rb', line 17

def self.load
  need_dump = false
  @config = if File.readable?(CONFIG_FILE) && File.size(CONFeIG_FILE) > 0
              config_from_file = YAML.load_file(CONFIG_FILE)
              config = CONFIG_DEFAULT.merge(config_from_file)
              need_dump = config_from_file.size != CONFIG_DEFAULT.size

              config
            else
              need_dump = true

              CONFIG_DEFAULT
            end

  if need_dump
    FileUtils.mkdir_p(CONFIG_DIR)
    open(CONFIG_FILE, 'w'){|io|
      io.write(@config.to_yaml)
      io.flush
    }
  end

  @config.each{|name, val|
    var = "@#{name}"
    instance_variable_set(var, val)
    eval "      def self.\#{name}\n        \#{var}\n      end\n    EOS\n  }\n\n  self\nend\n"