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(par) ⇒ KeyValueReader
constructor
Constructor.
-
#keys ⇒ Array
Returns all keys (i.e. left handed side of the parsed key/values).
-
#merge(a_hash) ⇒ Object
Merges a hash of key value pairs without actually overwriting existing entries.
-
#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(par) ⇒ KeyValueReader
Constructor
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rakeoe/key_value_reader.rb', line 24 def initialize(par) if par.is_a? String raise "No such file #{par}" unless File.exist?(par) @file_name = par @env = self.class.read_file(@file_name) elsif par.is_a? Hash @env = par end 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 (‘“)
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rakeoe/key_value_reader.rb', line 71 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! if value.empty? 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 else 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('=').strip val.gsub!(/^["']*/, '') val.gsub!(/["']$/, '') val.gsub!(/[\n]*/, '') env[key] = val.strip end end env end |
.substitute_dollar_symbols!(env) ⇒ Object
Substitute all dollar values with either already parsed values or with system environment variables
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rakeoe/key_value_reader.rb', line 40 def self.substitute_dollar_symbols!(env) more_dollars = false resolved_dollar_vars = env.each_with_object(Hash.new) do |var, obj| # expand all variable patterns and try to match ENV or env pattern = /\$([a-zA-Z_]+[a-zA-Z0-9_]*)|\$\{(.+)\}/ obj[var[0]] = var[1].gsub(pattern) do # if in ENV, use it rv = ENV[$1||$2] unless rv # if not in ENV, use env, but only if not same as string we want to substitute rv = env[$1||$2] if env[$1||$2] != var[1] end rv end # if still contains dollar symbol: recurse at end more_dollars = true if obj[var[0]] =~ pattern end # overwrite old values with resolved values env.merge!(resolved_dollar_vars) self.substitute_dollar_symbols!(env) if more_dollars end |
Instance Method Details
#add(key, value) ⇒ Object
Adds a value for key
173 174 175 176 177 178 179 |
# File 'lib/rakeoe/key_value_reader.rb', line 173 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
183 184 185 186 187 188 |
# File 'lib/rakeoe/key_value_reader.rb', line 183 def dump puts "#{@file_name}" @env.each_pair do |key, value| puts "[#{key}]: #{value}" end end |
#file ⇒ String
Returns filename of read file
134 135 136 |
# File 'lib/rakeoe/key_value_reader.rb', line 134 def file @file_name end |
#get(key) ⇒ String
Returns the value belonging to key (right hand side), or empty string if no such value
144 145 146 |
# File 'lib/rakeoe/key_value_reader.rb', line 144 def get(key) @env[key] || '' end |
#keys ⇒ Array
Returns all keys (i.e. left handed side of the parsed key/values)
117 118 119 |
# File 'lib/rakeoe/key_value_reader.rb', line 117 def keys @env.keys end |
#merge(a_hash) ⇒ Object
Merges a hash of key value pairs without actually overwriting existing entries. This is similar as the ||= operator on a key => value basis.
164 165 166 |
# File 'lib/rakeoe/key_value_reader.rb', line 164 def merge(a_hash) @env.merge!(a_hash) { |key, v1, v2| v1 } end |
#set(key, value) ⇒ Object
Sets a value for key
154 155 156 |
# File 'lib/rakeoe/key_value_reader.rb', line 154 def set(key, value) @env[key] = value end |
#values ⇒ Array
Returns all values (i.e. right handed side of the parsed key/values)
126 127 128 |
# File 'lib/rakeoe/key_value_reader.rb', line 126 def values @env.values end |