module HelperClasses::ReadConfig

Public Instance Methods

bash(file, downcase = false) click to toggle source

Very simple bash-reader, doesn't do array or multi-line configurations

# File lib/helper_classes/readconfig.rb, line 20
def bash(file, downcase = false)
  return nil unless File.exists? file
  IO.readlines(file).collect { |l|
    if l =~ /^#/
      nil
    elsif l =~ /([^ ]+)=(.*)/
      [(downcase ? $1.downcase : $1).to_sym, $2]
    end
  }.compact.to_h
end
file_name(file) click to toggle source

Searches in this order: ~/.config ~ /etc

Returns nil if nothing found

# File lib/helper_classes/readconfig.rb, line 11
def file_name(file)
  %w( ~/.config ~ /etc ).each { |d|
    file_abs = File.expand_path("#{d}/#{file}")
    File.exists?(file_abs) and return file_abs
  }
  nil
end
json(file) click to toggle source
# File lib/helper_classes/readconfig.rb, line 45
def json(file)
  p 'Not implemented yet'
  exit
end
ruby(file) click to toggle source

Ruby file-reader, returns created hash THIS IS ABSOLUTELY INSECURE AND WILL EAT YOUR KITTENS! It returns what the file returns at the end - so most probably you'll want something like

{ one: 1,
  two: 2 }

in that config-file

# File lib/helper_classes/readconfig.rb, line 40
def ruby(file)
  return {} unless File.exists? file.to_s
  return eval(IO.readlines(file).join)
end