Declare::Constraints::Simple::Library::General - General Constraints


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

Index


Code Index:

NAME

Top

Declare::Constraints::Simple::Library::General - General Constraints

SYNOPSIS

Top

  # custom error messages
  my $constraint = 
    And( Message( 'You need to specify a Value', IsDefined ),
         Message( 'The specified Value is not an Int', IsInt ));

  # build results
  my $valid   = ReturnTrue;
  my $invalid = ReturnFalse('Just because');

DESCRIPTION

Top

This library is meant to contain those constraints and constraint-like elements that apply generally to the whole framework.

CONSTRAINTS

Top

Message($message, $constraint)

Overrides the message set on the result object for failures in $constraint. For example:

  my $message = 'How hard is it to give me a number?';
  my $constraint = Message($message, IsNumber);

  my $result = $constraint->('duh...');
  print $result->message, "\n";

The Message constraint overrides the error message returned by it's whole subtree, however, the Message specification nearest to the point of failure will win. So while this

  my $constraint = Message( 'Foo',
                            IsArrayRef( Message( 'Bar', IsInt )));

  my $result = $constraint->(['I am not an Integer']);
  print $result->message;

will print Bar, this

  my $result = $constraint->('I\'m not even an ArrayRef');
  print $result->message;

will output Foo.

Scope($name, $constraint)

Executes the passed $constraint in a newly generated scope named $name.

SetResult($scope, $name, $constraint)

Stores the result ov an evaluation of $constraint in $scope under $name.

IsValid($scope, $name)

Returns a true result if the result $name, which has to have been stored previously in the scope named $scope, was valid.

ReturnTrue()

Returns a true result.

ReturnFalse($msg)

Returns a false result containing $msg as error message.

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::General;
use warnings;
use strict;

use Declare::Constraints::Simple-Library;

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

constraint 'Message',
    sub {
        my ($msg, $c) = @_;
        return sub {
            return _with_message($msg, $c, @_);
        };
    };

constraint 'Scope',
    sub {
        my ($scope_name, $constraint) = @_;
        return sub {
            return _with_scope($scope_name, $constraint, @_);
        };
    };

constraint 'SetResult',
    sub {
        my ($scope, $name, $constraint) = @_;
        return sub {
            my $result = $constraint->(@_);
            _set_result($scope, $name, $result);
            return $result;
        };
    };

constraint 'IsValid',
    sub {
        my ($scope, $name) = @_;
        return sub {
            _info("$scope:$name");
            return _false unless _has_result($scope, $name);
            my $result = _get_result($scope, $name);
            return _result($result, 
                "Value '$name' in scope '$scope' is invalid");
        };
    };

constraint 'ReturnTrue',
    sub { return sub { _true } };

constraint 'ReturnFalse',
    sub { my $msg = shift; return sub { _false($msg) } };

1;