| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | Declare::Constraints::Simple::Library::Referencial - Ref Constraints | ||||||||||
| 4 | |||||||||||
| 5 | =cut | ||||||||||
| 6 | |||||||||||
| 7 | package Declare::Constraints::Simple::Library::Referencial; | ||||||||||
| 8 | 13 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 13 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 13 | ▶ | use Declare::Constraints::Simple-Library; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||
| 14 | |||||||||||
| 15 | # scalar or array references | ||||||||||
| 16 | my $scalar_or_array = IsRefType( qw(SCALAR ARRAY) ); | ||||||||||
| 17 | |||||||||||
| 18 | # scalar reference | ||||||||||
| 19 | my $int_ref = IsScalarRef( IsInt ); | ||||||||||
| 20 | |||||||||||
| 21 | # accept mappings of ids to objects with "name" methods | ||||||||||
| 22 | my $id_obj_map = | ||||||||||
| 23 | IsHashRef( -keys => IsInt, | ||||||||||
| 24 | -values => And( IsObject, | ||||||||||
| 25 | HasMethods('name') )); | ||||||||||
| 26 | |||||||||||
| 27 | # an integer list | ||||||||||
| 28 | my $int_list = IsArrayRef( IsInt ); | ||||||||||
| 29 | |||||||||||
| 30 | # accept code references | ||||||||||
| 31 | my $is_closure = IsCodeRef; | ||||||||||
| 32 | |||||||||||
| 33 | # accept a regular expression | ||||||||||
| 34 | my $is_regex = IsRegex; | ||||||||||
| 35 | |||||||||||
| 36 | =head1 DESCRIPTION | ||||||||||
| 37 | |||||||||||
| 38 | This library contains those constraints that can test the validity of | ||||||||||
| 39 | references and their types. | ||||||||||
| 40 | |||||||||||
| 41 | =head1 CONSTRAINTS | ||||||||||
| 42 | |||||||||||
| 43 | =head2 IsRefType(@types) | ||||||||||
| 44 | |||||||||||
| 45 | Valid if the value is a reference of a kind in C<@types>. | ||||||||||
| 46 | |||||||||||
| 47 | =cut | ||||||||||
| 48 | |||||||||||
| 49 | constraint 'IsRefType', | ||||||||||
| 50 | 7 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 51 | 7 | my (@types) = @_; | |||||||||
| 52 | 7 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 53 | 7 | ▶ | return _false('Undefined Value') unless defined $_[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 6 | my @match = grep { ref($_[0]) eq $_ } @types; | |||||||||
| 55 | 6 | ▶ | return scalar(@match) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | ? _true | ||||||||||
| 57 | : _false('No matching RefType'); | ||||||||||
| 58 | 7 | }; | |||||||||
| 59 | 13 | }; | |||||||||
| 60 | |||||||||||
| 61 | =head2 IsScalarRef($constraint) | ||||||||||
| 62 | |||||||||||
| 63 | This is true if the value is a scalar reference. A possible constraint | ||||||||||
| 64 | for the scalar references target value can be passed. E.g. | ||||||||||
| 65 | |||||||||||
| 66 | my $test_integer_ref = IsScalarRef(IsInt); | ||||||||||
| 67 | |||||||||||
| 68 | =cut | ||||||||||
| 69 | |||||||||||
| 70 | constraint 'IsScalarRef', | ||||||||||
| 71 | 15 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 72 | 15 | my @vc = @_; | |||||||||
| 73 | 15 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 74 | 15 | ▶ | return _false('Undefined Value') unless defined $_[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 12 | ▶ | return _false('Not a ScalarRef') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | unless ref($_[0]) eq 'SCALAR'; | ||||||||||
| 77 | 8 | ▶ | return _true unless @vc; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 7 | my $result = _apply_checks(${$_[0]}, \@vc); | |||||||||
| 79 | 7 | ▶ | return $result unless $result->is_valid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | 2 | return _true; | |||||||||
| 81 | 15 | }; | |||||||||
| 82 | 13 | }; | |||||||||
| 83 | |||||||||||
| 84 | =head2 IsArrayRef($constraint) | ||||||||||
| 85 | |||||||||||
| 86 | The value is valid if the value is an array reference. The contents of | ||||||||||
| 87 | the array can be validated by passing an other C<$constraint> as | ||||||||||
| 88 | argument. | ||||||||||
| 89 | |||||||||||
| 90 | The stack or path part of C<IsArrayRef> is C<IsArrayRef[$index]> where | ||||||||||
| 91 | C<$index> is the index of the failing element. | ||||||||||
| 92 | |||||||||||
| 93 | =cut | ||||||||||
| 94 | |||||||||||
| 95 | constraint 'IsArrayRef', | ||||||||||
| 96 | 15 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 97 | 15 | my @vc = @_; | |||||||||
| 98 | 32 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||
| 99 | 32 | ▶ | return _false('Undefined Value') unless defined $_[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 30 | ▶ | return _false('Not an ArrayRef') | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | unless ref($_[0]) eq 'ARRAY'; | ||||||||||
| 102 | 24 | for (0 .. $#{$_[0]}) { | |||||||||
| 103 | 59 | my $result = _apply_checks($_[0][$_], \@vc, $_); | |||||||||
| 104 | 59 | ▶ | return $result unless $result->is_valid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 105 | } | ||||||||||
| 106 | 18 | return _true; | |||||||||
| 107 | 15 | }; | |||||||||
| 108 | 13 | }; | |||||||||
| 109 | |||||||||||
| 110 | =head2 IsHashRef(-keys => $constraint, -values => $constraint) | ||||||||||
| 111 | |||||||||||
| 112 | True if the value is a hash reference. It can also take two named | ||||||||||
| 113 | parameters: C<-keys> can pass a constraint to check the hashes keys, | ||||||||||
| 114 | C<-values> does the same for its values. | ||||||||||
| 115 | |||||||||||
| 116 | The stack or path part of C<IsHashRef> looks like | ||||||||||
| 117 | C<IsHashRef[$type $key]> where C<$type> is either C<val> or C<key> | ||||||||||
| 118 | depending on what was validated, and C<$key> being the key that didn't | ||||||||||
| 119 | pass validation. | ||||||||||
| 120 | |||||||||||
| 121 | =cut | ||||||||||
| 122 | |||||||||||
| 123 | constraint 'IsHashRef', | ||||||||||
| 124 | 15 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 125 | 15 | my %def = @_; | |||||||||
| 126 | 38 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.0
| |||||||||||
| 127 | 38 | ▶ | return _false('Undefined Value') unless defined $_[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 37 | ▶ | return _false('Not a HashRef') unless ref($_[0]) eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | 35 | ▶ | if (my $c = $def{'-values'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 13 | for (keys %{$_[0]}) { | |||||||||
| 131 | my $r = | ||||||||||
| 132 | 32 | _apply_checks($_[0]{$_}, _listify($c), "val $_"); | |||||||||
| 133 | 32 | ▶ | return $r unless $r->is_valid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | } | ||||||||||
| 135 | } | ||||||||||
| 136 | 32 | ▶ | if (my $c = $def{'-keys'}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 137 | 4 | for (keys %{$_[0]}) { | |||||||||
| 138 | 4 | my $r = _apply_checks($_, _listify($c), "key $_"); | |||||||||
| 139 | 4 | ▶ | return $r unless $r->is_valid; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 140 | } | ||||||||||
| 141 | } | ||||||||||
| 142 | 30 | return _true; | |||||||||
| 143 | 15 | }; | |||||||||
| 144 | 13 | }; | |||||||||
| 145 | |||||||||||
| 146 | =head2 IsCodeRef() | ||||||||||
| 147 | |||||||||||
| 148 | Code references have to be valid to pass this constraint. | ||||||||||
| 149 | |||||||||||
| 150 | =cut | ||||||||||
| 151 | |||||||||||
| 152 | constraint 'IsCodeRef', | ||||||||||
| 153 | 4 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 154 | 4 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 155 | 4 | ▶ | return _false('Undefined Value') unless defined $_[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 156 | 3 | return _result((ref($_[0]) eq 'CODE'), 'Not a CodeRef'); | |||||||||
| 157 | 4 | }; | |||||||||
| 158 | 13 | }; | |||||||||
| 159 | |||||||||||
| 160 | =head2 IsRegex() | ||||||||||
| 161 | |||||||||||
| 162 | True if the value is a regular expression built with C<qr>. B<Note> | ||||||||||
| 163 | however, that a simple string that could be used like C</$rx/> will | ||||||||||
| 164 | not pass this constraint. You can combine multiple constraints with | ||||||||||
| 165 | L<And(@constraints)> though. | ||||||||||
| 166 | |||||||||||
| 167 | =cut | ||||||||||
| 168 | |||||||||||
| 169 | constraint 'IsRegex', | ||||||||||
| 170 | 5 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 171 | 6 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 172 | 6 | ▶ | return _false('Undefined Value') unless defined $_[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | 5 | return _result((ref($_[0]) eq 'Regexp'), | |||||||||
| 174 | 'Not a Regular Expression'); | ||||||||||
| 175 | 5 | }; | |||||||||
| 176 | 13 | }; | |||||||||
| 177 | |||||||||||
| 178 | =head1 SEE ALSO | ||||||||||
| 179 | |||||||||||
| 180 | L<Declare::Constraints::Simple>, L<Declare::Constraints::Simple::Library> | ||||||||||
| 181 | |||||||||||
| 182 | =head1 AUTHOR | ||||||||||
| 183 | |||||||||||
| 184 | Robert 'phaylon' Sedlacek C<E<lt>phaylon@dunkelheit.atE<gt>> | ||||||||||
| 185 | |||||||||||
| 186 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 187 | |||||||||||
| 188 | This module is free software, you can redistribute it and/or modify it | ||||||||||
| 189 | under the same terms as perl itself. | ||||||||||
| 190 | |||||||||||
| 191 | =cut | ||||||||||
| 192 | |||||||||||
| 193 | 13 | 1; | |||||||||