Class: Trafaret::String
Direct Known Subclasses
Email
Instance Attribute Summary
Attributes inherited from Validator
#converters, #options
Instance Method Summary
collapse
Methods inherited from Validator
#&, #===, #add, #call, #failure, #initialize, #perform_convert, #to, #|
Instance Method Details
#convert(data) ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/trafaret/validators.rb', line 71
def convert(data)
if data.is_a? MatchData
data.string
else
data
end
end
|
#prepare ⇒ Object
54
55
56
|
# File 'lib/trafaret/validators.rb', line 54
def prepare
@regex = ::Regexp.compile @options[:regex] if @options[:regex]
end
|
#validate(data) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/trafaret/validators.rb', line 58
def validate(data)
return failure('Not a String') unless data.is_a? ::String
return failure('Should not be blank') if !@options[:allow_blank] && data.empty?
return failure('Too short') if @options[:min_length] && data.size < @options[:min_length]
return failure('Too long') if @options[:max_length] && data.size > @options[:max_length]
if @regex
match = @regex.match(data)
return failure('Does not match') unless match
return match
end
data
end
|