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].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:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/facter/custom_facts/util/normalization.rb', line 17

def normalize(value)
  case value
  when Integer, Float, TrueClass, FalseClass, NilClass, Symbol, Date
    value
  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:



78
79
80
81
82
# File 'lib/facter/custom_facts/util/normalization.rb', line 78

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:



90
91
92
# File 'lib/facter/custom_facts/util/normalization.rb', line 90

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:



52
53
54
55
56
57
# File 'lib/facter/custom_facts/util/normalization.rb', line 52

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