Class: Bureaucrat::Validators::RegexValidator
- Inherits:
-
Object
- Object
- Bureaucrat::Validators::RegexValidator
- Defined in:
- lib/bureaucrat/validators.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#message ⇒ Object
Returns the value of attribute message.
-
#regex ⇒ Object
Returns the value of attribute regex.
Instance Method Summary collapse
-
#call(value) ⇒ Object
Validates that the input validates the regular expression.
-
#initialize(options = {}) ⇒ RegexValidator
constructor
A new instance of RegexValidator.
Constructor Details
#initialize(options = {}) ⇒ RegexValidator
Returns a new instance of RegexValidator.
11 12 13 14 15 |
# File 'lib/bureaucrat/validators.rb', line 11 def initialize( = {}) @regex = Regexp.new(.fetch(:regex, '')) @message = .fetch(:message, 'Enter a valid value.') @code = .fetch(:code, :invalid) end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
9 10 11 |
# File 'lib/bureaucrat/validators.rb', line 9 def code @code end |
#message ⇒ Object
Returns the value of attribute message.
9 10 11 |
# File 'lib/bureaucrat/validators.rb', line 9 def @message end |
#regex ⇒ Object
Returns the value of attribute regex.
9 10 11 |
# File 'lib/bureaucrat/validators.rb', line 9 def regex @regex end |
Instance Method Details
#call(value) ⇒ Object
Validates that the input validates the regular expression
18 19 20 21 22 |
# File 'lib/bureaucrat/validators.rb', line 18 def call(value) if regex !~ value raise ValidationError.new(@message, code, regex: regex) end end |