class StubRequests::Concerns::Property::Validator

Class Validator provides validation for adding properties

@author Mikael Henriksson <mikael@zoolutions.se> @since 0.1.2

Attributes

default[R]

@!attribute [r] default

@return [Object] the default value of the property
name[R]

@!attribute [r] name

@return [Symbol] the name of the property
properties[R]

@!attribute [r] properties

@return [Hash] the list of currently defined properties
type[R]

@!attribute [r] type

@return [Class, Module] the type of the property

Public Class Methods

call(name, type, default, properties) click to toggle source

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
new(name, type, default = nil, properties = {}) click to toggle source

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

run_validations() click to toggle source

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_default() click to toggle source

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_name() click to toggle source

Validates that the name is of type Symbol

@raise [InvalidArgumentType] when name is not a Symbol

@return [void]

# File lib/stub_requests/concerns/property/validator.rb, line 102
def validate_name
  validate! name: :name, value: name, type: Symbol
end
validate_undefined() click to toggle source

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