Class: Ufo::Config::Parse

Inherits:
Object
  • Object
show all
Defined in:
lib/ufo/config/parse.rb

Instance Method Summary collapse

Instance Method Details

#for(config, type: :boolean) ⇒ Object



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
# File 'lib/ufo/config/parse.rb', line 12

def for(config, type: :boolean)
  lines = IO.readlines("#{Ufo.root}/.ufo/config.rb")
  config_line = lines.find do |l|
    # IE: Regexp.new("config\.layering.show.*=")
    regexp = Regexp.new("config\.#{config}.*=")
    l =~ regexp && l !~ /^\s+#/
  end
  return false unless config_line # default is false
  config_value = config_line.gsub(/.*=/,'').strip.gsub(/["']/,'')
  case type
  when :array
    eval(config_value) # IE: '["a"]' => ["a"]
  when :boolean
    config_value != "false" && config_value != "nil"
  when :string
    config_value.sub(/\s+#.*/,'') # remove trailing comment
  else
    raise "Type #{type.inspect} not supported"
  end
rescue Exception => e
  # if ENV['UFO_DEBUG']
    puts "#{e.class} #{e.message}".color(:yellow)
    puts "WARN: Unable to parse for config.layering.show".color(:yellow)
    puts "Using default: config.layering.show = false"
  # end
  false
end