#-- # ============================================================================= # Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu) # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # * The names of its contributors may not be used to endorse or promote # products derived from this software without specific prior written # permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. # ============================================================================= #++ require 'copland/configuration/errors' module Copland module Configuration module YAML # This module implements type validation routines that are used by most # of the classes in the Copland::Configuration::YAML module. module TypeValidator # 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. 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 # 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). 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 end end end end