class Hermod::Validators::RegularExpression

Checks the value matches the given regular expression

Attributes

pattern[R]

Public Class Methods

new(pattern) click to toggle source

Sets up the pattern the value is expected to match

# File lib/hermod/validators/regular_expression.rb, line 10
def initialize(pattern)
  @pattern = pattern
end

Private Instance Methods

message(value, attributes) click to toggle source
# File lib/hermod/validators/regular_expression.rb, line 24
def message(value, attributes)
  "#{value.inspect} does not match #{pattern.inspect}"
end
test(value, attributes) click to toggle source

Public: Checks the value matches the pattern. Blank values are ignored because those are checked by the ValuePresence validator if necessary.

Returns a boolean

# File lib/hermod/validators/regular_expression.rb, line 20
def test(value, attributes)
  value.blank? || value =~ pattern
end