line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
=head1 NAME |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
Declare::Constraints::Simple::Library::OO - OO Constraints |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=cut |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
package Declare::Constraints::Simple::Library::OO; |
8
|
12
|
|
|
12
|
|
77
|
use warnings; |
|
12
|
|
|
|
|
26
|
|
|
12
|
|
|
|
|
350
|
|
9
|
12
|
|
|
12
|
|
60
|
use strict; |
|
12
|
|
|
|
|
24
|
|
|
12
|
|
|
|
|
581
|
|
10
|
|
|
|
|
|
|
|
11
|
12
|
|
|
12
|
|
95
|
use Declare::Constraints::Simple-Library; |
|
12
|
|
|
|
|
26
|
|
|
12
|
|
|
|
|
172
|
|
12
|
|
|
|
|
|
|
|
13
|
12
|
|
|
12
|
|
74
|
use Class::Inspector; |
|
12
|
|
|
|
|
24
|
|
|
12
|
|
|
|
|
313
|
|
14
|
12
|
|
|
12
|
|
94
|
use Scalar::Util (); |
|
12
|
|
|
|
|
36
|
|
|
12
|
|
|
|
|
6065
|
|
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 |
38
|
|
|
|
|
|
|
all the specified C<@methods>. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
The stack or path part of C looks like C |
41
|
|
|
|
|
|
|
where C<$method> is the first found missing method. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
constraint 'HasMethods', |
46
|
|
|
|
|
|
|
sub { |
47
|
|
|
|
|
|
|
my (@methods) = @_; |
48
|
|
|
|
|
|
|
return sub { |
49
|
|
|
|
|
|
|
return _false('Undefined Value') unless defined $_[0]; |
50
|
|
|
|
|
|
|
return _false('Not a Class or Object') |
51
|
|
|
|
|
|
|
unless Scalar::Util::blessed($_[0]) |
52
|
|
|
|
|
|
|
or Class::Inspector->loaded($_[0]); |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
for (@methods) { |
55
|
|
|
|
|
|
|
unless ($_[0]->can($_)) { |
56
|
|
|
|
|
|
|
_info($_); |
57
|
|
|
|
|
|
|
return _false("Method $_ not implemented"); |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
return _true; |
62
|
|
|
|
|
|
|
}; |
63
|
|
|
|
|
|
|
}; |
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
|
|
|
|
|
|
|
sub { |
74
|
|
|
|
|
|
|
my (@classes) = @_; |
75
|
|
|
|
|
|
|
return sub { |
76
|
|
|
|
|
|
|
return _false('Undefined Value') unless defined $_[0]; |
77
|
|
|
|
|
|
|
for (@classes) { |
78
|
|
|
|
|
|
|
return _true if eval { $_[0]->isa($_) }; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
return _false('No matching Class'); |
81
|
|
|
|
|
|
|
}; |
82
|
|
|
|
|
|
|
}; |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head2 IsClass() |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Valid if value is a loaded class. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=cut |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
constraint 'IsClass', |
91
|
|
|
|
|
|
|
sub { |
92
|
|
|
|
|
|
|
return sub { |
93
|
|
|
|
|
|
|
return _false('Undefined Value') unless defined $_[0]; |
94
|
|
|
|
|
|
|
return _result(Class::Inspector->loaded($_[0]), |
95
|
|
|
|
|
|
|
'Not a loaded Class'); |
96
|
|
|
|
|
|
|
}; |
97
|
|
|
|
|
|
|
}; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 IsObject() |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
True if the value is blessed. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=cut |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
constraint 'IsObject', |
106
|
|
|
|
|
|
|
sub { |
107
|
|
|
|
|
|
|
return sub { |
108
|
|
|
|
|
|
|
return _false('Undefined Value') unless defined $_[0]; |
109
|
|
|
|
|
|
|
return _result(Scalar::Util::blessed($_[0]), |
110
|
|
|
|
|
|
|
'Not an Object'); |
111
|
|
|
|
|
|
|
}; |
112
|
|
|
|
|
|
|
}; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 SEE ALSO |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
L, L |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 AUTHOR |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Robert 'phaylon' Sedlacek Cphaylon@dunkelheit.atE> |
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
|
|
|
|
|
|
|
1; |