Module: HelperClasses::ReadConfig
Instance Method Summary collapse
-
#bash(file, downcase = false) ⇒ Object
Very simple bash-reader, doesn’t do array or multi-line configurations.
-
#file_name(file) ⇒ Object
Searches in this order: ~/.config ~ /etc.
- #json(file) ⇒ Object
-
#ruby(file) ⇒ Object
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.
Instance Method Details
#bash(file, downcase = false) ⇒ Object
Very simple bash-reader, doesn’t do array or multi-line configurations
20 21 22 23 24 25 26 27 28 29 |
# 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) ⇒ Object
Searches in this order: ~/.config ~ /etc
Returns nil if nothing found
11 12 13 14 15 16 17 |
# File 'lib/helper_classes/readconfig.rb', line 11 def file_name(file) %w( ~/.config ~ /etc ).each { |d| file_abs = File.("#{d}/#{file}") File.exists?(file_abs) and return file_abs } nil end |
#json(file) ⇒ Object
45 46 47 48 |
# File 'lib/helper_classes/readconfig.rb', line 45 def json(file) p 'Not implemented yet' exit end |
#ruby(file) ⇒ Object
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
40 41 42 43 |
# 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 |