Module: LegacyFacter::Util::Values

Included in:
Confine
Defined in:
lib/facter/custom_facts/util/values.rb

Overview

A util module for custom_facts containing helper methods

Defined Under Namespace

Classes: DeepFreezeError, DeepMergeError

Class Method Summary collapse

Class Method Details

.convert(value) ⇒ Object



74
75
76
77
78
# File 'lib/facter/custom_facts/util/values.rb', line 74

def convert(value)
  value = value.to_s if value.is_a?(Symbol)
  value = value.downcase if value.is_a?(String)
  value
end

.deep_freeze(value) ⇒ void

This method returns an undefined value.

Duplicate and deeply freeze a given data structure

Parameters:

  • value (Object)

    The structure to freeze



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/facter/custom_facts/util/values.rb', line 15

def deep_freeze(value)
  case value
  when Numeric, Symbol, TrueClass, FalseClass, NilClass
    # These are immutable values, we can safely ignore them
    value
  when String
    value.dup.freeze
  when Array
    value.map do |entry|
      deep_freeze(entry)
    end.freeze
  when Hash
    value.each_with_object({}) do |(key, val), hash|
      hash[deep_freeze(key)] = deep_freeze(val)
    end.freeze
  else
    raise DeepFreezeError, "Cannot deep freeze #{value}:#{value.class}"
  end
end

.deep_merge(left, right, path = []) ⇒ Object

Perform a deep merge of two nested data structures.

Parameters:

  • left (Object)
  • right (Object)
  • path (Array<String>) (defaults to: [])

    The traversal path followed when merging nested hashes

Returns:

  • (Object)

    The merged data structure.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/facter/custom_facts/util/values.rb', line 44

def deep_merge(left, right, path = [])
  ret = nil

  if left.is_a?(Hash) && right.is_a?(Hash)
    ret = left.merge(right) do |key, left_val, right_val|
      path.push(key)
      merged = deep_merge(left_val, right_val, path)
      path.pop
      merged
    end
  elsif left.is_a?(Array) && right.is_a?(Array)
    ret = left.dup.concat(right)
  elsif right.nil?
    ret = left
  elsif left.nil?
    ret = right
  elsif left.nil? && right.nil?
    ret = nil
  else
    msg = "Cannot merge #{left.inspect}:#{left.class} and #{right.inspect}:#{right.class}"
    unless path.empty?
      msg << ' at root'
      msg << path.map { |part| "[#{part.inspect}]" }.join
    end
    raise DeepMergeError, msg
  end

  ret
end

.flatten_structure(path, structure) ⇒ Hash

Flatten the given data structure to something that’s suitable to return as flat facts.

Parameters:

  • path (String)

    The fact path to be prefixed to the given value.

  • structure (Object)

    The data structure to flatten. Nested hashes will be recursively flattened, everything else will be returned as-is.

Returns:

  • (Hash)

    The given data structure prefixed with the given path



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/facter/custom_facts/util/values.rb', line 88

def flatten_structure(path, structure)
  results = {}

  if structure.is_a? Hash
    structure.each_pair do |name, value|
      new_path = "#{path}_#{name}".gsub(%r{-|/}, '_')
      results.merge! flatten_structure(new_path, value)
    end
  elsif structure.is_a? Array
    structure.each_with_index do |value, index|
      new_path = "#{path}_#{index}"
      results.merge! flatten_structure(new_path, value)
    end
  else
    results[path] = structure
  end

  results
end