| 1 | package Valiant::Proxy::Object; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Valiant::Util 'throw_exception'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Scalar::Util 'blessed'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | with 'Valiant::Proxy'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 14 | ▶ | sub read_attribute_for_validation { | ||||||||||||||||||||||||||
|
Subroutine
read_attribute_for_validation: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | 14 | my ($self, $attribute) = @_; | |||||||||||||||||||||||||||
| 11 | 14 | ▶ | if($self->for->can($attribute)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 12 | 14 | return $self->for->$attribute; | |||||||||||||||||||||||||||
| 13 | } else { | ||||||||||||||||||||||||||||
| 14 | 0 | throw_exception MissingMethod => (object=>$self->for, method=>$attribute); | |||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 9 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 19 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 20 | 9 | ( my $method = our $AUTOLOAD ) =~ s{.*::}{}; | |||||||||||||||||||||||||||
| 21 | 9 | ▶ | return if $method eq 'DESTROY' or $method eq 'can'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$method eq "DESTROY" or $method eq "can"
MC/DC50%1/2
$method eq "DESTROY" or $method eq "can"
$method eq "DESTROY"
$method eq "can"
Truth table66%2/3
A or B
A= $method eq "DESTROY"B= $method eq "can"
| |||||||||||||||||||||||||||||
| 22 | 5 | ▶ | if(blessed($self->for) && $self->for->can($method)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($self->for) and $self->for->can($method)
MC/DC50%1/2
builtin::blessed($self->for) and $self->for->can($method)
builtin::blessed($self->for)
$self->for->can($method)
Truth table66%2/3
A and B
A= builtin::blessed($self->for)B= $self->for->can($method)
| |||||||||||||||||||||||||||||
| 23 | 4 | return $self->for->$method(@_); | |||||||||||||||||||||||||||
| 24 | } else { | ||||||||||||||||||||||||||||
| 25 | 1 | throw_exception MissingMethod => (object=>$self->for, method=>$method); | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | 1; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 NAME | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Valiant::Proxy::Object - Wrap any object into a validatable result object. | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | my $proxy = Valiant::Proxy::Object->new( | ||||||||||||||||||||||||||||
| 38 | validations => [ | ||||||||||||||||||||||||||||
| 39 | [ name => length => [2,15] ], | ||||||||||||||||||||||||||||
| 40 | [ age => numericality => 'positive_integer' ], | ||||||||||||||||||||||||||||
| 41 | ], | ||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | my $result = $proxy->validate($user_object); | ||||||||||||||||||||||||||||
| 45 | $result->invalid; # true when $user_object breaks the rules | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Create a validation object for a given class or role. Useful when you need (or prefer) | ||||||||||||||||||||||||||||
| 50 | to build up a validation ruleset in code rather than via the annotations-like approach | ||||||||||||||||||||||||||||
| 51 | given in L<Valiant::Validations>. Can also be useful to add validations to a class that | ||||||||||||||||||||||||||||
| 52 | isn't Moo/se and can't use L<Valiant::Validations> or is outside your control (such as | ||||||||||||||||||||||||||||
| 53 | a third party library). Lastly you may need to build validation sets based on existing | ||||||||||||||||||||||||||||
| 54 | metadata, such as via database introspection or from a file containing validation | ||||||||||||||||||||||||||||
| 55 | instructions. | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | This uses AUTOLOAD to delegate method calls to the underlying object. Calling | ||||||||||||||||||||||||||||
| 58 | a method that the underlying object does not provide throws a | ||||||||||||||||||||||||||||
| 59 | L<Valiant::Util::Exception::MissingMethod> rather than silently returning undef. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Please note that the code used to create the validation object is not speed optimized so | ||||||||||||||||||||||||||||
| 62 | I recommend you not use this approach in 'hot' code paths. Its probably best if you can | ||||||||||||||||||||||||||||
| 63 | create all these during your application startup once (for long lived applications). Maybe | ||||||||||||||||||||||||||||
| 64 | not ideal for 'fire and forget' scripts like cron jobs or CGI. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | You probably won't use this directly, although you can. | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | This does the interface defined by L<Valiant::Proxy> so see the docs on that. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Also: L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>. | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | See L<Valiant> | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | See L<Valiant> | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||