Class: Waw::Validation::RegexpValidator

Inherits:
Validator show all
Defined in:
lib/waw/validation/regexp_validator.rb

Overview

Validator that checks that values match a given regular expression

Instance Method Summary collapse

Methods inherited from Validator

#&, #===, #=~, #not, #|

Methods included from Helpers

#all_missing?, #any_missing?, #argument_safe, #error, #is_missing?, #missings_to_nil, #no_missing?, #to_validator

Constructor Details

#initialize(regexp, strip = true) ⇒ RegexpValidator

Creates a validator instance.

If strip is set to true, the regexp is checked against a stripped version of value.to_s. In this case, the conversion strips the string as well.



13
14
15
# File 'lib/waw/validation/regexp_validator.rb', line 13

def initialize(regexp, strip = true)
  @regexp, @strip = regexp, strip
end

Instance Method Details

#convert_and_validate(*values) ⇒ Object

Converts and validate method



23
24
25
# File 'lib/waw/validation/regexp_validator.rb', line 23

def convert_and_validate(*values)
  validate(*values) ? [true, values.collect{|v| @strip ? v.to_s.strip : v}] : [false, values]
end

#validate(*values) ⇒ Object

Validation method



18
19
20
# File 'lib/waw/validation/regexp_validator.rb', line 18

def validate(*values)
  values.all?{|v| @regexp =~ (@strip ? v.to_s.strip : v.to_s)}
end