class Raml::Parameter::AbstractParameter
Constants
- VALID_TYPES
Public Class Methods
new(name, parameter_data, parent)
click to toggle source
@param name [String] the parameter name. @param parameter_data [Hash, Array<Hash>] the parameter data. If the parameter supports multiple types,
it should be an array of hashes, one hash each for each type.
@param parent [Raml::Node] the parameter’s parent node.
Calls superclass method
Raml::PropertiesNode::new
# File lib/raml/node/parameter/abstract_parameter.rb, line 65 def initialize(name, parameter_data, parent) if parameter_data.is_a? Array @name = name @children ||= [] parameter_data.each do |parameter| @children << self.class.new(name, parameter, self) end elsif parameter_data.is_a? Hash super end end
Public Instance Methods
has_multiple_types?()
click to toggle source
@return [Boolean] true if the parameter supports multiple type alternatives, false otherwise.
# File lib/raml/node/parameter/abstract_parameter.rb, line 78 def has_multiple_types? not children.empty? end
merge(other)
click to toggle source
@private
Calls superclass method
Raml::Merge#merge
# File lib/raml/node/parameter/abstract_parameter.rb, line 83 def merge(other) raise MergeError, "#{self.class} names don't match." if name != other.name case [ has_multiple_types?, other.has_multiple_types? ] when [ true , true ] match, no_match = other.types.values.partition { |param| types.include? param.type } # Merge parameters with the same type. match = Hash[ match.map { |param| [ param.type, param ] } ] types.each { |type, param| param.merge match[type] if match[type] } # Add parameters with no matching type. @children.concat no_match when [ true , false ] if types[other.type] types[other.type].merge other else @children << other end when [ false, true ] if other.types[self.type] self.merge other.types[self.type] @children << self.clone @children.concat other.types.values.reject { |type| self.type == type.type } reset else @children << self.clone @children.concat other.types.values reset end when [ false, false ] super end self end
Private Instance Methods
reset()
click to toggle source
# File lib/raml/node/parameter/abstract_parameter.rb, line 244 def reset scalar_properties.each { |prop| instance_variable_set "@#{prop}", nil } end
validate_default()
click to toggle source
# File lib/raml/node/parameter/abstract_parameter.rb, line 216 def validate_default validate_value :default end
validate_enum()
click to toggle source
# File lib/raml/node/parameter/abstract_parameter.rb, line 130 def validate_enum if enum if type == 'string' raise InvalidParameterAttribute, "enum attribute must be an array of strings: #{enum} (#{enum.class})" unless enum.is_a?(Array) && enum.all? { |val| val.is_a? String } else raise InapplicableParameterAttribute, 'enum attribute is only applicable to string parameters.' end end end
validate_example()
click to toggle source
# File lib/raml/node/parameter/abstract_parameter.rb, line 200 def validate_example validate_value :example end
validate_max_length()
click to toggle source
# File lib/raml/node/parameter/abstract_parameter.rb, line 168 def validate_max_length if max_length if type != 'string' raise InapplicableParameterAttribute, 'maxLength attributes are applicable only to string parameters.' else raise InvalidParameterAttribute, 'maxLength attributes must be an integer' unless max_length.is_a? Integer end end end
validate_maximum()
click to toggle source
# File lib/raml/node/parameter/abstract_parameter.rb, line 189 def validate_maximum if maximum if %w(integer number).include? type raise InvalidParameterAttribute, 'maximum attribute must be numeric' unless maximum.is_a? Numeric else raise InapplicableParameterAttribute, 'maximum attribute applicable only to number or integer parameters.' end end end
validate_min_length()
click to toggle source
# File lib/raml/node/parameter/abstract_parameter.rb, line 158 def validate_min_length if min_length if type != 'string' raise InapplicableParameterAttribute, 'minLength attributes are applicable only to string parameters.' else raise InvalidParameterAttribute, 'minLength attributes must be an integer' unless min_length.is_a? Integer end end end
validate_minimum()
click to toggle source
# File lib/raml/node/parameter/abstract_parameter.rb, line 178 def validate_minimum if minimum if %w(integer number).include? type raise InvalidParameterAttribute, 'minimum attribute must be numeric' unless minimum.is_a? Numeric else raise InapplicableParameterAttribute, 'minimum attribute applicable only to number or integer parameters.' end end end
validate_pattern()
click to toggle source
# File lib/raml/node/parameter/abstract_parameter.rb, line 141 def validate_pattern if pattern if type == 'string' raise InvalidParameterAttribute, 'pattern attribute must be a string' unless pattern.is_a? String pattern.gsub!(/\\*\^/u) { |m| m.size.odd? ? "#{m.chop}\\A" : m } pattern.gsub!(/\\*\$/u) { |m| m.size.odd? ? "#{m.chop}\\z" : m } begin @pattern = Regexp.new pattern rescue RegexpError raise InvalidParameterAttribute, 'pattern attribute must be a valid regexp' end else raise InapplicableParameterAttribute, 'pattern attribute is only applicable to string parameters.' end end end
validate_repeat()
click to toggle source
# File lib/raml/node/parameter/abstract_parameter.rb, line 204 def validate_repeat unless [true, false].include?(repeat) raise InvalidParameterAttribute, 'repeat attribute must be true or false.' end end
validate_required()
click to toggle source
# File lib/raml/node/parameter/abstract_parameter.rb, line 210 def validate_required unless [true, false].include?(required) raise InvalidParameterAttribute, "required attribute must be true or false: #{required} (#{required.class})" end end
validate_type()
click to toggle source
# File lib/raml/node/parameter/abstract_parameter.rb, line 126 def validate_type raise InvalidParameterType unless VALID_TYPES.include? type end
validate_value(which)
click to toggle source
# File lib/raml/node/parameter/abstract_parameter.rb, line 220 def validate_value(which) val = send which if val err_msg = "#{which} attribute for a %s parameter must be a %s: #{val} (#{val.class})" case type when 'string' raise InvalidParameterAttribute, ( err_msg % [ 'string' , 'string' ] ) unless val.is_a? String when 'number' raise InvalidParameterAttribute, ( err_msg % [ 'number' , 'number' ] ) unless val.is_a? Numeric when 'integer' raise InvalidParameterAttribute, ( err_msg % [ 'integer', 'integer' ] ) unless val.is_a? Integer when 'date' raise InvalidParameterAttribute, ( err_msg % [ 'date' , 'string' ] ) unless val.is_a? String when 'boolean' raise InvalidParameterAttribute, ( err_msg % [ 'boolean', 'boolean' ] ) unless [TrueClass, FalseClass].include? val.class end end end