Class: Digget::Validator
- Inherits:
-
Object
- Object
- Digget::Validator
- 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
-
#casted_params ⇒ Hash
readonly
This method returns a hash with the parameters casted to their requested type.
-
#errors ⇒ Array
readonly
This method returns an array with all the errors during validation.
Instance Method Summary collapse
- #filter(name, type, options = {}) ⇒ Object
-
#initialize(params, object = nil) ⇒ Validator
constructor
A new instance of Validator.
-
#valid? ⇒ Boolean
This method is a helper method to determine whether the validation of the parameters was successful.
-
#verify(name, type, options = {}) ⇒ Object
This method verifies one of the parameters with the provided options.
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_params ⇒ Hash (readonly)
This method returns a hash with the parameters casted to their requested type
20 21 22 |
# File 'lib/digget/validator.rb', line 20 def casted_params @casted_params end |
#errors ⇒ Array (readonly)
This method returns an array with all the errors during validation
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, = {}) param = verify(name, type, ) return object unless @errors.empty? (param, name, ) end |
#valid? ⇒ Boolean
This method is a helper method to determine whether the validation of the parameters was successful
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
26 27 28 29 30 31 32 |
# File 'lib/digget/validator.rb', line 26 def verify(name, type, = {}) casted_param = cast(@params[name], type) return unless @errors.empty? @casted_params[name] = casted_param (casted_param, name, ) casted_param end |