Class: Sinatra::ParamValidator::Validator

Inherits:
Object
  • Object
show all
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

Direct Known Subclasses

Form, UrlParam

Defined Under Namespace

Classes: Form, UrlParam

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

.validatorsObject (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

#errorsObject (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.message]
end

#success?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/sinatra/param_validator/validator.rb', line 26

def success?
  @errors.empty?
end