class StubRequests::Concerns::Property::Validator
Class
Validator
provides validation for adding properties
@author Mikael Henriksson <mikael@zoolutions.se> @since 0.1.2
Attributes
@!attribute [r] default
@return [Object] the default value of the property
@!attribute [r] name
@return [Symbol] the name of the property
@!attribute [r] properties
@return [Hash] the list of currently defined properties
@!attribute [r] type
@return [Class, Module] the type of the property
Public Class Methods
Validates that the property can be added to the class
@param [Symbol] name the name of the property @param [Class, Module] type the type of the property @param [Object] default the default value of the property @param [Hash] properties the list of currently defined properties
@raise [InvalidArgumentType] when name is not a Symbol
@raise [InvalidArgumentType] when default does not match type @raise [PropertyDefined] when property has already been defined
@return [void]
# File lib/stub_requests/concerns/property/validator.rb, line 42 def self.call(name, type, default, properties) new(name, type, default, properties).run_validations end
Initializes a new {Validator}
@param [Symbol] name the name of the property @param [Class, Module] type the type of the property @param [Object] default the default value of the property @param [Hash] properties the list of currently defined properties
# File lib/stub_requests/concerns/property/validator.rb, line 70 def initialize(name, type, default = nil, properties = {}) @type = Array(type).flatten @default = default @name = name @properties = properties || {} end
Public Instance Methods
Performs all validations
@raise [InvalidArgumentType] when name is not a Symbol
@raise [InvalidArgumentType] when default does not match type @raise [PropertyDefined] when property has already been defined
@return [void]
# File lib/stub_requests/concerns/property/validator.rb, line 87 def run_validations validate_undefined validate_name validate_default end
Private Instance Methods
Validate that the default value matches the type
@raise [InvalidArgumentType] when default does not match type
@return [void]
# File lib/stub_requests/concerns/property/validator.rb, line 114 def validate_default return unless default || default.is_a?(FalseClass) validate! name: :default, value: default, type: type end
Validate that the property has not been defined
@raise [PropertyDefined] when property has already been defined
@return [void]
# File lib/stub_requests/concerns/property/validator.rb, line 128 def validate_undefined return unless properties return unless (prop = properties[name]) raise PropertyDefined, name: name, type: prop[:type], default: prop[:default] end