Module: Facter::Util::Normalization

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

Defined Under Namespace

Classes: NormalizationError

Constant Summary collapse

VALID_TYPES =
[Integer, Float, TrueClass, FalseClass, NilClass, String, Array, Hash]

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:



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

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

Raises:



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

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:

Raises:



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

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

Instance Method Details

#normalize_string(value) ⇒ Object



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

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