Class: RegexSettingValidator

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

Constant Summary collapse

LOREM =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eget sem non elit tincidunt rhoncus."

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ RegexSettingValidator

Returns a new instance of RegexSettingValidator.



7
8
9
# File 'lib/validators/regex_setting_validator.rb', line 7

def initialize(opts = {})
  @opts = opts
end

Instance Method Details

#error_messageObject



24
25
26
# File 'lib/validators/regex_setting_validator.rb', line 24

def error_message
  I18n.t("site_settings.errors.invalid_regex")
end

#valid_regex?(val) ⇒ Boolean

Check that string is a valid regex, and that it doesn’t match most of the lorem string.

Returns:

  • (Boolean)


16
17
18
19
20
21
22
# File 'lib/validators/regex_setting_validator.rb', line 16

def valid_regex?(val)
  r = Regexp.new(val)
  matches = r.match(LOREM)
  matches.nil? || matches[0].length < (LOREM.length - 10)
rescue StandardError
  false
end

#valid_value?(val) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/validators/regex_setting_validator.rb', line 11

def valid_value?(val)
  !val.present? || valid_regex?(val)
end