blib/lib/Venus/Schema.pm

filter: Statementstmt 97.2% 35 / 36 Branchbran 50.0% 2 / 4 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 10 / 10 Podpod 83.3% 5 / 6 totaltotal 83.3% 55 / 66 SCAR 16.2
CC
5
Cov
87%
build_args
3.8
error
3.1
BEGIN
1.0
CRAP 5.1
« blib/lib/Venus/Regexp.pm ↑ Summary blib/lib/Venus/Core/Class.pm »
1 package Venus::Schema;
2  
3 1 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Venus::Class 'attr', 'base';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 base 'Venus::Kind::Utility';
11  
12 # ATTRIBUTES
13  
14 1 attr 'definition';
15  
16 # BUILDERS
17  
18 sub build_args {
19 21 ▶ my ($self, $data) = @_;
Subroutine build_args: called CC=3 SCAR=13.3
Pod undocumented
20  
21 21 ▶ if (keys %$data == 1 && exists $data->{definition}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$data == 1 and exists $data->{"definition"}) 0 21 21
Condition33%1/3
keys %$data == 1 and exists $data->{"definition"}
!ll&&!rl&&r
2100
MC/DC0%0/2
keys %$data == 1 and exists $data->{"definition"}
keys %$data == 1 exists $data->{"definition"}
Truth table33%1/3
A and B
A= keys %$data == 1B= exists $data->{"definition"}
ABresult
0X0
100
111
22 0 return $data;
23 }
24 return {
25 21 definition => $data,
26 };
27 }
28  
29 # METHODS
30  
31 sub assert {
32 18 ▶ my ($self) = @_;
Subroutine assert: called CC=1 SCAR=0.0
Pod documented
33  
34 18 require Venus::Assert;
35  
36 18 my $assert = Venus::Assert->new;
37  
38 18 return $assert->expression($assert->render('hashkeys', $self->definition));
39 }
40  
41 sub check {
42 4 ▶ my ($self, $data) = @_;
Subroutine check: called CC=1 SCAR=0.0
Pod documented
43  
44 4 my $assert = $self->assert;
45  
46 4 return $assert->valid($data);
47 }
48  
49 sub deduce {
50 4 ▶ my ($self, $data) = @_;
Subroutine deduce: called CC=1 SCAR=0.0
Pod documented
51  
52 4 require Venus::Type;
53  
54 4 my $assert = $self->assert;
55  
56 4 return Venus::Type->new($assert->validate($data))->deduce_deep;
57 }
58  
59 sub error {
60 4 ▶ my ($self, $data) = @_;
Subroutine error: called CC=3 SCAR=11.3
Pod documented
61  
62 4 my $error = $self->catch('validate', $data);
63  
64 4 ▶ die $error if $error && !$error->isa('Venus::Check::Error');
Branch: 1 branch
BranchTrueFalseTotal
if $error and not $error->isa("Venus::Check::Error") 0 4 4
Condition66%2/3
$error and not $error->isa("Venus::Check::Error")
!ll&&!rl&&r
130
MC/DC0%0/2
$error and not $error->isa("Venus::Check::Error")
$error not $error->isa("Venus::Check::Error")
Truth table33%1/3
A and not(B)
A= $errorB= not $error->isa("Venus::Check::Error")
ABresult
0X0
100
111
65  
66 4 return $error;
67 }
68  
69 sub validate {
70 8 ▶ my ($self, $data) = @_;
Subroutine validate: called CC=1 SCAR=0.0
Pod documented
71  
72 8 my $assert = $self->assert;
73  
74 8 return $assert->validate($data);
75 }
76  
77 1 1;
78  
79  
80  
81 =head1 NAME
82  
83 Venus::Schema - Schema Class
84  
85 =cut
86  
87 =head1 ABSTRACT
88  
89 Schema Class for Perl 5
90  
91 =cut
92  
93 =head1 SYNOPSIS
94  
95 package main;
96  
97 use Venus::Schema;
98  
99 my $schema = Venus::Schema->new;
100  
101 # bless({...}, 'Venus::Schema')
102  
103 =cut
104  
105 =head1 DESCRIPTION
106  
107 This package provides methods for validating whether objects and complex data
108 structures conform to a schema.
109  
110 =cut
111  
112 =head1 ATTRIBUTES
113  
114 This package has the following attributes:
115  
116 =cut
117  
118 =head2 definition
119  
120 definition(hashref $data) (hashref)
121  
122 The definition attribute is read-write, accepts C<(HashRef)> values, and is
123 optional.
124  
125 I<Since C<2.55>>
126  
127 =over 4
128  
129 =item definition example 1
130  
131 # given: synopsis
132  
133 package main;
134  
135 my $definition = $schema->definition({});
136  
137 # {}
138  
139 =back
140  
141 =over 4
142  
143 =item definition example 2
144  
145 # given: synopsis
146  
147 # given: example-1 definition
148  
149 package main;
150  
151 $definition = $schema->definition;
152  
153 # {}
154  
155 =back
156  
157 =cut
158  
159 =head1 INHERITS
160  
161 This package inherits behaviors from:
162  
163 L<Venus::Kind::Utility>
164  
165 =cut
166  
167 =head1 METHODS
168  
169 This package provides the following methods:
170  
171 =cut
172  
173 =head2 assert
174  
175 assert() (Venus::Assert)
176  
177 The assert method builds and returns a L<Venus::Assert> object based on the
178 L</definition>.
179  
180 I<Since C<2.55>>
181  
182 =over 4
183  
184 =item assert example 1
185  
186 # given: synopsis
187  
188 package main;
189  
190 my $assert = $schema->assert;
191  
192 # bless({...}, 'Venus::Assert')
193  
194 =back
195  
196 =over 4
197  
198 =item assert example 2
199  
200 # given: synopsis
201  
202 package main;
203  
204 $schema->definition({
205 name => 'string',
206 });
207  
208 my $assert = $schema->assert;
209  
210 # bless({...}, 'Venus::Assert')
211  
212 =back
213  
214 =cut
215  
216 =head2 check
217  
218 check(hashref $data) (boolean)
219  
220 The check method builds an assert object using L</assert> and returns the
221 result of the L<Venus::Assert/check> method.
222  
223 I<Since C<2.55>>
224  
225 =over 4
226  
227 =item check example 1
228  
229 # given: synopsis
230  
231 package main;
232  
233 my $check = $schema->check;
234  
235 # false
236  
237 =back
238  
239 =over 4
240  
241 =item check example 2
242  
243 # given: synopsis
244  
245 package main;
246  
247 $schema->definition({
248 name => 'string',
249 role => {
250 title => 'string',
251 level => 'number',
252 },
253 });
254  
255 my $check = $schema->check({});
256  
257 # false
258  
259 =back
260  
261 =over 4
262  
263 =item check example 3
264  
265 # given: synopsis
266  
267 package main;
268  
269 $schema->definition({
270 name => 'string',
271 role => {
272 title => 'string',
273 level => 'number',
274 },
275 });
276  
277 my $check = $schema->check({
278 name => 'someone',
279 role => {},
280 });
281  
282 # false
283  
284 =back
285  
286 =over 4
287  
288 =item check example 4
289  
290 # given: synopsis
291  
292 package main;
293  
294 $schema->definition({
295 name => 'string',
296 role => {
297 title => 'string',
298 level => 'number',
299 },
300 });
301  
302 my $check = $schema->check({
303 name => 'someone',
304 role => {
305 title => 'engineer',
306 level => 1,
307 },
308 });
309  
310 # true
311  
312 =back
313  
314 =cut
315  
316 =head2 deduce
317  
318 deduce(hashref $data) (Venus::Hash)
319  
320 The deduce method builds an assert object using L</assert> and validates the
321 value provided using L<Venus::Assert/validate>, passing the result to
322 L<Venus::Type/deduce_deep> unless the validation throws an exception.
323  
324 I<Since C<2.55>>
325  
326 =over 4
327  
328 =item deduce example 1
329  
330 # given: synopsis
331  
332 package main;
333  
334 my $deduce = $schema->deduce;
335  
336 # Exception! (isa Venus::Check::Error)
337  
338 =back
339  
340 =over 4
341  
342 =item deduce example 2
343  
344 # given: synopsis
345  
346 package main;
347  
348 $schema->definition({
349 name => 'string',
350 role => {
351 title => 'string',
352 level => 'number',
353 },
354 });
355  
356 my $deduce = $schema->deduce({});
357  
358 # Exception! (isa Venus::Check::Error)
359  
360 =back
361  
362 =over 4
363  
364 =item deduce example 3
365  
366 # given: synopsis
367  
368 package main;
369  
370 $schema->definition({
371 name => 'string',
372 role => {
373 title => 'string',
374 level => 'number',
375 },
376 });
377  
378 my $deduce = $schema->deduce({
379 name => 'someone',
380 role => {},
381 });
382  
383 # Exception! (isa Venus::Check::Error)
384  
385 =back
386  
387 =over 4
388  
389 =item deduce example 4
390  
391 # given: synopsis
392  
393 package main;
394  
395 $schema->definition({
396 name => 'string',
397 role => {
398 title => 'string',
399 level => 'number',
400 },
401 });
402  
403 my $deduce = $schema->deduce({
404 name => 'someone',
405 role => {
406 title => 'engineer',
407 level => 1,
408 },
409 });
410  
411 # bless({...}, 'Venus::Hash')
412  
413 =back
414  
415 =cut
416  
417 =head2 error
418  
419 error(hashref $data) (Venus::Error)
420  
421 The error method builds an assert object using L</assert> and validates the
422 value provided using L<Venus::Assert/validate>, catching any error thrown and
423 returning it, otherwise returning undefined.
424  
425 I<Since C<2.55>>
426  
427 =over 4
428  
429 =item error example 1
430  
431 # given: synopsis
432  
433 package main;
434  
435 my $error = $schema->error;
436  
437 # Exception! (isa Venus::Check::Error)
438  
439 =back
440  
441 =over 4
442  
443 =item error example 2
444  
445 # given: synopsis
446  
447 package main;
448  
449 $schema->definition({
450 name => 'string',
451 role => {
452 title => 'string',
453 level => 'number',
454 },
455 });
456  
457 my $error = $schema->error({});
458  
459 # Exception! (isa Venus::Check::Error)
460  
461 =back
462  
463 =over 4
464  
465 =item error example 3
466  
467 # given: synopsis
468  
469 package main;
470  
471 $schema->definition({
472 name => 'string',
473 role => {
474 title => 'string',
475 level => 'number',
476 },
477 });
478  
479 my $error = $schema->error({
480 name => 'someone',
481 role => {},
482 });
483  
484 # Exception! (isa Venus::Check::Error)
485  
486 =back
487  
488 =over 4
489  
490 =item error example 4
491  
492 # given: synopsis
493  
494 package main;
495  
496 $schema->definition({
497 name => 'string',
498 role => {
499 title => 'string',
500 level => 'number',
501 },
502 });
503  
504 my $error = $schema->error({
505 name => 'someone',
506 role => {
507 title => 'engineer',
508 level => 1,
509 },
510 });
511  
512 # undef
513  
514 =back
515  
516 =cut
517  
518 =head2 validate
519  
520 validate(hashref $data) (hashref)
521  
522 The validate method builds an assert object using L</assert> and validates the
523 value provided using L<Venus::Assert/validate>, returning the result unless the
524 validation throws an exception.
525  
526 I<Since C<2.55>>
527  
528 =over 4
529  
530 =item validate example 1
531  
532 # given: synopsis
533  
534 package main;
535  
536 my $validate = $schema->validate;
537  
538 # Exception! (isa Venus::Check::Error)
539  
540 =back
541  
542 =over 4
543  
544 =item validate example 2
545  
546 # given: synopsis
547  
548 package main;
549  
550 $schema->definition({
551 name => 'string',
552 role => {
553 title => 'string',
554 level => 'number',
555 },
556 });
557  
558 my $validate = $schema->validate({});
559  
560 # Exception! (isa Venus::Check::Error)
561  
562 =back
563  
564 =over 4
565  
566 =item validate example 3
567  
568 # given: synopsis
569  
570 package main;
571  
572 $schema->definition({
573 name => 'string',
574 role => {
575 title => 'string',
576 level => 'number',
577 },
578 });
579  
580 my $validate = $schema->validate({
581 name => 'someone',
582 role => {},
583 });
584  
585 # Exception! (isa Venus::Check::Error)
586  
587 =back
588  
589 =over 4
590  
591 =item validate example 4
592  
593 # given: synopsis
594  
595 package main;
596  
597 $schema->definition({
598 name => 'string',
599 role => {
600 title => 'string',
601 level => 'number',
602 },
603 });
604  
605 my $validate = $schema->validate({
606 name => 'someone',
607 role => {
608 title => 'engineer',
609 level => 1,
610 },
611 });
612  
613 # {name => 'someone', role => {title => 'engineer', level => 1,},}
614  
615 =back
616  
617 =cut
618  
619 =head1 AUTHORS
620  
621 Awncorp, C<awncorp@cpan.org>
622  
623 =cut
624  
625 =head1 LICENSE
626  
627 Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>.
628  
629 This program is free software, you can redistribute it and/or modify it under
630 the terms of the Apache license version 2.0.
631  
632 =cut
« blib/lib/Venus/Regexp.pm ↑ Summary blib/lib/Venus/Core/Class.pm »