blib/lib/Types/LoadableClass.pm

filter: Statementstmt 55.3% 36 / 65 Branchbran 18.7% 3 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 42.3% 11 / 26 Podpod n/a totaltotal 46.7% 50 / 107 CC 12 SCAR 15.8
CC
12
Cov
48%
__ANON__
12.0
__ANON__
12.0
__ANON__
12.0
CRAP 4.8
↑ Summary
1 5 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Types::LoadableClass;
6  
7 5 our $AUTHORITY = 'cpan:TOBYINK';
8 5 our $VERSION = '0.003';
9  
10 use Type::Library
11 5 ▶ -base,
Subroutine BEGIN: called CC=1 SCAR=0.0
12 -declare => qw(
13 ModuleName LoadableClass LoadableRole
14 ClassIsa ClassDoes ClassCan
15 5 );
16  
17 5 ▶use Type::Utils -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Types::Standard qw( StrMatch RoleName );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 ▶use Module::Runtime qw($module_name_rx is_module_name);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Class::Load qw(load_optional_class is_class_loaded);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 declare ModuleName,
24 as StrMatch[ qr/\A$module_name_rx\z/ ],
25 0 ▶ message {
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 "'$_' is not a valid module name";
27 5 };
28  
29 declare LoadableClass,
30 as ModuleName,
31 0 ▶ where {
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 0 load_optional_class($_)
33 }
34 39 ▶ inline_as {
Subroutine __ANON__: called CC=1 SCAR=0.0
35 39 (undef, "Class::Load::load_optional_class($_)");
36 }
37 4 ▶ message {
Subroutine __ANON__: called CC=2 SCAR=7.6
38 4 ▶ ModuleName->validate($_) or "'$_' could not be loaded";
Branch: 1 branch
BranchTrueFalseTotal
&ModuleName()->validate($_) or "'${_}' could not be loaded" 0 4 4
39 5 };
40  
41 declare LoadableRole,
42 as intersection([ LoadableClass, RoleName ]),
43 2 ▶ message {
Subroutine __ANON__: called CC=2 SCAR=6.9
44 2 ▶ LoadableClass->validate($_) or "'$_' is not a loadable role";
Branch: 1 branch
BranchTrueFalseTotal
&LoadableClass()->validate($_) or "'${_}' is not a loadable role" 1 1 2
45 5 };
46  
47 declare ClassIsa,
48 as LoadableClass,
49 0 ▶ constraint_generator => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
50 0 ▶ my @bases = @_ or return ClassIsa;
Branch: 1 branch
BranchTrueFalseTotal
my(@bases) = or return &ClassIsa() 0 0 0
51 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
52 0 ▶ $_[0]->isa($_) && return !!1 for @bases;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->isa($_) and 0 0 0
53 0 return !!0;
54 0 };
55 },
56 0 ▶ inline_generator => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 0 my @bases = @_;
58 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 my $var = $_[1];
60 return (
61 undef,
62 0 sprintf(
63 '(%s)',
64 join(
65 ' or ',
66 map(sprintf('%s->isa(%s)', $var, B::perlstring($_)), @bases),
67 ),
68 ),
69 );
70 0 };
71 5 };
72  
73 declare ClassDoes,
74 as LoadableClass,
75 0 ▶ constraint_generator => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
76 0 ▶ my @roles = @_ or return ClassDoes;
Branch: 1 branch
BranchTrueFalseTotal
my(@roles) = or return &ClassDoes() 0 0 0
77 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
78 0 ▶ $_[0]->DOES($_) || return !!0 for @roles;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->DOES($_) or 0 0 0
79 0 return !!1;
80 0 };
81 },
82 0 ▶ inline_generator => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 0 my @roles = @_;
84 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
85 0 my $var = $_[1];
86 return (
87 undef,
88 0 sprintf(
89 'do { my $method = %s->can("DOES")||%s->can("isa"); %s } ',
90 $var,
91 $var,
92 join(
93 ' and ',
94 map(sprintf('%s->$method(%s)', $var, B::perlstring($_)), @roles),
95 ),
96 ),
97 );
98 0 };
99 5 };
100  
101 declare ClassCan,
102 as LoadableClass,
103 0 ▶ constraint_generator => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
104 0 ▶ my @methods = @_ or return ClassCan;
Branch: 1 branch
BranchTrueFalseTotal
my(@methods) = or return &ClassCan() 0 0 0
105 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
106 0 ▶ $_[0]->can($_) || return !!0 for @methods;
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->can($_) or 0 0 0
107 0 return !!1;
108 0 };
109 },
110 0 ▶ inline_generator => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 0 my @methods = @_;
112 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
113 0 my $var = $_[1];
114 return (
115 undef,
116 0 map(sprintf('%s->can(%s)', $var, B::perlstring($_)), @methods),
117 );
118 0 };
119 5 };
120  
121 __PACKAGE__->meta->add_coercion({
122 name => 'ExpandPrefix',
123 type_constraint => ModuleName,
124 0 ▶ coercion_generator => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 0 my ($self, $target, $prefix) = @_;
126 0 Types::TypeTiny::StringLike->assert_valid($prefix);
127 return (
128 0 StrMatch[qr{\A-.+}],
129 qq{ do { (my \$tmp = \$_) =~ s{\\A-}{$prefix\::}; \$tmp } },
130 );
131 }
132 5 });
133  
134 5 1;
135  
136 __END__
↑ Summary