Declare::Constraints::Simple::Library::Array - Array Constraints


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

Index


Code Index:

NAME

Top

Declare::Constraints::Simple::Library::Array - Array Constraints

SYNOPSIS

Top

  # accept a list of pairs
  my $pairs_validation = IsArrayRef( HasArraySize(2,2) );

  # integer => object pairs
  my $pairs = And( OnEvenElements(IsInt), 
                   OnOddElements(IsObject) );

  # a three element array
  my $tri = And( HasArraySize(3,3),
                 OnArrayElements(0, IsInt,
                                 1, IsDefined,
                                 2, IsClass) );

DESCRIPTION

Top

This module contains all constraints that can be applied to array references.

CONSTRAINTS

Top

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

With $min defaulting to 1. So a specification of

  my $profile = HasArraySize;

checks for at least one value. To force an exact size of the array, specify the same values for both:

  my $profile = HasArraySize(3, 3);

OnArrayElements($key => $constraint, $key => $constraint, ...)

Applies the the $constraints to the corresponding $keys if they are present. For required keys see HasArraySize.

OnEvenElements($constraint)

Runs the constraint on all even elements of an array. See also OnOddElements.

OnOddElements($constraint)

Runs the constraint on all odd elements of an array. See also OnEvenElements.

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

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

constraint 'HasArraySize',
    sub {
        my ($min, $max) = @_;
        $min = 1 unless defined $min;
        return sub {
            return _false('Undefined Value') unless defined $_[0];
            return _false('Not an ArrayRef') 
                unless ref($_[0]) eq 'ARRAY';
            return _false("Less than $min Array elements")
                unless scalar(@{$_[0]}) >= $min;
            return _true 
                unless $max;
            return _false("More than $max Array elements")
                unless scalar(@{$_[0]}) <= $max;
            return _true;
        };
    };

constraint 'OnArrayElements',
    sub {
        my %keymap = @_;
        my @keys   = sort keys %keymap;
        for (@keys) {
            croak "Not an array index: $_" if $_ =~ /\D/;
        }
        
        return sub {
            return _false('Undefined Value') unless defined $_[0];
            return _false('Not an ArrayRef') 
                unless ref($_[0]) eq 'ARRAY';
            for my $k (@keys) {
                last if $k > $#{$_[0]};
                my $r = $keymap{$k}->($_[0][$k]);
                _info($k);
                return $r unless $r->is_valid;
            }
            return _true;
        }
    };

constraint 'OnEvenElements',
    sub {
        my ($c) = @_;

        return sub {
            return _false('Undefined Value') unless defined $_[0];
            return _false('Not an ArrayRef')
                unless ref($_[0]) eq 'ARRAY';
            my $p = 0;
            while ($p <= $#{$_[0]}) {
                my $r = $c->($_[0][$p]);
                _info($p);
                return $r unless $r->is_valid;
                $p += 2;
            }
            return _true;
        };
    };


constraint 'OnOddElements',
    sub {
        my ($c) = @_;

        return sub {
            return _false('Undefined Value') unless defined $_[0];
            return _false('Not an ArrayRef')
                unless ref($_[0]) eq 'ARRAY';
            my $p = 1;
            while ($p <= $#{$_[0]}) {
                my $r = $c->($_[0][$p]);
                _info($p);
                return $r unless $r->is_valid;
                $p += 2;
            }
            return _true;
        };
    };

1;