Declare::Constraints::Simple::Library::Scalar - Scalar Constraints


Declare-Constraints-Simple documentation Contained in the Declare-Constraints-Simple distribution.

Index


Code Index:

NAME

Top

Declare::Constraints::Simple::Library::Scalar - Scalar Constraints

SYNOPSIS

Top

  # match one of a set of regexes
  my $some_regexes = Matches(qr/foo/, qr/bar/);

  # allow only defined values
  my $is_defined = IsDefined;

  # between 5 and 50 chars
  my $five_to_fifty = HasLength(5, 50);

  # match against a set of values
  my $command_constraint = IsOneOf(qw(create update delete));

  # check for trueness
  my $is_true = IsTrue;

  # simple equality
  my $is_foo = IsEq('foo');

DESCRIPTION

Top

This library contains all constraints to validate scalar values.

CONSTRAINTS

Top

Matches(@regex)

  my $c = Matches(qr/foo/, qr/bar/);

If one of the parameters matches the expression, this is true.

IsDefined()

True if the value is defined.

HasLength([$min, [$max]])

Is true if the value has a length above $min (which defaults to 1> and, if supplied, under the value of $max. A simple

  my $c = HasLength;

checks if the value has a length of at least 1.

IsOneOf(@values)

True if one of the @values equals the passed value. undef values work with this too, so

  my $c = IsOneOf(1, 2, undef);

will return true on an undefined value.

IsTrue()

True if the value evulates to true in boolean context.

IsEq($comparator)

Valid if the value is eq the $comparator.

SEE ALSO

Top

Declare::Constraints::Simple, Declare::Constraints::Simple::Library

AUTHOR

Top

Robert 'phaylon' Sedlacek <phaylon@dunkelheit.at>

LICENSE AND COPYRIGHT

Top


Declare-Constraints-Simple documentation Contained in the Declare-Constraints-Simple distribution.
package Declare::Constraints::Simple::Library::Scalar;
use warnings;
use strict;

use Declare::Constraints::Simple-Library;

use Carp::Clan qw(^Declare::Constraints::Simple);

constraint 'Matches',
    sub {
        my @rx = @_;
        croak 'Matches needs at least one Regexp as argument'
            unless @rx;
        for (@rx) {
            croak 'Matches only takes Regexps as arguments'
                unless ref($_) eq 'Regexp';
        }
        return sub {
            return _false('Undefined Value') unless defined $_[0];
            for (@rx) {
                return _true if $_[0] =~ /$_/;
            }
            return _false('Regex does not match');
        };
    };

constraint 'IsDefined',
    sub {
        return sub { 
            return _result((defined($_[0]) ? 1 : 0), 'Undefined Value');
        };
    };

constraint 'HasLength',
    sub {
        my ($min, $max) = @_;
        $min = 1 unless defined $min;
        $max = 0 unless defined $max;
        return sub {
            my ($val) = @_;
            return _false('Undefined Value') unless defined $val;
            return _false('Value too short') unless $min <= length($val);
            return _true unless $max;
            return _result(((length($val) <= $max) ? 1 : 0), 
                'Value too long');
        };
    };

constraint 'IsOneOf',
    sub {
        my @vals = @_;
        return sub {
            for (@vals) {
                unless (defined $_) {
                    return _true unless defined $_[0];
                    next;
                }
                next unless defined $_[0];
                return _true if $_[0] eq $_;
            }
            return _false('No Value matches');
        };
    };

constraint 'IsTrue', 
    sub {
        return sub { $_[0] ? _true : _false('Value evaluates to False') };
    };

constraint 'IsEq',
    sub {
        my ($compare) = @_;
        return sub { 
            return _result(
                ($compare eq $_[0]), 
                "'$_[0]' does not equal '$compare'"
            );
        };
    };

1;