Class: Bureaucrat::Validators::RegexValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/bureaucrat/validators.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options = {})
  @regex = Regexp.new(options.fetch(:regex, ''))
  @message = options.fetch(:message, 'Enter a valid value.')
  @code = options.fetch(:code, :invalid)
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



9
10
11
# File 'lib/bureaucrat/validators.rb', line 9

def code
  @code
end

#messageObject

Returns the value of attribute message.



9
10
11
# File 'lib/bureaucrat/validators.rb', line 9

def message
  @message
end

#regexObject

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