Strelka::ParamValidator::

Constraint

class
Superclass
Object
Extended With
Loggability
Strelka::MethodUtilities

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

block[R]

The constraint's check block

description[W]

The field's description

name[R]

The name of the field the constraint governs

Public Class Methods

anchor
for( field, spec=nil, *options, &block )

Return a Constraint object appropriate for the given field and spec.

# File lib/strelka/paramvalidator.rb, line 98
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
anchor
new( name, *args, &block )

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 119
def initialize( name, *args, &block )
        @name                 = name
        @block                = block

        @description = args.shift if args.first.is_a?( String )

        @required     = args.include?( :required )
        @untaint      = args.include?( :untaint )
        @multiple     = args.include?( :multiple )
end
anchor
register_type( syntax_class )

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 90
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

anchor
==( other )

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 172
def ==( other )
        return self.name == other.name &&
                other.instance_of?( self.class ) &&
                self.block == other.block
end
anchor
apply( value, force_untaint=false )

Check the given value against the constraint and return the result if it passes.

# File lib/strelka/paramvalidator.rb, line 159
def apply( value, force_untaint=false )
        untaint = self.untaint? || force_untaint

        if self.multiple?
                return self.check_multiple( value, untaint )
        else
                return self.check( value, untaint )
        end
end
anchor
description()

Get the description of the field.

# File lib/strelka/paramvalidator.rb, line 180
def description
        return @description || self.generate_description
end
anchor
multiple?()

Returns true if the field can have multiple values.

# File lib/strelka/paramvalidator.rb, line 146
attr_predicate :multiple?
anchor
required?()

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 151
attr_predicate :required?
anchor
to_s()

Return the constraint expressed as a String.

# File lib/strelka/paramvalidator.rb, line 186
def to_s
        desc = self.validator_description

        flags = []
        flags << 'required' if self.required?
        flags << 'multiple' if self.multiple?
        flags << 'untaint' if self.untaint?

        desc << " (%s)" % [ flags.join(',') ] unless flags.empty?

        return desc
end
anchor
untaint?()

Returns true if the constraint will also untaint its result before returning it.

# File lib/strelka/paramvalidator.rb, line 155
attr_predicate :untaint?

Protected Instance Methods

anchor
check( value, untaint )

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 220
def check( value, untaint )
        return self.block.to_proc.call( value ) if self.block
        value.untaint if untaint && value.respond_to?( :untaint )
        return value
end
anchor
check_multiple( values, untaint )

Check the given values against the constraint and return the results if all of them succeed.

# File lib/strelka/paramvalidator.rb, line 229
def check_multiple( values, untaint )
        values = [ values ] unless values.is_a?( Array )
        results = []

        values.each do |value|
                result = self.check( value, untaint ) or return nil
                results << result
        end

        return results
end
anchor
generate_description()

Generate a description from the name of the field.

# File lib/strelka/paramvalidator.rb, line 243
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
anchor
validator_description()

Return a description of the validation provided by the constraint object.

# File lib/strelka/paramvalidator.rb, line 205
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