Module: Ant::Configuration::Utils

Overview

This module provides some operations with strings and hashes that are commonly used while parsing or merging strings

Instance Method Summary collapse

Instance Method Details

#array_wrap(value) ⇒ Object

Ensures that a value is an array. If it is not an array it wraps it inside an array

Examples

array_wrap(1) => [1]
array_wrap([1, 2, 3]) => [1, 2, 3]


13
14
15
# File 'lib/ant/configs/utils.rb', line 13

def array_wrap(value)
  value.is_a?(Array) ? value : [value]
end

#parse_type(string) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/ant/configs/utils.rb', line 74

def parse_type(string)
  case string.downcase
  when 'true'
    true
  when 'false'
    false
  else
    string
  end
end

#recursive_merge(receiver, sender) ⇒ Object

Merges two hashes into one, but if a key is also a hash it will merge it too. This is applied recursively

Examples

a = { a: 1, b: { c: 2 } }
b = { b: { d: 3 }, e: 4}
recursive_merge(a, b) => { a: 1, b: { c: 2, d: 3 }, e: 4}

rubocop: disable AbcSize



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ant/configs/utils.rb', line 24

def recursive_merge(receiver, sender)
  return receiver if sender.nil?

  result = receiver.dup
  (receiver.keys + sender.keys).each do |key|
    value = if receiver[key].is_a?(Hash)
              recursive_merge(receiver[key], sender[key])
            elsif receiver[key].is_a?(Array)
              # TODO: Enable merging arrays
              sender[key]
            else
              sender[key]
            end
    result[key] = value if value
  end
  result
end

#recursive_set(hash, key, value) ⇒ Object

Takes a hash, an array and a value It will traverse recursively into the hash and create a key inside the hash using the string inside key

Examples

recursive_set({}, %w[hello key], 3) => { 'hello' => { 'key' => 3 } }


48
49
50
51
52
53
54
55
56
57
# File 'lib/ant/configs/utils.rb', line 48

def recursive_set(hash, key, value)
  current = key[0]
  if key.size == 1
    hash[current] = value
    hash
  else
    hash[current] ||= {}
    recursive_set(hash[current], key[1..-1], value)
  end
end

#split_env_string(string) ⇒ Object

This method is used to parse values from vars passed from ENV or ARGV. Currently all the values are passed as either string or Array of String. The delimiter used is ‘,’ and it allows to escape it

Examples

split_env_string('hello') => 'hello'
split_env_string('hello, world') => ['hello', 'world']
split_env_string('hello\, world') => 'hello, world'


66
67
68
69
70
71
72
# File 'lib/ant/configs/utils.rb', line 66

def split_env_string(string)
  # TODO: parse type of string
  strings = string.split(/(?<!\\),/)
                  .map { |str| str.gsub('\,', ',') }
                  .map { |str| parse_type(str) }
  strings.size == 1 ? strings.first : strings
end

#symbolize(hash) ⇒ Object



85
86
87
# File 'lib/ant/configs/utils.rb', line 85

def symbolize(hash)
  hash.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
end