Module: Dployr::Utils

Included in:
Config::Instance, Configuration
Defined in:
lib/dployr/utils.rb

Constant Summary collapse

MERGE_OPTIONS =
{ merge_hash_arrays: false, knockout_prefix: false }

Class Method Summary collapse

Class Method Details

.deep_copy(o) ⇒ Object



45
46
47
# File 'lib/dployr/utils.rb', line 45

def deep_copy(o)
  Marshal.load Marshal.dump o
end

.deep_merge(target = {}, *origins) ⇒ Object



38
39
40
41
42
43
# File 'lib/dployr/utils.rb', line 38

def deep_merge(target = {}, *origins)
  origins.each do |h|
    target.deep_merge! h, MERGE_OPTIONS if h.is_a? Hash
  end
  target
end

.get_by_key(hash, key) ⇒ Object



15
16
17
18
19
# File 'lib/dployr/utils.rb', line 15

def get_by_key(hash, key)
  if hash.is_a? Hash and key
    hash[key] or hash[key.to_sym] or hash[key.to_s]
  end
end

.get_real_key(hash, key) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dployr/utils.rb', line 21

def get_real_key(hash, key)
  if key and hash.is_a? Hash
    if hash.key? key
      key
    elsif hash.key? key.to_sym
      key.to_sym
    elsif hash.key? key.to_s
      key.to_s
    end
  end
end

.has(hash, key) ⇒ Object



10
11
12
13
# File 'lib/dployr/utils.rb', line 10

def has(hash, key)
  (hash.is_a? Hash and
    (hash.key? key or hash.key? key.to_sym or hash.key? key.to_s))
end

.merge(target, *origins) ⇒ Object



33
34
35
36
# File 'lib/dployr/utils.rb', line 33

def merge(target, *origins)
  origins.each { |h| target = target.merge h }
  target
end

.replace_env_vars(str) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/dployr/utils.rb', line 65

def replace_env_vars(str)
  str.gsub(/\$\{(\w+)\}/) do
    if ENV.key? $1
      ENV[$1]
    else
      ''
    end
  end
end

.replace_placeholders(str, data) ⇒ Object



75
76
77
# File 'lib/dployr/utils.rb', line 75

def replace_placeholders(str, data)
  str % data if data.is_a? Hash or data.is_a? Array
end

.replace_vars(str) ⇒ Object



49
50
51
# File 'lib/dployr/utils.rb', line 49

def replace_vars(str)
  str.gsub(/\%\{(\w+)\}/) { yield $1 }
end

.template(str, data) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dployr/utils.rb', line 53

def template(str, data)
  raise ArgumentError.new 'Data must be a hash' unless data.is_a? Hash
  replace_vars str do |match|
    key = get_real_key data, match
    if key
      data[key]
    else
      raise ArgumentError.new "Missing template variable: #{match}"
    end
  end
end

.traverse_map(hash, &block) ⇒ Object



79
80
81
# File 'lib/dployr/utils.rb', line 79

def traverse_map(hash, &block)
  traverse_mapper hash, nil, &block
end

.traverse_mapper(hash, key, &block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/dployr/utils.rb', line 83

def traverse_mapper(hash, key, &block)
  case hash
  when String
    hash = yield hash, key
  when Array
    hash.map! { |item| traverse_mapper item, nil, &block }
  when Hash
    buf = {}
    hash.each do |k, v|
      if k.is_a? String
        new_key = yield k, k
        if new_key != k
          hash.delete k
          buf[new_key] = traverse_mapper v, new_key, &block
          next
        end
      end
      hash[k] = traverse_mapper v, k, &block
    end
    hash.merge! buf
  end
  hash
end