blib/lib/Hades/Realm/Compiled/Params.pm

filter: Statementstmt 96.9% 96 / 99 Branchbran 79.4% 54 / 68 Conditioncond 72.0% 18 / 25 MC/DCmcdc 53.3% 8 / 15 Subroutinesub 100% 10 / 10 Podpod 100% 7 / 7 totaltotal 86.1% 193 / 224 SCAR 39.4
CC
47
Cov
88%
build_sub
17.0
build_type
10.0
push_cpo
7.3
CRAP 51.3
↑ Summary
1 package Hades::Realm::Compiled::Params;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use base qw/Hades/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 our $VERSION = 0.03;
6  
7 sub new {
8 15 ▶ my ( $cls, %args ) = ( shift(), scalar @_ == 1 ? %{ $_[0] } : @_ );
Subroutine new: called CC=6 SCAR=18.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 ? : 13 2 15
9 15 my $self = $cls->SUPER::new(%args);
10 15 my %accessors = ( cpo => { required => 1, default => [], }, );
11 15 for my $accessor ( keys %accessors ) {
12 my $value
13 = $self->$accessor(
14 defined $args{$accessor}
15 ? $args{$accessor}
16 15 ▶ : $accessors{$accessor}->{default} );
Branch: 1 branch
BranchTrueFalseTotal
defined $args{$accessor} ? : 11 4 15
17 13 ▶ unless ( !$accessors{$accessor}->{required} || defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $accessors{$accessor}{"required"} or defined $value) 0 13 13
Condition33%1/3
not $accessors{$accessor}{"required"} or defined $value
l!l&&r!l&&!r
0130
MC/DC0%0/2
not $accessors{$accessor}{"required"} or defined $value
not $accessors{$accessor}{"required"} defined $value
Truth table33%1/3
not(A) or B
A= not $accessors{$accessor}{"required"}B= defined $value
ABresult
1X1
011
000
18 0 die "$accessor accessor is required";
19 }
20 }
21 13 return $self;
22 }
23  
24 sub cpo {
25 22 ▶ my ( $self, $value ) = @_;
Subroutine cpo: called CC=4 SCAR=13.9
Pod documented
26 22 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 18 4 22
27 18 ▶ if ( ( ref($value) || "" ) ne "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $value || "") ne "ARRAY") 4 14 18
Condition100%2/2
ref $value || ''
l!l
162
MC/DC100%1/1
ref $value || ''
ref $value
Truth table100%2/2
A
A= ref $value
Aresult
11
00
28 4 die qq{ArrayRef: invalid value $value for accessor cpo};
29 }
30 14 $self->{cpo} = $value;
31 }
32 18 return $self->{cpo};
33 }
34  
35 sub push_cpo {
36 17 ▶ my ( $self, $key, $value ) = @_;
Subroutine push_cpo: called CC=7 SCAR=19.8
Pod documented
37 17 ▶ if ( !defined($key) || ref $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $key or ref $key) 2 15 17
Condition66%2/3
not defined $key or ref $key
l!l&&r!l&&!r
0215
MC/DC50%1/2
not defined $key or ref $key
not defined $key ref $key
Truth table66%2/3
not(A) or B
A= not defined $keyB= ref $key
ABresult
1X1
011
000
38 2 ▶ $key = defined $key ? $key : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 2 0 2
39 2 die qq{Str: invalid value $key for variable \$key in method push_cpo};
40 }
41 15 ▶ if ( !defined($value) || ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value or ref $value) 2 13 15
Condition66%2/3
not defined $value or ref $value
l!l&&r!l&&!r
0213
MC/DC50%1/2
not defined $value or ref $value
not defined $value ref $value
Truth table66%2/3
not(A) or B
A= not defined $valueB= ref $value
ABresult
1X1
011
000
42 2 ▶ $value = defined $value ? $value : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 2 0 2
43 2 die
44 qq{Str: invalid value $value for variable \$value in method push_cpo};
45 }
46 13 push @{ $self->{cpo} }, qq|$key => $value|;
47 }
48  
49 sub build_accessor {
50 11 ▶ my ( $orig, $self, @params ) = ( 'SUPER::build_accessor', @_ );
Subroutine build_accessor: called CC=5 SCAR=16.1
Pod documented
51 $params[2]->{ $params[1] }->{type}->[0] =~ s/\s*\[/[/g
52 11 ▶ if $params[2]->{ $params[1] }->{type}->[0];
Branch: 1 branch
BranchTrueFalseTotal
if $params[2]{$params[1]}{"type"}[0] 10 1 11
53 $self->push_cpo( $params[1],
54 '[' . ( $params[2]->{ $params[1] }->{type}->[0] || 'Any' )
55 . ($params[2]->{ $params[1] }->{default}
56 11 ▶ ? ', { default => ' . $params[2]->{ $params[1] }->{default} . '} ]'
Branch: 1 branch
BranchTrueFalseTotal
$params[2]{$params[1]}{'default'} ? : 4 7 11
Condition100%2/2
$params[2]{$params[1]}{'type'}[0] || 'Any'
l!l
101
MC/DC100%1/1
$params[2]{$params[1]}{'type'}[0] || 'Any'
$params[2]{$params[1]}{'type'}[0]
Truth table100%2/2
A
A= $params[2]{$params[1]}{'type'}[0]
Aresult
11
00
57 : ']'
58 )
59 );
60 11 my @res = $self->$orig(@params);
61 11 ▶ return wantarray ? @res : $res[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 11 11
62 }
63  
64 sub build_sub {
65 8 ▶ my ( $self, $mg, $name, $meta ) = @_;
Subroutine build_sub: called CC=16 SCAR=28.3
Pod documented
66 8 ▶ if ( !ref $mg ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $mg) 2 6 8
67 2 ▶ $mg = defined $mg ? $mg : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $mg ? : 2 0 2
68 2 die qq{Ref: invalid value $mg for variable \$mg in method build_};
69 }
70 6 ▶ if ( !defined($name) || ref $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $name or ref $name) 2 4 6
Condition66%2/3
not defined $name or ref $name
l!l&&r!l&&!r
024
MC/DC50%1/2
not defined $name or ref $name
not defined $name ref $name
Truth table66%2/3
not(A) or B
A= not defined $nameB= ref $name
ABresult
1X1
011
000
71 2 ▶ $name = defined $name ? $name : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 2 0 2
72 2 die
73 qq{Str: invalid value $name for variable \$name in method build_sub};
74 }
75 4 ▶ if ( ( ref($meta) || "" ) ne "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $meta || "") ne "HASH") 2 2 4
Condition100%2/2
ref $meta || ''
l!l
31
MC/DC100%1/1
ref $meta || ''
ref $meta
Truth table100%2/2
A
A= ref $meta
Aresult
11
00
76 2 ▶ $meta = defined $meta ? $meta : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $meta ? : 2 0 2
77 2 die
78 qq{HashRef: invalid value $meta for variable \$meta in method build_sub};
79 }
80 2 my $code = $meta->{$name}->{code};
81 2 my ( $params, $types, $params_explanation, $private );
82 2 ▶ $private = $self->build_private($name) if $meta->{$name}->{private};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{$name}{"private"} 0 2 2
83 2 ▶ if ( $meta->{$name}->{param} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{$name}{"param"}) 2 0 2
84 2 for my $param ( @{ $meta->{$name}->{param} } ) {
85 2 ▶ $params_explanation .= ', ' if $params_explanation;
Branch: 1 branch
BranchTrueFalseTotal
if $params_explanation 0 2 2
86 2 $params .= ', ' . $param;
87 2 my $pm = $meta->{$name}->{params_map}->{$param};
88 2 ▶ $pm->{type} ||= q|Any|;
Condition50%1/2
$pm->{"type"} ||= "Any"
l!l
20
MC/DC0%0/1
$pm->{"type"} ||= "Any"
$pm->{"type"}
Truth table50%1/2
A
A= $pm->{"type"}
Aresult
11
00
89 2 ▶ my $t = $pm->{type} . ($pm->{default} ? ', { default => ' . $pm->{default} . ' }' : '');
Branch: 1 branch
BranchTrueFalseTotal
$pm->{'default'} ? : 1 1 2
90 2 ▶ $types .= $types ? ', ' . $t : $t;
Branch: 1 branch
BranchTrueFalseTotal
$types ? : 0 2 2
91 2 $params_explanation .= qq|param $param to be a $pm-> { type } |;
92 }
93 2 my $type = $self->build_type( $name, $types, $types );
94 2 $self->push_cpo( $name, '[' . $types . ']' );
95 2 $code = qq| { my (\$self $params) = (shift$type); $code; } |;
96 2 $params =~ s/^\,\s*//;
97 2 my $example = qq|\$obj->$name($params)|;
98 $mg->sub($name)->code($code)
99 ->pod(qq|call $name method. Expects $params_explanation.|)
100 ->example($example)
101 2 ->test( $self->build_tests( $name, $meta->{$name} ) );
102 }
103 }
104  
105 sub build_type {
106 19 ▶ my ( $self, $name, $type, $value ) = @_;
Subroutine build_type: called CC=10 SCAR=23.0
Pod documented
107 19 ▶ if ( !defined($name) || ref $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $name or ref $name) 2 17 19
Condition66%2/3
not defined $name or ref $name
l!l&&r!l&&!r
0217
MC/DC50%1/2
not defined $name or ref $name
not defined $name ref $name
Truth table66%2/3
not(A) or B
A= not defined $nameB= ref $name
ABresult
1X1
011
000
108 2 ▶ $name = defined $name ? $name : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 2 0 2
109 2 die
110 qq{Str: invalid value $name for variable \$name in method build_type};
111 }
112 17 ▶ if ( defined $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 14 3 17
113 14 ▶ if ( ref $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type) 2 12 14
114 2 die
115 qq{Optional[Str]: invalid value $type for variable \$type in method build_type};
116 }
117 }
118 15 ▶ if ( defined $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 4 11 15
119 4 ▶ if ( ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) 2 2 4
120 2 die
121 qq{Optional[Str]: invalid value $value for variable \$value in method build_type};
122 }
123 }
124 13 my $code = '';
125 13 ▶ if ($type) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type) 12 1 13
126 12 ▶ $code
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 2 10 12
127 .= $value
128 ? qq|, \$VALIDATE->$name->(\@_)|
129 : qq|(\$value) = \$VALIDATE->$name->(\$value);|;
130 }
131 13 return $code;
132 }
133  
134 sub after_class {
135 4 ▶ my ( $self, $mg ) = @_;
Subroutine after_class: called CC=5 SCAR=16.3
Pod documented
136 4 ▶ if ( ( ref($mg) || "" ) =~ m/^(|HASH|ARRAY|SCALAR|CODE|GLOB)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((ref $mg || "") =~ /^(|HASH|ARRAY|SCALAR|CODE|GLOB)$/) 2 2 4
Condition100%2/2
ref $mg || ''
l!l
31
MC/DC100%1/1
ref $mg || ''
ref $mg
Truth table100%2/2
A
A= ref $mg
Aresult
11
00
137 2 ▶ $mg = defined $mg ? $mg : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $mg ? : 2 0 2
138 2 die
139 qq{Object: invalid value $mg for variable \$mg in method after_class};
140 }
141 2 my $cpo = join ', ', @{ $self->cpo };
142 2 $mg->use(q|Types::Standard qw/Any Item Bool Str Num Int Ref ScalarRef ArrayRef HashRef CodeRef RegexpRef GlobRef Object Map Tuple Dict Optional/|);
143 2 $mg->use(q|Compiled::Params::OO|);
144 2 $mg->our(q|$VALIDATE|);
145 2 my $code = qq|\$VALIDATE = Compiled::Params::OO::cpo( $cpo );|;
146 2 my %class = %Module::Generate::CLASS;
147 2 ▶ if ( $class{CURRENT}{BEGIN} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class{'CURRENT'}{'BEGIN'}) { } 0 2 2
148 0 ( my $begin = $class{CURRENT}{BEGIN} ) =~ s/\s*\}\s*$//;
149 0 $code = $begin . $code . "\}";
150 }
151 2 else { $code = qq|{ $code }|; }
152 2 $class{CURRENT}{BEGIN} = $code;
153 }
154  
155 4 1;
156  
157 __END__
158  
159 =head1 NAME
160  
161 Hades::Realm::Compiled::Params - The great new Hades::Realm::Compiled::Params!
162  
163 =head1 VERSION
164  
165 Version 0.03
166  
167 =cut
168  
169 =head1 SYNOPSIS
170  
171 use Hades::Realm::Compiled::Params;
172  
173 Hades::Realm::Compiled::Params->run({
174 eval => q|
175 Kosmos {
176 penthos :d(2) :p :pr :c :t(Int)
177 curae :r :t(Any)
178 geras $nosoi :t(Int) {
179 if ($self->penthos == $nosoi) {
180 return $self->curae;
181 }
182 }
183 }
184 |,
185 lib => 't/lib'
186 });
187  
188 ... generates ...
189  
190 package Kosmos;
191 use strict;
192 use warnings;
193 use Types::Standard qw/Str Optional HashRef Tuple Map Dict ArrayRef Int Any/;
194 use Compiled::Params::OO;
195 our $VERSION = 0.01;
196 our $VALIDATE;
197  
198 BEGIN {
199 $VALIDATE = Compiled::Params::OO::cpo(
200 penthos => [ Int, { default => 2 } ],
201 curae => [ Any ],
202 geras => [ Int, { default => 2 } ]
203 );
204 }
205  
206 sub new {
207 my ( $cls, %args ) = ( shift(), scalar @_ == 1 ? %{ $_[0] } : @_ );
208 my $self = bless {}, $cls;
209 my %accessors
210 = ( curae => { required => 1, }, penthos => { default => 2, }, );
211 for my $accessor ( keys %accessors ) {
212 my $value
213 = $self->$accessor(
214 defined $args{$accessor}
215 ? $args{$accessor}
216 : $accessors{$accessor}->{default} );
217 unless ( !$accessors{$accessor}->{required} || defined $value ) {
218 die "$accessor accessor is required";
219 }
220 }
221 return $self;
222 }
223  
224 sub penthos {
225 my ( $self, $value ) = @_;
226 my $private_caller = caller();
227 if ( $private_caller ne __PACKAGE__ ) {
228 die "cannot call private method penthos from $private_caller";
229 }
230 if ( defined $value ) {
231 ($value) = $VALIDATE->penthos->($value);
232 $self->{penthos} = $value;
233 }
234 return $self->{penthos};
235 }
236  
237 sub clear_penthos {
238 my ($self) = @_;
239 delete $self->{penthos};
240 return $self;
241 }
242  
243 sub has_penthos {
244 my ($self) = @_;
245 return exists $self->{penthos};
246 }
247  
248 sub curae {
249 my ( $self, $value ) = @_;
250 if ( defined $value ) {
251 ($value) = $VALIDATE->curae->($value);
252 $self->{curae} = $value;
253 }
254 return $self->{curae};
255 }
256  
257 sub geras {
258 my ( $self, $nosoi ) = ( shift, $VALIDATE->geras->(@_) );
259 if ( $self->penthos == $nosoi ) { return $self->curae; }
260 }
261  
262 =head1 SUBROUTINES/METHODS
263  
264 =head2 new
265  
266 Instantiate a new Hades::Realm::Compiled::Params object.
267  
268 Hades::Realm::Compiled::Params->new
269  
270 =head2 push_cpo
271  
272 call push_cpo method. Expects param $key to be a Str, param $value to be a Str.
273  
274 $obj->push_cpo($key, $value)
275  
276 =head2 build_accessor
277  
278 call build_accessor method.
279  
280 =head2 build_sub
281  
282 call build_sub method. Expects param $mg to be a Ref, param $name to be a Str, param $meta to be a HashRef.
283  
284 $obj->build_sub($mg, $name, $meta)
285  
286 =head2 build_type
287  
288 call build_type method. Expects param $name to be a Str, param $type to be a Optional[Str], param $value to be a Optional[Str].
289  
290 $obj->build_type($name, $type, $value)
291  
292 =head2 after_class
293  
294 call after_class method. Expects param $mg to be a Object.
295  
296 $obj->after_class($mg)
297  
298 =head1 ACCESSORS
299  
300 =head2 cpo
301  
302 get or set cpo.
303  
304 $obj->cpo;
305  
306 $obj->cpo($value);
307  
308 =head1 AUTHOR
309  
310 LNATION, C<< <email at lnation.org> >>
311  
312 =head1 BUGS
313  
314 Please report any bugs or feature requests to C<bug-hadesrealmcompiledparams at rt.cpan.org>, or through
315 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Hades-Realm-Compiled-Params>. I will be notified, and then you'll
316 automatically be notified of progress on your bug as I make changes.
317  
318 =head1 SUPPORT
319  
320 You can find documentation for this module with the perldoc command.
321  
322 perldoc Hades::Realm::Compiled::Params
323  
324 You can also look for information at:
325  
326 =over 4
327  
328 =item * RT: CPAN's request tracker (report bugs here)
329  
330 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Hades-Realm-Compiled-Params>
331  
332 =item * AnnoCPAN: Annotated CPAN documentation
333  
334 L<http://annocpan.org/dist/Hades-Realm-Compiled-Params>
335  
336 =item * CPAN Ratings
337  
338 L<https://cpanratings.perl.org/d/Hades-Realm-Compiled-Params>
339  
340 =item * Search CPAN
341  
342 L<https://metacpan.org/release/Hades-Realm-Compiled-Params>
343  
344 =back
345  
346 =head1 ACKNOWLEDGEMENTS
347  
348 =head1 LICENSE AND COPYRIGHT
349  
350 This software is Copyright (c) 2020 by LNATION.
351  
352 This is free software, licensed under:
353  
354 The Artistic License 2.0 (GPL Compatible)
355  
356 =cut
357  
358  
↑ Summary