Class: RakeOE::KeyValueReader
- Inherits:
-
Object
- Object
- RakeOE::KeyValueReader
- Defined in:
- lib/rakeoe/key_value_reader.rb
Overview
“var6” = ‘val8’
Instance Attribute Summary collapse
-
#env ⇒ Object
Returns the value of attribute env.
Class Method Summary collapse
-
.read_file(file_name) ⇒ Hash
Read the given file and split according to expected key=value format Ignore empty lines or lines starting with a comment (#), ignores comments within a line Also removes quote characters (‘“).
-
.substitute_dollar_symbols!(env) ⇒ Object
Substitute all dollar values with either already parsed values or with system environment variables.
Instance Method Summary collapse
-
#add(key, value) ⇒ Object
Adds a value for key.
-
#dump ⇒ Object
Dumps all parsed variables as debugging aid.
-
#file ⇒ String
Returns filename of read file.
-
#get(key) ⇒ String
Returns the value belonging to key (right hand side), or empty string if no such value.
-
#initialize(env_file) ⇒ KeyValueReader
constructor
A new instance of KeyValueReader.
-
#keys ⇒ Array
Returns all keys (i.e. left handed side of the parsed key/values).
-
#set(key, value) ⇒ Object
Sets a value for key.
-
#values ⇒ Array
Returns all values (i.e. right handed side of the parsed key/values).
Constructor Details
#initialize(env_file) ⇒ KeyValueReader
Returns a new instance of KeyValueReader.
21 22 23 24 25 26 27 |
# File 'lib/rakeoe/key_value_reader.rb', line 21 def initialize(env_file) raise "No such file #{env_file}" unless File.exist?(env_file) @file_name = env_file @env = self.class.read_file(@file_name) self.class.substitute_dollar_symbols!(@env) end |
Instance Attribute Details
#env ⇒ Object
Returns the value of attribute env.
19 20 21 |
# File 'lib/rakeoe/key_value_reader.rb', line 19 def env @env end |
Class Method Details
.read_file(file_name) ⇒ Hash
Read the given file and split according to expected key=value format Ignore empty lines or lines starting with a comment (#), ignores comments within a line Also removes quote characters (‘“)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rakeoe/key_value_reader.rb', line 57 def self.read_file(file_name) env = Hash.new prev_key = String.new File.readlines(file_name).each do |line| line.strip! next if line.start_with?('#') next if line.empty? # delete comments within line line.gsub!(/#.*/, '') key, *value = line.split('=') next unless key # remove 'export ', quotes and leading/trailing white space from line key.gsub!(/^export\s*/, '') key.gsub!(/["']*/, '') key.strip! unless value.empty? prev_key = key # We could have split multiple '=' in one line. # Put back any "=" in the value part # and concatenate split strings val = value.join('=') val.gsub!(/["'\n]*/, '') env[key] = val.strip else if prev_key && !line.include?('=') # multiline value: treat key as value and add to previous found key env[prev_key] = "#{env[prev_key]} #{key}" end end end env end |
.substitute_dollar_symbols!(env) ⇒ Object
Substitute all dollar values with either already parsed values or with system environment variables
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rakeoe/key_value_reader.rb', line 31 def self.substitute_dollar_symbols!(env) resolved_dollar_vars = env.each_with_object(Hash.new) do |var, obj| # search for '$BLA..' identifier pattern = /\$[[:alnum:]]+/ match = var[1].match(pattern) if match # remove '$' from match, we don't need it as key key = match[0].gsub('$', '') value = env[key] ? env[key] : ENV[key] raise "No $#{key} found in environment" if value.nil? obj[var[0]] = var[1].gsub(pattern, value) end end # overwrite old values with resolved values env.merge!(resolved_dollar_vars) end |
Instance Method Details
#add(key, value) ⇒ Object
Adds a value for key
148 149 150 151 152 153 154 |
# File 'lib/rakeoe/key_value_reader.rb', line 148 def add(key, value) if @env.has_key?(key) @env[key] += value else set(key,value) end end |
#dump ⇒ Object
Dumps all parsed variables as debugging aid
158 159 160 161 162 163 |
# File 'lib/rakeoe/key_value_reader.rb', line 158 def dump puts "#{@file_name}" @env.each_pair do |key, value| puts "[#{key}]: #{value}" end end |
#file ⇒ String
Returns filename of read file
118 119 120 |
# File 'lib/rakeoe/key_value_reader.rb', line 118 def file @file_name end |
#get(key) ⇒ String
Returns the value belonging to key (right hand side), or empty string if no such value
128 129 130 |
# File 'lib/rakeoe/key_value_reader.rb', line 128 def get(key) @env[key] || '' end |
#keys ⇒ Array
Returns all keys (i.e. left handed side of the parsed key/values)
101 102 103 |
# File 'lib/rakeoe/key_value_reader.rb', line 101 def keys @env.keys end |
#set(key, value) ⇒ Object
Sets a value for key
138 139 140 |
# File 'lib/rakeoe/key_value_reader.rb', line 138 def set(key, value) @env[key] = value end |
#values ⇒ Array
Returns all values (i.e. right handed side of the parsed key/values)
110 111 112 |
# File 'lib/rakeoe/key_value_reader.rb', line 110 def values @env.values end |