Module: Tacape::Tools::Helpers::JsonConfig::InstanceMethods

Defined in:
lib/tacape/tools/helpers/json_config.rb

Instance Method Summary collapse

Instance Method Details

#load_configObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tacape/tools/helpers/json_config.rb', line 43

def load_config
  if File.exist? @config_file
    @config = JSON.parse(File.read(@config_file))
    unless @config.class==Hash
      raise 'Corrupt JSON file!'
    end
    return
  else
    setup
  end
end

#save_configObject



55
56
57
58
59
60
61
# File 'lib/tacape/tools/helpers/json_config.rb', line 55

def save_config
  unless File.exist? File.dirname(@config_file)
    FileUtils.mkdir_p(File.dirname(@config_file))
  end
  
  File.open(@config_file, 'w') {|f| f.write(@config.to_json) }
end

#setupObject



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
# File 'lib/tacape/tools/helpers/json_config.rb', line 6

def setup
  @config={}
  @config_template.each do |k,v|
    tip=' use comma, no spaces' if v.class==Array
    if @config_template[k]!=nil && @config_template[k]!=''
      question = "#{k} [default=#{@config_template[k]}]#{tip}:"
    else
      question = "#{k}#{tip}:"
    end

    input = ask(question)

    unless input.empty?
      case v
      when String
        @config[k]=input
      when Array
        @config[k]=input.split(',')
      else
        puts "Bailed on #{v.class}"
      end
      if k.include?('folder') || k.include?('file')
        if File.dirname(@config[k]) == '.'
          @config[k]="#{ENV['HOME']}/#{@config[k]}"  
        end
        unless File.exist?(File.dirname(@config[k]))
          FileUtils.mkdir_p(File.dirname(@config[k]))  
        end
      end
    else
      @config[k]=@config_template[k]  
    end
  end

  save_config
end