Class: Contrast::Utils::EnvConfigurationItem

Inherits:
Object
  • Object
show all
Defined in:
lib/contrast/utils/env_configuration_item.rb

Overview

Stores key and value of a environmnt variable and can normalize the path for comparison to common config

Constant Summary collapse

START_UNDERSCORE =
/^(_+)/.cs__freeze
END_UNDERSCORE =
/(_+)$/.cs__freeze
REPEATING_UNDERSCORE =
/_{3,}/.cs__freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, value) ⇒ EnvConfigurationItem

Returns a new instance of EnvConfigurationItem.



15
16
17
18
19
20
21
22
# File 'lib/contrast/utils/env_configuration_item.rb', line 15

def initialize key, value
  key = EnvConfigurationItem.resolve_corrected_path(key)
  @key = key
  @dot_path_array = key.downcase.split(Contrast::Utils::ObjectShare::DOUBLE_UNDERSCORE)
  @value = value

  correct_dot_path
end

Instance Attribute Details

#dot_path_arrayObject (readonly)

Returns the value of attribute dot_path_array.



13
14
15
# File 'lib/contrast/utils/env_configuration_item.rb', line 13

def dot_path_array
  @dot_path_array
end

#keyObject (readonly)

Returns the value of attribute key.



13
14
15
# File 'lib/contrast/utils/env_configuration_item.rb', line 13

def key
  @key
end

#valueObject (readonly)

Returns the value of attribute value.



13
14
15
# File 'lib/contrast/utils/env_configuration_item.rb', line 13

def value
  @value
end

Class Method Details

.resolve_corrected_path(path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/contrast/utils/env_configuration_item.rb', line 39

def self.resolve_corrected_path path
  tmp_path = path.dup
  start_match = START_UNDERSCORE.match(tmp_path)
  tmp_path = tmp_path[start_match.offset(0).last..] if start_match

  tmp_path.gsub!(REPEATING_UNDERSCORE, '__')

  end_match = END_UNDERSCORE.match(tmp_path)
  tmp_path = tmp_path[0...end_match.offset(0).first] if end_match

  tmp_path
end

Instance Method Details

#correct_dot_pathObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/contrast/utils/env_configuration_item.rb', line 24

def correct_dot_path
  return unless @dot_path_array

  # we need to shift by one to access the appropriate level of the config
  @dot_path_array.shift
  # and then we need to fix any rules
  idx = @dot_path_array.find_index('rules')
  return unless idx

  next_idx = idx + 1
  return if next_idx == @dot_path_array.length

  @dot_path_array[next_idx] = @dot_path_array[next_idx].to_s.tr('_', '-')
end