Module: LegacyFacter::Util::Normalization Private

Defined in:
lib/facter/custom_facts/util/normalization.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Classes: NormalizationError

Constant Summary collapse

VALID_TYPES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[Integer, Float, TrueClass, FalseClass, NilClass, Symbol, String, Array, Hash, Date, Time].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.normalize(value) ⇒ void

This method returns an undefined value.

Recursively normalize the given data structure

Raises:



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/facter/custom_facts/util/normalization.rb', line 20

def normalize(value)
  case value
  when Integer, Float, TrueClass, FalseClass, NilClass, Symbol
    value
  when Date, Time
    value.iso8601
  when String
    normalize_string(value)
  when Array
    normalize_array(value)
  when Hash
    normalize_hash(value)
  else
    raise NormalizationError, "Expected #{value} to be one of #{VALID_TYPES.inspect}, but was #{value.class}"
  end
end

.normalize_array(value) ⇒ void

This method returns an undefined value.

Validate all elements of the array.

Parameters:

  • value (Array)

Raises:



83
84
85
86
87
# File 'lib/facter/custom_facts/util/normalization.rb', line 83

def normalize_array(value)
  value.collect do |elem|
    normalize(elem)
  end
end

.normalize_hash(value) ⇒ void

This method returns an undefined value.

Validate all keys and values of the hash.

Parameters:

  • value (Hash)

Raises:



95
96
97
# File 'lib/facter/custom_facts/util/normalization.rb', line 95

def normalize_hash(value)
  Hash[value.collect { |k, v| [normalize(k), normalize(v)] }]
end

Instance Method Details

#normalize_string(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:



57
58
59
60
61
62
# File 'lib/facter/custom_facts/util/normalization.rb', line 57

def normalize_string(value)
  converted = Iconv.conv('UTF-8//IGNORE', 'UTF-8', value)
  raise NormalizationError, "String #{value.inspect} is not valid UTF-8" if converted != value

  value
end