blib/lib/kura.pm

filter: Statementstmt 98.9% 91 / 92 Branchbran 91.1% 31 / 34 Conditioncond 57.1% 4 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 95.0% 19 / 20 Podpod 0.0% 0 / 2 totaltotal 91.8% 146 / 159 CC 21 SCAR 13.7
CC
21
Cov
95%
create_constraint
8.0
_new_kura_item
7.0
_create_constraint_from_hashref
4.6
CRAP 3.9
↑ Summary
1 package kura;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 our $VERSION = "0.10";
6  
7 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Sub::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 my %FORBIDDEN_NAME = map { $_ => 1 } qw{
12 BEGIN CHECK DESTROY END INIT UNITCHECK
13 AUTOLOAD STDIN STDOUT STDERR ARGV ARGVOUT ENV INC SIG
14 };
15  
16 6 my @ALLOWED_CONSTRAINT_CLASSES = qw(
17 Data::Validator
18 Poz::Types
19 );
20  
21 19 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
22 19 my $pkg = shift;
23 19 my $caller = caller;
24  
25 19 $pkg->import_into($caller, @_);
26 }
27  
28 # Import into the caller package.
29 21 ▶sub import_into {
Subroutine import_into: called CC=2 SCAR=6.9
Pod undocumented
30 21 my $pkg = shift;
31 21 my ($caller, $name, $constraint) = @_;
32  
33 21 my ($kura_item, $err) = _new_kura_item($caller, $name, $constraint);
34 21 ▶ Carp::croak $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 7 14 21
35  
36 14 _save_kura_item($kura_item, $caller);
37 14 _save_inc($caller);
38 }
39  
40 # Create a constraint object.
41 #
42 # @param $constraint Defined. Following `create_constraint` function allows these types: Object, CodeRef, HashRef.
43 # @param $opts Dict[name => Str, caller => Str]
44 # @return ($constraint, undef) | (undef, $error_message)
45 #
46 # NOTE: This function is a hook point. If you want to customize the constraint object, you can override this function.
47 15 ▶sub create_constraint {
Subroutine create_constraint: called CC=8 SCAR=20.8
Pod undocumented
48 15 my ($constraint, $opts) = @_;
49  
50 15 ▶ if (my $blessed = Scalar::Util::blessed($constraint)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $blessed = builtin::blessed($constraint)) { } 9 6 15
elsif (my $reftype = builtin::reftype($constraint)) { } 5 1 6
51 9 ▶ return _create_constraint_from_typetiny($constraint, $opts) if $constraint->isa('Type::Tiny');
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->isa("Type::Tiny") 2 7 9
52 7 ▶ return ($constraint, undef) if $constraint->can('check');
Branch: 1 branch
BranchTrueFalseTotal
if $constraint->can("check") 6 1 7
53 1 ▶ return ($constraint, undef) if grep { $constraint->isa($_) } @ALLOWED_CONSTRAINT_CLASSES;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$constraint->isa($_);} @ALLOWED_CONSTRAINT_CLASSES 0 1 1
54 1 return (undef, "Invalid constraint. Object must have a `check` method or allowed constraint class: $blessed");
55 }
56 elsif (my $reftype = Scalar::Util::reftype($constraint)) {
57 5 ▶ if ($reftype eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($reftype eq 'CODE') { } 4 1 5
elsif ($reftype eq 'HASH') { } 1 0 1
58 4 return _create_constraint_from_coderef($constraint, $opts);
59 }
60 elsif ($reftype eq 'HASH') {
61 1 return _create_constraint_from_hashref($constraint, $opts);
62 }
63 }
64  
65 1 return (undef, 'Invalid constraint');
66 }
67  
68 # Create a constraint object from a Type::Tiny object.
69 2 ▶sub _create_constraint_from_typetiny {
Subroutine _create_constraint_from_typetiny: called CC=2 SCAR=6.9
70 2 my ($type, $opts) = @_;
71  
72 2 ▶ $type->{name} = $opts->{name} if $type->is_anon;
Branch: 1 branch
BranchTrueFalseTotal
if $type->is_anon 1 1 2
73  
74 2 return ($type, undef);
75 }
76  
77 # Create a constraint object from a code reference.
78 4 ▶sub _create_constraint_from_coderef {
Subroutine _create_constraint_from_coderef: called CC=1 SCAR=0.0
79 4 my ($coderef, $opts) = @_;
80  
81 4 require Type::Tiny;
82  
83 4 my $args = {};
84 4 $args->{name} = $opts->{name};
85 4 ▶ $args->{constraint} = sub { !!eval { $coderef->($_[0]) } };
Subroutine __ANON__: called CC=1 SCAR=0.0
86 0 ▶ $args->{message} = sub { sprintf('%s did not pass the constraint "%s"', Type::Tiny::_dd($_[0]), $args->{name}) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
87  
88 4 return (Type::Tiny->new(%$args), undef);
89 }
90  
91 # Create a constraint object from a hash reference.
92 1 ▶sub _create_constraint_from_hashref {
Subroutine _create_constraint_from_hashref: called CC=4 SCAR=15.2
93 1 my ($args, $opts) = @_;
94  
95 1 ▶ my $blessed = delete $args->{blessed} || 'Type::Tiny';
Condition50%1/2
delete $args->{'blessed'} || 'Type::Tiny'
l!l
01
MC/DC0%0/1
delete $args->{'blessed'} || 'Type::Tiny'
delete $args->{'blessed'}
Truth table50%1/2
A
A= delete $args->{'blessed'}
Aresult
11
00
96 1 ▶ eval "require $blessed" or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $blessed" or die $@ 1 0 1
97  
98 1 ▶ $args->{name} //= $opts->{name};
Condition33%1/3
$args->{"name"} //= $opts->{"name"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$args->{"name"} //= $opts->{"name"}
$args->{"name"} $opts->{"name"}
Truth table33%1/3
A //= B
A= $args->{"name"}B= $opts->{"name"}
ABresult
1X1
011
000
99  
100 1 return ($blessed->new(%$args), undef);
101 }
102  
103 # Create a new kura item which is Dict[name => Str, code => CodeRef].
104 # If the name or constraint is invalid, it returns (undef, $error_message).
105 # Otherwise, it returns ($kura_item, undef).
106 21 ▶sub _new_kura_item {
Subroutine _new_kura_item: called CC=7 SCAR=19.5
107 21 my ($caller, $name, $constraint) = @_;
108  
109 {
110 21 ▶ return (undef, 'name is required') if !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 1 20 21
111 20 ▶ return (undef, "'$name' is forbidden.") if $FORBIDDEN_NAME{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $FORBIDDEN_NAME{$name} 1 19 20
112 19 ▶ return (undef, "'$name' is already defined") if $caller->can($name);
Branch: 1 branch
BranchTrueFalseTotal
if $caller->can($name) 1 18 19
113 }
114  
115 18 ▶ return (undef, 'constraint is required') if !defined $constraint;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $constraint 1 17 18
116 17 ($constraint, my $err) = create_constraint($constraint, { name => $name, caller => $caller });
117 17 ▶ return (undef, $err) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 3 14 17
118  
119 # Prefix '_' means private, so it is not exported.
120 14 ▶ my $is_private = $name =~ /^_/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^_/ ? : 1 13 14
121  
122 14 ▶ my $kura_item = { name => $name, code => sub { $constraint }, is_private => $is_private };
Subroutine _PrivateFoo: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
123 14 return ($kura_item, undef);
124 }
125  
126 # Save the kura item to the caller package
127 14 ▶sub _save_kura_item {
Subroutine _save_kura_item: called CC=2 SCAR=6.9
128 14 my ($kura_item, $caller) = @_;
129  
130 14 my $name = $kura_item->{name};
131 14 my $code = Sub::Util::set_subname("$caller\::$name", $kura_item->{code});
132  
133 6 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
134 6 ▶ no warnings "once";
Subroutine BEGIN: called CC=1 SCAR=0.0
135 14 *{"$caller\::$name"} = $code;
136  
137 14 ▶ if (!$kura_item->{is_private}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($kura_item->{"is_private"}) 13 1 14
138 13 push @{"$caller\::EXPORT_OK"}, $name;
139 13 push @{"$caller\::KURA"}, $name;
140 }
141  
142 14 return;
143 }
144  
145 # Hack to make the caller package already loaded. Useful for multi-packages in a single file.
146 14 ▶sub _save_inc {
Subroutine _save_inc: called CC=2 SCAR=6.9
147 14 my ($caller) = @_;
148  
149 14 ( my $file = $caller ) =~ s{::}{/}g;
150 14 ▶ $INC{"$file.pm"} ||= __FILE__;
Condition100%2/2
$INC{"$file.pm"} ||= __FILE__
l!l
122
MC/DC100%1/1
$INC{"$file.pm"} ||= __FILE__
$INC{"$file.pm"}
Truth table100%2/2
A
A= $INC{"$file.pm"}
Aresult
11
00
151  
152 14 return;
153 }
154  
155 6 1;
156 __END__
↑ Summary