class RakeOE::KeyValueReader

Provides functions for accessing key/value variables out of a file. For every variable of that file a getter method to this class is auto generated

Supported file conventions:

var1 = val1 var2 = val2 export var3 = val3:val4:val5 var4 = ‘val6’ var5 = “val7” “var6” = ‘val8’

Attributes

env[RW]

Public Class Methods

new(par) click to toggle source

Constructor @param [String, Hash] par If given as string, it should be a file name to the key-value file

if given as Hash, it already contains a key-value mapping that should be $-substituted
# File lib/rakeoe/key_value_reader.rb, line 23
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
read_file(file_name) click to toggle source

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 (‘“)

@param [String] file_name Filename to be used for operation @return [Hash] A hash containing all parsed key/value pairs

# File lib/rakeoe/key_value_reader.rb, line 70
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) click to toggle source

Substitute all dollar values with either already parsed values or with system environment variables @param [Hash] env Hash containing values that have to be expanded

# File lib/rakeoe/key_value_reader.rb, line 39
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

Public Instance Methods

add(key, value) click to toggle source

Adds a value for key

@param [String] key Key that should be used for operation @param [String] value Value that should be used for operation

# File lib/rakeoe/key_value_reader.rb, line 172
def add(key, value)
  if @env.has_key?(key)
    @env[key] += value
  else
    set(key,value)
  end
end
dump() click to toggle source

Dumps all parsed variables as debugging aid

# File lib/rakeoe/key_value_reader.rb, line 182
def dump
  puts "#{@file_name}"
  @env.each_pair do |key, value|
    puts "[#{key}]: #{value}"
  end
end
file() click to toggle source

Returns filename of read file

@return [String] File name

# File lib/rakeoe/key_value_reader.rb, line 133
def file
  @file_name
end
get(key) click to toggle source

Returns the value belonging to key (right hand side), or empty string if no such value

@param [String] key Key that should be used for operation @return [String] Value of given key

# File lib/rakeoe/key_value_reader.rb, line 143
def get(key)
  @env[key] || ''
end
keys() click to toggle source

Returns all keys (i.e. left handed side of the parsed key/values)

@return [Array] all keys

# File lib/rakeoe/key_value_reader.rb, line 116
def keys
  @env.keys
end
merge(a_hash) click to toggle source

Merges a hash of key value pairs without actually overwriting existing entries. This is similar as the ||= operator on a key => value basis.

@param a_hash Hash of Key/Value pairs

@return the

# File lib/rakeoe/key_value_reader.rb, line 163
def merge(a_hash)
  @env.merge!(a_hash) { |key, v1, v2| v1 }
end
set(key, value) click to toggle source

Sets a value for key

@param [String] key Key that should be used for operation @param [String] value Value that should be used for operation

# File lib/rakeoe/key_value_reader.rb, line 153
def set(key, value)
  @env[key] = value
end
values() click to toggle source

Returns all values (i.e. right handed side of the parsed key/values)

@return [Array] all values

# File lib/rakeoe/key_value_reader.rb, line 125
def values
  @env.values
end