blib/lib/SQL/Translator/Types.pm

filter: Statementstmt 100% 36 / 36 Branchbran 81.2% 13 / 16 Conditioncond 61.5% 8 / 13 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 100% 11 / 11 Podpod 100% 2 / 2 totaltotal 84.7% 72 / 85 CC 14 SCAR 14.1
CC
14
Cov
88%
enum
7.1
__ANON__
5.0
__ANON__
4.1
CRAP 4.1
« blib/lib/SQL/Translator/Schema/Role/Extra.pm ↑ Summary blib/lib/SQL/Translator/Generator/DDL/SQLite.pm »
1 package SQL::Translator::Types;
2  
3 77 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 77 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 SQL::Translator::Types - Type checking functions
9  
10 =head1 SYNOPSIS
11  
12 package Foo;
13 use Moo;
14 use SQL::Translator::Types qw(schema_obj enum);
15  
16 has foo => ( is => 'rw', isa => schema_obj('Trigger') );
17 has bar => ( is => 'rw', isa => enum([qw(baz quux quuz)], {
18 msg => "Invalid value for bar: '%s'", icase => 1,
19 });
20  
21 =head1 DESCRIPTIONS
22  
23 This module exports functions that return coderefs suitable for L<Moo>
24 C<isa> type checks.
25 Errors are reported using L<SQL::Translator::Utils/throw>.
26  
27 =cut
28  
29 77 ▶use SQL::Translator::Utils qw(throw);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 77 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 77 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 77 our @EXPORT_OK = qw(schema_obj enum);
34  
35 =head1 FUNCTIONS
36  
37 =head2 schema_obj($type)
38  
39 Returns a coderef that checks that its arguments is an object of the
40 class C<< SQL::Translator::Schema::I<$type> >>.
41  
42 =cut
43  
44 674 ▶sub schema_obj {
Subroutine schema_obj: called CC=2 SCAR=7.2
Pod documented
45 674 my ($class) = @_;
46 674 my $name = lc $class;
47 674 ▶ $class = 'SQL::Translator::Schema' . ($class eq 'Schema' ? '' : "::$class");
Branch: 1 branch
BranchTrueFalseTotal
$class eq 'Schema' ? : 297 377 674
48 3019 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
49 3019 ▶ throw("Not a $name object")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[0]) and $_[0]->isa($class) 0 3019 3019
Condition33%1/3
builtin::blessed($_[0]) and $_[0]->isa($class)
!ll&&!rl&&r
003019
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa($class)
builtin::blessed($_[0]) $_[0]->isa($class)
Truth table33%1/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa($class)
ABresult
0X0
100
111
50 unless blessed($_[0])
51 and $_[0]->isa($class);
52 674 };
53 }
54  
55 =head2 enum(\@strings, [$msg | \%parameters])
56  
57 Returns a coderef that checks that the argument is one of the provided
58 C<@strings>.
59  
60 =head3 Parameters
61  
62 =over
63  
64 =item msg
65  
66 L<sprintf|perlfunc/sprintf> string for the error message.
67 If no other parameters are needed, this can be provided on its own,
68 instead of the C<%parameters> hashref.
69 The invalid value is passed as the only argument.
70 Defaults to C<Invalid value: '%s'>.
71  
72 =item icase
73  
74 If true, folds the values to lower case before checking for equality.
75  
76 =item allow_undef
77  
78 If true, allow C<undef> in addition to the specified strings.
79  
80 =item allow_false
81  
82 If true, allow any false value in addition to the specified strings.
83  
84 =back
85  
86 =cut
87  
88 374 ▶sub enum {
Subroutine enum: called CC=7 SCAR=19.6
Pod documented
89 374 my ($values, $args) = @_;
90 374 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
3740
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
91 374 ▶ $args = { msg => $args } unless ref($args) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args eq "HASH" 0 374 374
92 374 my $icase = !!$args->{icase};
93 374 ▶ my %values = map { ($icase ? lc : $_) => undef } @{$values};
Branch: 1 branch
BranchTrueFalseTotal
$icase ? : 146 1210 1356
94 374 ▶ my $msg = $args->{msg} || "Invalid value: '%s'";
Condition50%1/2
$args->{'msg'} || q[Invalid value: '%s']
l!l
3740
MC/DC0%0/1
$args->{'msg'} || q[Invalid value: '%s']
$args->{'msg'}
Truth table50%1/2
A
A= $args->{'msg'}
Aresult
11
00
95 my $extra_test
96 104 ▶ = $args->{allow_undef} ? sub { defined $_[0] }
Subroutine __ANON__: called CC=1 SCAR=0.0
97 1449 ▶ : $args->{allow_false} ? sub { !!$_[0] }
Subroutine __ANON__: called CC=1 SCAR=0.0
98 374 ▶ : undef;
Branch: 2 branches
BranchTrueFalseTotal
$args->{'allow_undef'} ? : 146 228 374
$args->{'allow_false'} ? : 228 0 228
99  
100 1553 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
101 1553 ▶ my $val = $icase ? lc $_[0] : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
$icase ? : 39 1514 1553
102 throw(sprintf($msg, $val))
103 if (!defined($extra_test) || $extra_test->($val))
104 1553 ▶ && !exists $values{$val};
Branch: 1 branch
BranchTrueFalseTotal
if !defined($extra_test) || &$extra_test($val) and not exists $values{$val} 1 1552 1553
Condition83%5/6
!defined($extra_test) || &$extra_test($val) and not exists $values{$val}
!ll&&!rl&&r
6489041
not defined $extra_test or &$extra_test($val)
l!l&&r!l&&!r
0905648
MC/DC66%2/3
!defined($extra_test) || &$extra_test($val) and not exists $values{$val}
not defined $extra_test &$extra_test($val) not exists $values{$val}
Truth table60%3/5
not(A) or B and not(C)
A= not defined $extra_testB= &$extra_test($val)C= not exists $values{$val}
ABCresult
00X0
1X00
0100
1X11
0111
105 374 };
106 }
107  
108 77 1;
« blib/lib/SQL/Translator/Schema/Role/Extra.pm ↑ Summary blib/lib/SQL/Translator/Generator/DDL/SQLite.pm »