Class: RegexSettingValidator
- Inherits:
-
Object
- Object
- RegexSettingValidator
- 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
- #error_message ⇒ Object
-
#initialize(opts = {}) ⇒ RegexSettingValidator
constructor
A new instance of RegexSettingValidator.
-
#valid_regex?(val) ⇒ Boolean
Check that string is a valid regex, and that it doesn’t match most of the lorem string.
- #valid_value?(val) ⇒ Boolean
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_message ⇒ Object
24 25 26 |
# File 'lib/validators/regex_setting_validator.rb', line 24 def 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.
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
11 12 13 |
# File 'lib/validators/regex_setting_validator.rb', line 11 def valid_value?(val) !val.present? || valid_regex?(val) end |