Module: Bolt::Util

Defined in:
lib/bolt/util.rb,
lib/bolt/util/puppet_log_level.rb

Defined Under Namespace

Modules: PuppetLogLevel

Class Method Summary collapse

Class Method Details

.deep_clone(obj, cloned = {}) ⇒ Object

Performs a deep_clone, using an identical copy if the cloned structure contains multiple references to the same object and prevents endless recursion. Credit to Jan Molic via github.com/rubyworks/facets/blob/master/LICENSE.txt



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bolt/util.rb', line 85

def deep_clone(obj, cloned = {})
  cloned[obj.object_id] if cloned.include?(obj.object_id)

  begin
    cl = obj.clone
  rescue TypeError
    # unclonable (TrueClass, Fixnum, ...)
    cloned[obj.object_id] = obj
    return obj
  else
    cloned[obj.object_id] = cl
    cloned[cl.object_id] = cl

    if cl.is_a? Hash
      obj.each { |k, v| cl[k] = deep_clone(v, cloned) }
    elsif cl.is_a? Array
      cl.collect! { |v| deep_clone(v, cloned) }
    elsif cl.is_a? Struct
      obj.each_pair { |k, v| cl[k] = deep_clone(v, cloned) }
    end

    cl.instance_variables.each do |var|
      v = cl.instance_variable_get(var)
      v_cl = deep_clone(v, cloned)
      cl.instance_variable_set(var, v_cl)
    end

    return cl
  end
end

.deep_merge(hash1, hash2) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/bolt/util.rb', line 36

def deep_merge(hash1, hash2)
  recursive_merge = proc do |_key, h1, h2|
    if h1.is_a?(Hash) && h2.is_a?(Hash)
      h1.merge(h2, &recursive_merge)
    else
      h2
    end
  end
  hash1.merge(hash2, &recursive_merge)
end

.map_vals(hash) ⇒ Object



47
48
49
50
51
# File 'lib/bolt/util.rb', line 47

def map_vals(hash)
  hash.each_with_object({}) do |(k, v), acc|
    acc[k] = yield(v)
  end
end

.read_config_file(path, default_paths = nil, file_name = 'file') ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bolt/util.rb', line 6

def read_config_file(path, default_paths = nil, file_name = 'file')
  logger = Logging.logger[self]
  path_passed = path
  if path.nil? && default_paths
    found_default = default_paths.select { |p| File.exist?(p) }
    if found_default.size > 1
      logger.warn "Found #{file_name}s at #{found_default.join(', ')}, using the first"
    end
    # Use first found, fall back to first default and try to load even if it didn't exist
    path = found_default.first || default_paths.first
  end

  path = File.expand_path(path)
  content = File.open(path, "r:UTF-8") { |f| YAML.safe_load(f.read) }
  logger.debug("Loaded #{file_name} from #{path}")
  content
rescue Errno::ENOENT
  msg = "Could not read #{file_name} file: #{path}"
  if path_passed
    raise Bolt::FileError.new(msg, path)
  else
    logger.debug(msg)
    nil
  end
rescue Psych::Exception
  raise Bolt::FileError.new("Could not parse #{file_name} file: #{path}", path)
rescue IOError, SystemCallError
  raise Bolt::FileError.new("Could not read #{file_name} file: #{path}", path)
end

.symbolize_top_level_keys(hsh) ⇒ Object

Accept hash and return hash with top level keys of type “String” converted to symbols.



122
123
124
# File 'lib/bolt/util.rb', line 122

def symbolize_top_level_keys(hsh)
  hsh.each_with_object({}) { |(k, v), h| k.is_a?(String) ? h[k.to_sym] = v : h[k] = v }
end

.walk_keys(data, &block) ⇒ Object

Accepts a Data object and returns a copy with all hash keys modified by block. use &:to_s to stringify keys or &:to_sym to symbolize them



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bolt/util.rb', line 55

def walk_keys(data, &block)
  if data.is_a? Hash
    data.each_with_object({}) do |(k, v), acc|
      v = walk_keys(v, &block)
      acc[yield(k)] = v
    end
  elsif data.is_a? Array
    data.map { |v| walk_keys(v, &block) }
  else
    data
  end
end

.walk_vals(data, &block) ⇒ Object

Accepts a Data object and returns a copy with all hash and array values Arrays and hashes including the initial object are modified before their descendants are.



71
72
73
74
75
76
77
78
79
80
# File 'lib/bolt/util.rb', line 71

def walk_vals(data, &block)
  data = yield(data)
  if data.is_a? Hash
    map_vals(data) { |v| walk_vals(v, &block) }
  elsif data.is_a? Array
    data.map { |v| walk_vals(v, &block) }
  else
    data
  end
end

.windows?Boolean

Returns true if windows false if not.

Returns:

  • (Boolean)


117
118
119
# File 'lib/bolt/util.rb', line 117

def windows?
  !!File::ALT_SEPARATOR
end