Simple/Library/OO.pm

filter: Statementstmt 100% 42 / 42 Branchbran 100% 14 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 97.3% 72 / 74 CC 11 SCAR 11.0
CC
11
Cov
98%
__ANON__
6.0
__ANON__
4.0
__ANON__
2.0
CRAP 3.0
« Simple/Library/Scalar.pm ↑ Summary Simple/Library/Operators.pm »
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
BranchTrueFalseTotal
unless defined $_[0] 1 8 9
50 8 ▶ return _false('Not a Class or Object')
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[0]) or "Class::Inspector"->loaded($_[0]) 1 7 8
Condition66%2/3
builtin::blessed($_[0]) or "Class::Inspector"->loaded($_[0])
l!l&&r!l&&!r
701
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])
ABresult
1X1
011
000
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
BranchTrueFalseTotal
unless ($_[0]->can($_)) 3 6 9
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
BranchTrueFalseTotal
unless defined $_[0] 1 6 7
77 6 for (@classes) {
78 6 ▶ return _true if eval { $_[0]->isa($_) };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $_[0]->isa($_) } } 2 4 6
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
BranchTrueFalseTotal
unless defined $_[0] 1 2 3
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
BranchTrueFalseTotal
unless defined $_[0] 1 3 4
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;
« Simple/Library/Scalar.pm ↑ Summary Simple/Library/Operators.pm »