| 1 | =head1 NAME | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | Declare::Constraints::Simple::Library::OO - OO Constraints | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =cut | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | package Declare::Constraints::Simple::Library::OO; | ||||||||||||||||||||||||||||
| 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 | 13 | ▶ | use Class::Inspector; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 13 | ▶ | use Scalar::Util (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # accept objects or classes | ||||||||||||||||||||||||||||
| 19 | my $object_or_class = Or( IsObject, IsClass ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # valid on objects with all methods | ||||||||||||||||||||||||||||
| 22 | my $proper_object = And( IsObject, | ||||||||||||||||||||||||||||
| 23 | HasMethods( qw(foo bar) )); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # validate against date objects | ||||||||||||||||||||||||||||
| 26 | my $is_date_object = IsA('DateTime'); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | This library contains the constraints for validating parameters in an | ||||||||||||||||||||||||||||
| 31 | object oriented manner. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 CONSTRAINTS | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head2 HasMethods(@methods) | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | Returns true if the value is an object or class that C<can> | ||||||||||||||||||||||||||||
| 38 | all the specified C<@methods>. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | The stack or path part of C<HasMethods> looks like C<HasMethods[$method]> | ||||||||||||||||||||||||||||
| 41 | where C<$method> is the first found missing method. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | constraint 'HasMethods', | ||||||||||||||||||||||||||||
| 46 | 9 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 9 | my (@methods) = @_; | |||||||||||||||||||||||||||
| 48 | 9 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 49 | 9 | ▶ | return _false('Undefined Value') unless defined $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 8 | ▶ | return _false('Not a Class or Object') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($_[0]) or "Class::Inspector"->loaded($_[0])
MC/DC50%1/2
builtin::blessed($_[0]) or "Class::Inspector"->loaded($_[0])
builtin::blessed($_[0])
"Class::Inspector"->loaded($_[0])
Truth table66%2/3
A or B
A= builtin::blessed($_[0])B= "Class::Inspector"->loaded($_[0])
| |||||||||||||||||||||||||||||
| 51 | unless Scalar::Util::blessed($_[0]) | ||||||||||||||||||||||||||||
| 52 | or Class::Inspector->loaded($_[0]); | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 7 | for (@methods) { | |||||||||||||||||||||||||||
| 55 | 9 | ▶ | unless ($_[0]->can($_)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 3 | _info($_); | |||||||||||||||||||||||||||
| 57 | 3 | return _false("Method $_ not implemented"); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 4 | return _true; | |||||||||||||||||||||||||||
| 62 | 9 | }; | |||||||||||||||||||||||||||
| 63 | 13 | }; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =head2 IsA(@classes) | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Is true if the passed object or class is a subclass of one | ||||||||||||||||||||||||||||
| 68 | of the classes mentioned in C<@classes>. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | =cut | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | constraint 'IsA', | ||||||||||||||||||||||||||||
| 73 | 7 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 74 | 7 | my (@classes) = @_; | |||||||||||||||||||||||||||
| 75 | 7 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 76 | 7 | ▶ | return _false('Undefined Value') unless defined $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 6 | for (@classes) { | |||||||||||||||||||||||||||
| 78 | 6 | ▶ | return _true if eval { $_[0]->isa($_) }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 4 | return _false('No matching Class'); | |||||||||||||||||||||||||||
| 81 | 7 | }; | |||||||||||||||||||||||||||
| 82 | 13 | }; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | =head2 IsClass() | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Valid if value is a loaded class. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | constraint 'IsClass', | ||||||||||||||||||||||||||||
| 91 | 3 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | 3 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 93 | 3 | ▶ | return _false('Undefined Value') unless defined $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 2 | return _result(Class::Inspector->loaded($_[0]), | |||||||||||||||||||||||||||
| 95 | 'Not a loaded Class'); | ||||||||||||||||||||||||||||
| 96 | 3 | }; | |||||||||||||||||||||||||||
| 97 | 13 | }; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =head2 IsObject() | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | True if the value is blessed. | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | constraint 'IsObject', | ||||||||||||||||||||||||||||
| 106 | 4 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 107 | 4 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 108 | 4 | ▶ | return _false('Undefined Value') unless defined $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 3 | return _result(Scalar::Util::blessed($_[0]), | |||||||||||||||||||||||||||
| 110 | 'Not an Object'); | ||||||||||||||||||||||||||||
| 111 | 4 | }; | |||||||||||||||||||||||||||
| 112 | 13 | }; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | L<Declare::Constraints::Simple>, L<Declare::Constraints::Simple::Library> | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | Robert 'phaylon' Sedlacek C<E<lt>phaylon@dunkelheit.atE<gt>> | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | This module is free software, you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 125 | under the same terms as perl itself. | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =cut | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 13 | 1; | |||||||||||||||||||||||||||