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
# 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

Parameters:

  • key (String)

    Key that should be used for operation

  • value (String)

    Value that should be used for operation



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

#dumpObject

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

#fileString

Returns filename of read file

Returns:

  • (String)

    File name



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

Parameters:

  • key (String)

    Key that should be used for operation

Returns:

  • (String)

    Value of given key



128
129
130
# File 'lib/rakeoe/key_value_reader.rb', line 128

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

#keysArray

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

Returns:

  • (Array)

    all keys



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

Parameters:

  • key (String)

    Key that should be used for operation

  • value (String)

    Value that should be used for operation



138
139
140
# File 'lib/rakeoe/key_value_reader.rb', line 138

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



110
111
112
# File 'lib/rakeoe/key_value_reader.rb', line 110

def values
  @env.values
end