Class: Digget::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/digget/validator.rb

Overview

This class is the validator superclass containing the logic to cast the parameters and verify the options that are defined on them

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, object = nil) ⇒ Validator

Returns a new instance of Validator.



7
8
9
10
11
12
# File 'lib/digget/validator.rb', line 7

def initialize(params, object = nil)
  @object = object
  @casted_params = {}
  @errors = []
  @params = params
end

Instance Attribute Details

#casted_paramsHash (readonly)

This method returns a hash with the parameters casted to their requested type

Returns:

  • (Hash)

    A hash with as a key the parameter symbol and the casted value as value



20
21
22
# File 'lib/digget/validator.rb', line 20

def casted_params
  @casted_params
end

#errorsArray (readonly)

This method returns an array with all the errors during validation

Returns:

  • (Array)

    An array containing the validation errors



16
17
18
# File 'lib/digget/validator.rb', line 16

def errors
  @errors
end

Instance Method Details

#filter(name, type, options = {}) ⇒ Object



34
35
36
37
38
# File 'lib/digget/validator.rb', line 34

def filter(name, type, options = {})
  param = verify(name, type, options)
  return object unless @errors.empty?
  filter_options(param, name, options)
end

#valid?Boolean

This method is a helper method to determine whether the validation of the parameters was successful

Returns:

  • (Boolean)

    ‘true` if there were no errors during parameter checking, `false` otherwise



42
43
44
# File 'lib/digget/validator.rb', line 42

def valid?
  @errors.empty?
end

#verify(name, type, options = {}) ⇒ Object

This method verifies one of the parameters with the provided options

Parameters:

  • name (Symbol)

    The name of the param in the ‘params` hash

  • type (Class)

    Type that the parameter should be casted to

  • options (Hash) (defaults to: {})

    Validation options for this parameter



26
27
28
29
30
31
32
# File 'lib/digget/validator.rb', line 26

def verify(name, type, options = {})
  casted_param = cast(@params[name], type)
  return unless @errors.empty?
  @casted_params[name] = casted_param
  verify_options(casted_param, name, options)
  casted_param
end