Class: RakeOE::KeyValueReader

Inherits:
Object
  • Object
show all
Defined in:
lib/rakeoe/key_value_reader.rb

Overview

“var6” = ‘val8’

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#envObject

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

Parameters:

  • file_name (String)

    Filename to be used for operation

Returns:

  • (Hash)

    A hash containing all parsed key/value pairs



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
95
96
# 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!

    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



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

Parameters:

  • key (String)

    Key that should be used for operation

  • value (String)

    Value that should be used for operation



159
160
161
162
163
164
165
# File 'lib/rakeoe/key_value_reader.rb', line 159

def add(key, value)
  if @env.has_key?(key)
    @env[key] += value
  else
    set(key,value)
  end
end

#dumpObject

Dumps all parsed variables as debugging aid



169
170
171
172
173
174
# File 'lib/rakeoe/key_value_reader.rb', line 169

def dump
  puts "#{@file_name}"
  @env.each_pair do |key, value|
    puts "[#{key}]: #{value}"
  end
end

#fileString

Returns filename of read file

Returns:

  • (String)

    File name



120
121
122
# File 'lib/rakeoe/key_value_reader.rb', line 120

def file
  @file_name
end

#get(key) ⇒ String

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

Parameters:

  • key (String)

    Key that should be used for operation

Returns:

  • (String)

    Value of given key



130
131
132
# File 'lib/rakeoe/key_value_reader.rb', line 130

def get(key)
  @env[key] || ''
end

#keysArray

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

Returns:

  • (Array)

    all keys



103
104
105
# File 'lib/rakeoe/key_value_reader.rb', line 103

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.

Parameters:

  • a_hash

    Hash of Key/Value pairs

Returns:

  • the



150
151
152
# File 'lib/rakeoe/key_value_reader.rb', line 150

def merge(a_hash)
  @env.merge!(a_hash) { |key, v1, v2| v1 }
end

#set(key, value) ⇒ Object

Sets a value for key

Parameters:

  • key (String)

    Key that should be used for operation

  • value (String)

    Value that should be used for operation



140
141
142
# File 'lib/rakeoe/key_value_reader.rb', line 140

def set(key, value)
  @env[key] = value
end

#valuesArray

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

Returns:

  • (Array)

    all values



112
113
114
# File 'lib/rakeoe/key_value_reader.rb', line 112

def values
  @env.values
end