Simple/Library/General.pm

filter: Statementstmt 100% 41 / 41 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 16 / 16 Podpod n/a totaltotal 100% 59 / 59 CC 2 SCAR 1.1
CC
2
Cov
100%
__ANON__
2.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Simple/Result.pm ↑ Summary Simple.pm »
1 =head1 NAME
2  
3 Declare::Constraints::Simple::Library::General - General Constraints
4  
5 =cut
6  
7 package Declare::Constraints::Simple::Library::General;
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 Carp::Clan qw(^Declare::Constraints::Simple);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 SYNOPSIS
16  
17 # custom error messages
18 my $constraint =
19 And( Message( 'You need to specify a Value', IsDefined ),
20 Message( 'The specified Value is not an Int', IsInt ));
21  
22 # build results
23 my $valid = ReturnTrue;
24 my $invalid = ReturnFalse('Just because');
25  
26 =head1 DESCRIPTION
27  
28 This library is meant to contain those constraints and constraint-like
29 elements that apply generally to the whole framework.
30  
31 =head1 CONSTRAINTS
32  
33 =head2 Message($message, $constraint)
34  
35 Overrides the C<message> set on the result object for failures in
36 C<$constraint>. For example:
37  
38 my $message = 'How hard is it to give me a number?';
39 my $constraint = Message($message, IsNumber);
40  
41 my $result = $constraint->('duh...');
42 print $result->message, "\n";
43  
44 The C<Message> constraint overrides the error message returned by it's
45 whole subtree, however, the C<Message> specification nearest to the point
46 of failure will win. So while this
47  
48 my $constraint = Message( 'Foo',
49 IsArrayRef( Message( 'Bar', IsInt )));
50  
51 my $result = $constraint->(['I am not an Integer']);
52 print $result->message;
53  
54 will print C<Bar>, this
55  
56 my $result = $constraint->('I\'m not even an ArrayRef');
57 print $result->message;
58  
59 will output C<Foo>.
60  
61 =cut
62  
63 constraint 'Message',
64 3 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
65 3 my ($msg, $c) = @_;
66 14 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
67 14 return _with_message($msg, $c, @_);
68 3 };
69 13 };
70  
71 =head2 Scope($name, $constraint)
72  
73 Executes the passed C<$constraint> in a newly generated scope named
74 C<$name>.
75  
76 =cut
77  
78 constraint 'Scope',
79 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
80 2 my ($scope_name, $constraint) = @_;
81 8 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
82 8 return _with_scope($scope_name, $constraint, @_);
83 2 };
84 13 };
85  
86 =head2 SetResult($scope, $name, $constraint)
87  
88 Stores the result ov an evaluation of C<$constraint> in C<$scope> under
89 C<$name>.
90  
91 =cut
92  
93 constraint 'SetResult',
94 3 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
95 3 my ($scope, $name, $constraint) = @_;
96 11 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
97 11 my $result = $constraint->(@_);
98 11 _set_result($scope, $name, $result);
99 11 return $result;
100 3 };
101 13 };
102  
103 =head2 IsValid($scope, $name)
104  
105 Returns a true result if the result C<$name>, which has to have been stored
106 previously in the scope named C<$scope>, was valid.
107  
108 =cut
109  
110 constraint 'IsValid',
111 3 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
112 3 my ($scope, $name) = @_;
113 9 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
114 9 _info("$scope:$name");
115 9 ▶ return _false unless _has_result($scope, $name);
Branch: 1 branch
BranchTrueFalseTotal
unless _has_result($scope, $name) 1 8 9
116 8 my $result = _get_result($scope, $name);
117 8 return _result($result,
118 "Value '$name' in scope '$scope' is invalid");
119 3 };
120 13 };
121  
122 =head2 ReturnTrue()
123  
124 Returns a true result.
125  
126 =cut
127  
128 constraint 'ReturnTrue',
129 5 ▶ sub { return sub { _true } };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
130  
131 =head2 ReturnFalse($msg)
132  
133 Returns a false result containing C<$msg> as error message.
134  
135 =cut
136  
137 constraint 'ReturnFalse',
138 5 ▶ sub { my $msg = shift; return sub { _false($msg) } };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
139  
140 =head1 SEE ALSO
141  
142 L<Declare::Constraints::Simple>, L<Declare::Constraints::Simple::Library>
143  
144 =head1 AUTHOR
145  
146 Robert 'phaylon' Sedlacek C<E<lt>phaylon@dunkelheit.atE<gt>>
147  
148 =head1 LICENSE AND COPYRIGHT
149  
150 This module is free software, you can redistribute it and/or modify it
151 under the same terms as perl itself.
152  
153 =cut
154  
155 13 1;
« Simple/Result.pm ↑ Summary Simple.pm »