Class: Sinatra::ParamValidator::Validator
- Inherits:
-
Object
- Object
- Sinatra::ParamValidator::Validator
- Defined in:
- lib/sinatra/param_validator/validator.rb,
lib/sinatra/param_validator/validator/form.rb,
lib/sinatra/param_validator/validator/url_param.rb
Overview
Definition of a single validator
Defined Under Namespace
Class Attribute Summary collapse
-
.validators ⇒ Object
readonly
Returns the value of attribute validators.
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
Instance Method Summary collapse
- #handle_failure(_context) ⇒ Object
-
#initialize(&definition) ⇒ Validator
constructor
A new instance of Validator.
- #run(context, *args) ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(&definition) ⇒ Validator
Returns a new instance of Validator.
9 10 11 12 |
# File 'lib/sinatra/param_validator/validator.rb', line 9 def initialize(&definition) @definition = definition @errors = {} end |
Class Attribute Details
.validators ⇒ Object (readonly)
Returns the value of attribute validators.
33 34 35 |
# File 'lib/sinatra/param_validator/validator.rb', line 33 def validators @validators end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
7 8 9 |
# File 'lib/sinatra/param_validator/validator.rb', line 7 def errors @errors end |
Class Method Details
.inherited(subclass) ⇒ Object
35 36 37 38 |
# File 'lib/sinatra/param_validator/validator.rb', line 35 def inherited(subclass) super @validators << subclass end |
Instance Method Details
#handle_failure(_context) ⇒ Object
14 15 16 |
# File 'lib/sinatra/param_validator/validator.rb', line 14 def handle_failure(_context) raise ValidationFailedError, @errors end |
#run(context, *args) ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/sinatra/param_validator/validator.rb', line 18 def run(context, *args) context.instance_variable_set(:@_validator_errors, {}) context.instance_exec(*args, &@definition) @errors = context.remove_instance_variable(:@_validator_errors) rescue InvalidParameterError => e @errors[:general] = [e.] end |
#success? ⇒ Boolean
26 27 28 |
# File 'lib/sinatra/param_validator/validator.rb', line 26 def success? @errors.empty? end |