Module: Facter::Util::Values

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

Overview

A util module for facter containing helper methods

Defined Under Namespace

Classes: DeepFreezeError, DeepMergeError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.convert(value) ⇒ Object



74
75
76
77
78
# File 'lib/facter/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



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

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.inject({}) do |hash, (key, value)|
      hash[deep_freeze(key)] = deep_freeze(value)
      hash
    end.freeze
  else
    raise DeepFreezeError, "Cannot deep freeze #{value}:#{value.class}"
  end
end

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

Perform a deep merge of two nested data structures.



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/util/values.rb', line 44

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

  if left.is_a? Hash and 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 and right.is_a? Array
    ret = left.dup.concat(right)
  elsif right.nil?
    ret = left
  elsif left.nil?
    ret = right
  elsif left.nil? and right.nil?
    ret = nil
  else
    msg = "Cannot merge #{left.inspect}:#{left.class} and #{right.inspect}:#{right.class}"
    if not 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.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/facter/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(/\-|\//, '_')
      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

Instance Method Details

#convert(value) ⇒ Object (private)



74
75
76
77
78
# File 'lib/facter/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 (private)

This method returns an undefined value.

Duplicate and deeply freeze a given data structure



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

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.inject({}) do |hash, (key, value)|
      hash[deep_freeze(key)] = deep_freeze(value)
      hash
    end.freeze
  else
    raise DeepFreezeError, "Cannot deep freeze #{value}:#{value.class}"
  end
end

#deep_merge(left, right, path = [], &block) ⇒ Object (private)

Perform a deep merge of two nested data structures.



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/util/values.rb', line 44

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

  if left.is_a? Hash and 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 and right.is_a? Array
    ret = left.dup.concat(right)
  elsif right.nil?
    ret = left
  elsif left.nil?
    ret = right
  elsif left.nil? and right.nil?
    ret = nil
  else
    msg = "Cannot merge #{left.inspect}:#{left.class} and #{right.inspect}:#{right.class}"
    if not path.empty?
      msg << " at root"
      msg << path.map { |part| "[#{part.inspect}]" }.join
    end
    raise DeepMergeError, msg
  end

  ret
end

#flatten_structure(path, structure) ⇒ Hash (private)

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



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/facter/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(/\-|\//, '_')
      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