blib/lib/Venus/Role/Assertable.pm

filter: Statementstmt 89.6% 26 / 29 Branchbran 100% 2 / 2 Conditioncond 66.6% 4 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 81.8% 9 / 11 Podpod 83.3% 5 / 6 totaltotal 79.3% 46 / 58 SCAR 14.0
CC
4
Cov
86%
make
3.0
assertion
2.0
__ANON__
2.0
CRAP 4.0
« blib/lib/Venus/Role/Stashable.pm ↑ Summary blib/lib/Venus/Role/Pluggable.pm »
1 package Venus::Role::Assertable;
2  
3 96 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 96 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 96 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 96 ▶use Venus::Role 'fault';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # METHODS
11  
12 sub assert {
13 51 ▶ my ($self, $data) = @_;
Subroutine assert: called CC=1 SCAR=0.0
Pod documented
14  
15 51 return $self->assertion->result($data);
16 }
17  
18 sub assertion {
19 47 ▶ my ($self) = @_;
Subroutine assertion: called CC=2 SCAR=7.1
Pod documented
20  
21 47 require Venus::Assert;
22  
23 47 ▶ my $class = ref $self || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
1460
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
24  
25 47 my $assert = Venus::Assert->new($class);
26  
27 $assert->match('hashref')->format(sub{
28 0 ▶ $class->new($_)
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 47 });
30  
31 47 $assert->accept($class);
32  
33 47 return $assert;
34 }
35  
36 sub check {
37 3 ▶ my ($self, $data) = @_;
Subroutine check: called CC=1 SCAR=0.0
Pod documented
38  
39 3 return $self->assertion->valid($data);
40 }
41  
42 sub coerce {
43 0 ▶ my ($self, $data) = @_;
Subroutine coerce: not called CC=1 SCAR=6.9
Pod documented
44  
45 0 return $self->assertion->coerce($data);
46 }
47  
48 sub make {
49 52 ▶ my ($self, $data) = @_;
Subroutine make: called CC=3 SCAR=11.1
Pod documented
50  
51 52 ▶ return UNIVERSAL::isa($data, ref $self || $self)
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($data, ref $self || $self) ? : 4 48 52
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
3490
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
52 ? $data
53 : $self->assert($data);
54 }
55  
56 # EXPORTS
57  
58 sub EXPORT {
59 109 ▶ ['assert', 'assertion', 'check', 'coerce', 'make']
Subroutine EXPORT: called CC=1 SCAR=0.0
Pod undocumented
60 }
61  
62 96 1;
63  
64  
65  
66 =head1 NAME
67  
68 Venus::Role::Assertable - Assertable Role
69  
70 =cut
71  
72 =head1 ABSTRACT
73  
74 Assertable Role for Perl 5
75  
76 =cut
77  
78 =head1 SYNOPSIS
79  
80 package Example;
81  
82 use Venus::Class;
83 use Venus::Assert;
84  
85 with 'Venus::Role::Assertable';
86  
87 sub assertion {
88 Venus::Assert->new('Example')->accept('Example')
89 }
90  
91 package main;
92  
93 my $example = Example->new;
94  
95 # $example->check;
96  
97 =cut
98  
99 =head1 DESCRIPTION
100  
101 This package modifies the consuming package and requires methods for making the
102 object assertable.
103  
104 =cut
105  
106 =head1 METHODS
107  
108 This package provides the following methods:
109  
110 =cut
111  
112 =head2 assert
113  
114 assert(any $data) (any)
115  
116 The assert method returns the data provided if it passes the registered type
117 constraints, or throws an exception.
118  
119 I<Since C<1.23>>
120  
121 =over 4
122  
123 =item assert example 1
124  
125 # given: synopsis
126  
127 package main;
128  
129 my $assert = $example->assert;
130  
131 # Exception! (isa Venus::Check::Error)
132  
133 =back
134  
135 =over 4
136  
137 =item assert example 2
138  
139 # given: synopsis
140  
141 package main;
142  
143 my $assert = $example->assert({});
144  
145 # Exception! (isa Venus::Check::Error)
146  
147 =back
148  
149 =over 4
150  
151 =item assert example 3
152  
153 # given: synopsis
154  
155 package main;
156  
157 my $assert = $example->assert($example);
158  
159 # bless({}, "Example")
160  
161 =back
162  
163 =cut
164  
165 =head2 assertion
166  
167 assertion() (Venus::Assert)
168  
169 The assertion method receives no arguments and should returns a
170 L<Venus::Assert> object.
171  
172 I<Since C<1.23>>
173  
174 =over 4
175  
176 =item assertion example 1
177  
178 package main;
179  
180 my $example = Example->new;
181  
182 my $assertion = $example->assertion;
183  
184 # bless({name => "Example"}, "Venus::Assert")
185  
186 =back
187  
188 =cut
189  
190 =head2 check
191  
192 check(any $data) (boolean)
193  
194 The check method returns true if the data provided passes the registered type
195 constraints, or returns false.
196  
197 I<Since C<1.23>>
198  
199 =over 4
200  
201 =item check example 1
202  
203 # given: synopsis
204  
205 package main;
206  
207 my $check = $example->check;
208  
209 # 0
210  
211 =back
212  
213 =over 4
214  
215 =item check example 2
216  
217 # given: synopsis
218  
219 package main;
220  
221 my $check = $example->check({});
222  
223 # 0
224  
225 =back
226  
227 =over 4
228  
229 =item check example 3
230  
231 # given: synopsis
232  
233 package main;
234  
235 my $check = $example->check($example);
236  
237 # 1
238  
239 =back
240  
241 =cut
242  
243 =head2 coerce
244  
245 coerce(any $data) (any)
246  
247 The coerce method returns a coerced value if the data provided matches any of
248 the registered type coercions, or returns the data provided.
249  
250 I<Since C<1.23>>
251  
252 =over 4
253  
254 =item coerce example 1
255  
256 # given: synopsis
257  
258 package main;
259  
260 my $assertion = $example->assertion;
261  
262 $assertion->match('string')->format(sub{ucfirst(lc($_))});
263  
264 my $coerce = $assertion->coerce;
265  
266 # undef
267  
268 =back
269  
270 =over 4
271  
272 =item coerce example 2
273  
274 # given: synopsis
275  
276 package main;
277  
278 my $assertion = $example->assertion;
279  
280 $assertion->match('string')->format(sub{ucfirst(lc($_))});
281  
282 my $coerce = $assertion->coerce({});
283  
284 # {}
285  
286 =back
287  
288 =over 4
289  
290 =item coerce example 3
291  
292 # given: synopsis
293  
294 package main;
295  
296 my $assertion = $example->assertion;
297  
298 $assertion->match('string')->format(sub{ucfirst(lc($_))});
299  
300 my $coerce = $assertion->coerce('hello');
301  
302 # "Hello"
303  
304 =back
305  
306 =cut
307  
308 =head2 make
309  
310 make(any $data) (object)
311  
312 The make method returns an instance of the invocant, if the data provided
313 passes the registered type constraints, allowing for any coercion, or throws an
314 exception. If the data provided is itself an instance of the invocant it will
315 be returned unaltered.
316  
317 I<Since C<1.23>>
318  
319 =over 4
320  
321 =item make example 1
322  
323 # given: synopsis
324  
325 package main;
326  
327 my $make = $example->make;
328  
329 # Exception! (isa Venus::Check::Error)
330  
331 =back
332  
333 =over 4
334  
335 =item make example 2
336  
337 # given: synopsis
338  
339 package main;
340  
341 my $make = $example->make($example);
342  
343 # bless({}, "Example")
344  
345 =back
346  
347 =over 4
348  
349 =item make example 3
350  
351 # given: synopsis
352  
353 package main;
354  
355 my $make = $example->make({});
356  
357 # Exception! (isa Venus::Check::Error)
358  
359 =back
360  
361 =cut
362  
363 =head1 AUTHORS
364  
365 Awncorp, C<awncorp@cpan.org>
366  
367 =cut
368  
369 =head1 LICENSE
370  
371 Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>.
372  
373 This program is free software, you can redistribute it and/or modify it under
374 the terms of the Apache license version 2.0.
375  
376 =cut
« blib/lib/Venus/Role/Stashable.pm ↑ Summary blib/lib/Venus/Role/Pluggable.pm »