class Strelka::ParamValidator::RegexpConstraint
A constraint expressed as a regular expression.
Attributes
pattern[R]
The constraint's pattern
Public Class Methods
new( name, pattern, *args, &block )
click to toggle source
Create a new RegexpConstraint
that will validate the field of the given name
with the specified pattern
.
Calls superclass method
Strelka::ParamValidator::Constraint::new
# File lib/strelka/paramvalidator.rb, line 253 def initialize( name, pattern, *args, &block ) @pattern = pattern super( name, *args, &block ) end
Public Instance Methods
check( value )
click to toggle source
Check the value
against the regular expression and return its match groups if successful.
Calls superclass method
Strelka::ParamValidator::Constraint#check
# File lib/strelka/paramvalidator.rb, line 270 def check( value ) self.log.debug "Validating %p via regexp %p" % [ value, self.pattern ] match = self.pattern.match( value.to_s ) or return nil if match.captures.empty? self.log.debug " no captures, using whole match: %p" % [match[0]] return super( match[0] ) elsif match.names.length > 1 self.log.debug " extracting hash of named captures: %p" % [ match.names ] rhash = self.matched_hash( match ) return super( rhash ) elsif match.captures.length == 1 self.log.debug " extracting one capture: %p" % [match.captures.first] return super( match.captures.first ) else self.log.debug " extracting multiple captures: %p" % [match.captures] values = match.captures return super( values ) end end
matched_hash( match )
click to toggle source
Return a Hash of the given match
object's named captures.
# File lib/strelka/paramvalidator.rb, line 296 def matched_hash( match ) return match.names.inject( {} ) do |accum,name| value = match[ name ] accum[ name.to_sym ] = value accum end end
validator_description()
click to toggle source
Return the constraint expressed as a String.
# File lib/strelka/paramvalidator.rb, line 306 def validator_description return "a value matching the pattern %p" % [ self.pattern ] end