Module: RFacter::Util::Normalization

Defined in:
lib/rfacter/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:



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

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:

  • value (Array)

Raises:



79
80
81
82
83
# File 'lib/rfacter/util/normalization.rb', line 79

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:



91
92
93
# File 'lib/rfacter/util/normalization.rb', line 91

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

Instance Method Details

#normalize_string(value) ⇒ Object



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

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