Module: Copland::Configuration::YAML::TypeValidator

Included in:
ConfigurationPointProcessor, InterceptorProcessor, ListenerProcessor, PackageProcessor, SchemaParser, ServicePointProcessor
Defined in:
lib/copland/configuration/yaml/utils.rb

Overview

This module implements type validation routines that are used by most of the classes in the Copland::Configuration::YAML module.

Instance Method Summary collapse

Instance Method Details

#ensure_element_type(name, element, klass) ⇒ Object

Ensures that the given element (identified by the given name) is indeed an instance of the given klass. If it is not, a ParserError is raised.



47
48
49
50
51
52
53
54
# File 'lib/copland/configuration/yaml/utils.rb', line 47

def ensure_element_type( name, element, klass )
  unless element.is_a?( klass )
    raise ParserError,
      "#{name.inspect} must be of type #{klass}, " +
      "not #{element.class}",
      caller[1..-1]
  end
end

#validate_elements(hash, path = "") ⇒ Object

This requires that the VALID_KEYS and REQUIRED_KEYS constants have been set in the including class. Then, it tests to see if the given hash includes any values other than those that are defined in VALID_KEYS, or whether any keys in REQUIRED_KEYS are missing from hash. If either condition is true, a ParserError is raised. If path is given, it describes the location of the element (for error reporting).



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/copland/configuration/yaml/utils.rb', line 63

def validate_elements( hash, path="" )
  path = " at #{path}" if path
  invalid_keys = hash.keys - self.class::VALID_KEYS
  unless invalid_keys.empty?
    raise ParserError,
      "invalid elements(s)#{path}: #{invalid_keys.inspect}"
  end

  missing_keys = self.class::REQUIRED_KEYS - hash.keys
  unless missing_keys.empty?
    raise ParserError,
      "missing element(s)#{path}: #{missing_keys.inspect}"
  end
end