class Strelka::ParamValidator::Constraint
The base constraint type.
Constants
- FLAGS
Flags that are passed as Symbols when declaring a parameter
- TYPES
Map of constraint specification types to their equivalent
Constraint
class.
Attributes
The constraint's check block
The field's description
The name of the field the constraint governs
Public Class Methods
Return a Constraint
object appropriate for the given field
and spec
.
# File lib/strelka/paramvalidator.rb, line 95 def self::for( field, spec=nil, *options, &block ) self.log.debug "Building Constraint for %p (%p)" % [ field, spec ] # Handle omitted constraint if spec.is_a?( String ) || FLAGS.include?( spec ) options.unshift( spec ) spec = nil end spec ||= block subtype = TYPES[ spec.class ] or raise "No constraint type for a %p validation spec" % [ spec.class ] return subtype.new( field, spec, *options, &block ) end
Create a new Constraint
for the field with the given name
, configuring it with the specified args
. The block
is what does the actual validation, at least in the base class.
# File lib/strelka/paramvalidator.rb, line 116 def initialize( name, *args, &block ) @name = name @block = block @description = args.shift if args.first.is_a?( String ) @required = args.include?( :required ) @multiple = args.include?( :multiple ) end
Register the given subclass
as the Constraint
class to be used when the specified syntax_class
is given as the constraint in a parameter declaration.
# File lib/strelka/paramvalidator.rb, line 87 def self::register_type( syntax_class ) self.log.debug "Registering %p as the constraint class for %p objects" % [ self, syntax_class ] TYPES[ syntax_class ] = self end
Public Instance Methods
Comparison operator – Constraints are equal if they’re for the same field, they’re of the same type, and their blocks are the same.
# File lib/strelka/paramvalidator.rb, line 162 def ==( other ) return self.name == other.name && other.instance_of?( self.class ) && self.block == other.block end
Check the given value against the constraint and return the result if it passes.
# File lib/strelka/paramvalidator.rb, line 151 def apply( value ) if self.multiple? return self.check_multiple( value ) else return self.check( value ) end end
Get the description of the field.
# File lib/strelka/paramvalidator.rb, line 170 def description return @description || self.generate_description end
Returns true if the field can have multiple values.
# File lib/strelka/paramvalidator.rb, line 142 attr_predicate :multiple?
Returns true if the field associated with the constraint is required in order for the parameters to be valid.
# File lib/strelka/paramvalidator.rb, line 147 attr_predicate :required?
Return the constraint expressed as a String.
# File lib/strelka/paramvalidator.rb, line 176 def to_s desc = self.validator_description flags = [] flags << 'required' if self.required? flags << 'multiple' if self.multiple? desc << " (%s)" % [ flags.join(',') ] unless flags.empty? return desc end
Protected Instance Methods
Check the specified value against the constraint and return the results. By default, this just calls to_proc and the block and calls the result with the value as its argument.
# File lib/strelka/paramvalidator.rb, line 209 def check( value ) return self.block.to_proc.call( value ) if self.block return value end
Check the given values
against the constraint and return the results if all of them succeed.
# File lib/strelka/paramvalidator.rb, line 217 def check_multiple( values ) values = [ values ] unless values.is_a?( Array ) results = [] values.each do |value| result = self.check( value ) or return nil results << result end return results end
Generate a description from the name of the field.
# File lib/strelka/paramvalidator.rb, line 231 def generate_description self.log.debug "Auto-generating description for %p" % [ self ] desc = self.name.to_s. gsub( /.*\[(\w+)\]/, "\\1" ). gsub( /_(.)/ ) {|m| " " + m[1,1].upcase }. gsub( /^(.)/ ) {|m| m.upcase } self.log.debug " generated: %p" % [ desc ] return desc end
Return a description of the validation provided by the constraint object.
# File lib/strelka/paramvalidator.rb, line 194 def validator_description desc = 'a custom validator' if self.block location = self.block.source_location desc += " on line %d of %s" % [ location[1], location[0] ] end return desc end