blib/lib/Quaint.pm

filter: Statementstmt 92.9% 211 / 227 Branchbran 80.0% 40 / 50 Conditioncond 62.5% 15 / 24 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 97.3% 36 / 37 Podpod 0.0% 0 / 6 totaltotal 86.2% 306 / 355 CC 101 SCAR 17.2
CC
101
Cov
88%
__ANON__
12.0
eleven
12.0
one
12.0
CRAP 5.6
↑ Summary
1 package Quaint;
2  
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Module::Runtime qw/use_module/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Types::Standard qw/Any Bool Str Int Num HashRef ArrayRef Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '0.04';
11  
12 sub import {
13 9 ▶ my ($caller) = caller();
Subroutine import: called CC=1 SCAR=0.0
14  
15 9 my $metapkg;
16 {
17 5 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 $metapkg = meta::get_package( $caller );
19 }
20 9 $metapkg->add_symbol( '%META', \my %META );
21  
22 9 my $INDEX = 1;
23  
24 $metapkg->add_named_sub('ro', sub {
25 7 ▶ my @ro;
Subroutine ro: called CC=3 SCAR=11.0
26 7 for (@_) {
27 9 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 5 4 9
28 5 $_->{is} = 'ro';
29 5 push @ro, $_;
30 } else {
31 4 $META{attribute}{$_} = {
32 is => 'ro',
33 index => $INDEX++,
34 name => $_
35 };
36 4 push @ro, $META{attribute}{$_};
37 }
38 }
39 7 return @ro;
40 9 })->set_prototype('@');
41  
42 $metapkg->add_named_sub('req', sub {
43 0 ▶ my @req;
Subroutine __ANON__: not called CC=3 SCAR=14.2
Subroutine req: called CC=3 SCAR=14.2
44 0 for (@_) {
45 0 ▶ if (ref $_) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_) { } 0 0 0
if (ref $_) { } 3 8 11
46 0 $_->{required} = 1;
47 0 push @req, $_;
48 } else {
49 0 $META{attribute}{$_} = {
50 required => 1,
51 index => $INDEX++,
52 name => $_
53 };
54 0 push @req, $META{attribute}{$_};
55 }
56 }
57 0 return @req;
58 9 })->set_prototype('@');
59  
60 $metapkg->add_named_sub('default', sub {
61 4 ▶ my $default = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
62 4 my @def;
63 4 for (@_) {
64 6 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 3 3 6
65 3 $_->{default} = $default;
66 3 push @def, $_;
67 } else {
68 3 $META{attribute}{$_} = {
69 default => $default,
70 index => $INDEX++,
71 name => $_
72 };
73 3 push @def, $META{attribute}{$_};
74 }
75 }
76 4 return @def;
77 9 })->set_prototype('&@');
78  
79 $metapkg->add_named_sub('trigger', sub {
80 2 ▶ my $trigger = shift;
Subroutine trigger: called CC=3 SCAR=11.4
81 2 my @tri;
82 2 for (@_) {
83 2 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 0 2 2
84 0 $_->{trigger} = $trigger;
85 0 push @tri, $_;
86 } else {
87 2 $META{attribute}{$_} = {
88 trigger => $trigger,
89 index => $INDEX++,
90 name => $_
91 };
92 2 push @tri, $META{attribute}{$_};
93 }
94 }
95 2 return @tri;
96 9 })->set_prototype('&@');
97  
98 $metapkg->add_named_sub('before', sub {
99 2 ▶ my $function = shift;
Subroutine before: called CC=5 SCAR=16.3
100 2 my @before;
101 2 for (@_) {
102 2 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 1 1 2
103 1 push @{ $_->{before} }, $function;
104 1 push @before, $_;
105 } else {
106 1 ▶ my $item = $META{attribute}{$_} || $META{function}{$_} || { name => $_, before => [] };
Condition40%2/5
$META{'attribute'}{$_} || $META{'function'}{$_} || {'name', $_, 'before', []}
l!l
10
$META{"attribute"}{$_} or $META{"function"}{$_}
l!l&&r!l&&!r
100
MC/DC0%0/2
$META{'attribute'}{$_} || $META{'function'}{$_} || {'name', $_, 'before', []}
$META{"attribute"}{$_} $META{"function"}{$_}
Truth table33%1/3
A or B
A= $META{"attribute"}{$_}B= $META{"function"}{$_}
ABresult
1X1
011
000
107 1 push @{$item->{before}}, $function;
108 1 push @before, $item;
109 }
110 }
111 2 return @before;
112 9 })->set_prototype('&@');
113  
114 $metapkg->add_named_sub('around', sub {
115 2 ▶ my $function = shift;
Subroutine around: called CC=5 SCAR=17.6
116 2 my @around;
117 2 for (@_) {
118 2 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 0 2 2
119 0 push @{ $_->{around} }, $function;
120 0 push @around, $_;
121 } else {
122 2 ▶ my $item = $META{attribute}{$_} || $META{function}{$_} || { name => $_, around => [] };
Condition60%3/5
$META{'attribute'}{$_} || $META{'function'}{$_} || {'name', $_, 'around', []}
l!l
20
$META{"attribute"}{$_} or $META{"function"}{$_}
l!l&&r!l&&!r
110
MC/DC0%0/2
$META{'attribute'}{$_} || $META{'function'}{$_} || {'name', $_, 'around', []}
$META{"attribute"}{$_} $META{"function"}{$_}
Truth table66%2/3
A or B
A= $META{"attribute"}{$_}B= $META{"function"}{$_}
ABresult
1X1
011
000
123 2 push @{$item->{around}}, $function;
124 2 push @around, $item;
125 }
126 }
127 2 return @around;
128 9 })->set_prototype('&@');
129  
130 $metapkg->add_named_sub('after', sub {
131 2 ▶ my $function = shift;
Subroutine __ANON__: called CC=5 SCAR=16.3
132 2 my @after;
133 2 for (@_) {
134 2 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 1 1 2
135 1 push @{ $_->{after} }, $function;
136 1 push @after, $_;
137 } else {
138 1 ▶ my $item = $META{attribute}{$_} || $META{function}{$_} || { name => $_, after => [] };
Condition40%2/5
$META{'attribute'}{$_} || $META{'function'}{$_} || {'name', $_, 'after', []}
l!l
10
$META{"attribute"}{$_} or $META{"function"}{$_}
l!l&&r!l&&!r
100
MC/DC0%0/2
$META{'attribute'}{$_} || $META{'function'}{$_} || {'name', $_, 'after', []}
$META{"attribute"}{$_} $META{"function"}{$_}
Truth table33%1/3
A or B
A= $META{"attribute"}{$_}B= $META{"function"}{$_}
ABresult
1X1
011
000
139 1 push @{$item->{after}}, $function;
140 1 push @after, $item;
141 }
142 }
143 2 return @after;
144 9 })->set_prototype('&@');
145  
146  
147 my $make_attribute = sub {
148 18 ▶ my $type = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
149 18 for (@_) {
150 22 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 17 5 22
151 17 $_->{type} = $type;
152 17 $metapkg->add_named_sub($_->{name}, attribute($_));
153 } else {
154 5 $META{attribute}{$_} = { name => $_, type => $type, index => $INDEX++ };
155 5 $metapkg->add_named_sub($_, attribute($META{attribute}{$_}));
156 }
157 }
158 9 };
159  
160 $metapkg->add_named_sub('any', sub {
161 12 ▶ $make_attribute->(Any, @_);
Subroutine __ANON__: called CC=1 SCAR=0.0
162 9 })->set_prototype('@');
163  
164 $metapkg->add_named_sub('bool', sub {
165 1 ▶ $make_attribute->(Bool, @_);
Subroutine __ANON__: called CC=1 SCAR=0.0
166 9 })->set_prototype('@');
167  
168 $metapkg->add_named_sub('str', sub {
169 1 ▶ $make_attribute->(Str, @_);
Subroutine __ANON__: called CC=1 SCAR=0.0
170 9 })->set_prototype('@');
171  
172 $metapkg->add_named_sub('num', sub {
173 1 ▶ $make_attribute->(Num, @_);
Subroutine num: called CC=1 SCAR=0.0
174 9 })->set_prototype('@');
175  
176 $metapkg->add_named_sub('array', sub {
177 1 ▶ $make_attribute->(ArrayRef, @_);
Subroutine array: called CC=1 SCAR=0.0
178 9 })->set_prototype('@');
179  
180 $metapkg->add_named_sub('hash', sub {
181 1 ▶ $make_attribute->(HashRef, @_);
Subroutine hash: called CC=1 SCAR=0.0
182 9 })->set_prototype('@');
183  
184 $metapkg->add_named_sub('obj', sub {
185 1 ▶ $make_attribute->(Object, @_);
Subroutine __ANON__: called CC=1 SCAR=0.0
186 9 })->set_prototype('@');
187  
188 $metapkg->add_named_sub('function', sub {
189 2 ▶ my $function = shift;
Subroutine __ANON__: called CC=3 SCAR=12.0
190 2 for (@_) {
191 2 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 0 2 2
192 0 $_->{function} = $function;
193 0 $metapkg->add_named_sub($_->{name}, function($_));
194 } else {
195 2 $META{function}{$_} = { name => $_, function => $function };
196 2 $metapkg->add_named_sub($_, function($META{function}{$_}));
197 }
198 }
199 9 })->set_prototype('&@');
200  
201  
202 $metapkg->add_named_sub('extends', sub {
203 4 ▶ for (@_) {
Subroutine __ANON__: called CC=6 SCAR=17.9
204 4 eval { use_module($_); };
205 4 my $extend;
206 {
207 5 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
208 4 $extend = meta::get_package( $_ );
209 }
210 4 my %local = $extend->get_symbol('%META')->value;
211 4 my $isa = '@' . $caller . '::ISA';
212 4 eval "push $isa, '$_'";
213 4 for (sort { $local{attribute}{$a}{index} <=> $local{attribute}{$b}{index} } keys %{$local{attribute}}) {
214 18 ▶ if (!$META{attribute}{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($META{"attribute"}{$_}) 18 0 18
215 18 $META{attribute}{$_} = {%{$local{attribute}{$_}}};
216 18 $META{attribute}{$_}{index} = $INDEX++;
217 }
218 }
219 4 for ( keys %{ $local{function} } ) {
220 1 ▶ if (!$META{function}{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($META{"function"}{$_}) 1 0 1
221 1 $META{function}{$_} = {%{$local{function}{$_}}};
222 }
223 }
224 }
225 9 })->set_prototype('@');
226  
227 $metapkg->add_named_sub('new', sub {
228 10 ▶ my $self = bless {}, shift;
Subroutine __ANON__: called CC=5 SCAR=16.2
229 10 ▶ my %params = scalar @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 ? : 0 10 10
230 10 my @sorted_keys = sort { $META{attribute}{$a}{index} <=> $META{attribute}{$b}{index} } keys %{$META{attribute}};
231 10 $self->$_($META{attribute}{$_}{default}->($self)) for grep { $META{attribute}{$_}{default} } @sorted_keys;
232 10 $self->$_($params{$_}) for keys %params;
233 10 $self->$_ for grep { $META{attribute}{$_}{required} } @sorted_keys;
234 8 return $self;
235 9 });
236 }
237  
238 sub attribute {
239 22 ▶ my ($attr) = @_;
Subroutine attribute: called CC=1 SCAR=0.0
Pod undocumented
240 return sub {
241 128 ▶ $attr = scoped($_[0], 'attribute', $attr);
Subroutine __ANON__: called CC=12 SCAR=24.9
242 128 ▶ if (scalar @_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) 64 64 128
243 64 ▶ before($attr, @_) if ($attr->{before});
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"before"} 2 62 64
244 64 ▶ if ( $attr->{is} && $attr->{is} eq 'ro' && scalar caller() ne 'Quaint' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"is"} and $attr->{"is"} eq "ro" and scalar caller() ne "Quaint") 2 62 64
Condition83%5/6
$attr->{"is"} and $attr->{"is"} eq "ro" and scalar caller() ne "Quaint"
!ll&&!rl&&r
38242
$attr->{"is"} and $attr->{"is"} eq "ro"
!ll&&!rl&&r
38026
MC/DC66%2/3
$attr->{"is"} and $attr->{"is"} eq "ro" and scalar caller() ne "Quaint"
$attr->{"is"} $attr->{"is"} eq "ro" scalar caller() ne "Quaint"
Truth table75%3/4
A and B and C
A= $attr->{"is"}B= $attr->{"is"} eq "ro"C= scalar caller() ne "Quaint"
ABCresult
0XX0
10X0
1100
1111
245 2 die "attribute $attr->{name} is readonly";
246 }
247 62 ▶ my $val = $attr->{trigger} ? $attr->{trigger}->($_[0], $_[1]) : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
$attr->{'trigger'} ? : 8 54 62
248 62 ▶ ($val) = around($attr, $_[0], $val) if ($attr->{around});
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"around"} 2 60 62
249 62 ▶ $_[0]->{$attr->{name}} = $attr->{type} ? $attr->{type}->($val) : $val;
Branch: 1 branch
BranchTrueFalseTotal
$attr->{'type'} ? : 62 0 62
250 62 ▶ after($attr, $_[0], $_[0]->{$attr->{name}}) if ($attr->{after});
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"after"} 2 60 62
251 }
252 126 ▶ if ($attr->{required} && ! defined $_[0]->{$attr->{name}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"required"} and not defined $_[0]{$attr->{"name"}}) 2 124 126
Condition100%3/3
$attr->{"required"} and not defined $_[0]{$attr->{"name"}}
!ll&&!rl&&r
78462
MC/DC100%2/2
$attr->{"required"} and not defined $_[0]{$attr->{"name"}}
$attr->{"required"} not defined $_[0]{$attr->{"name"}}
Truth table100%3/3
A and not(B)
A= $attr->{"required"}B= not defined $_[0]{$attr->{"name"}}
ABresult
0X0
100
111
253 2 die "attribute $attr->{name} is required";
254 }
255 return $_[0]->{$attr->{name}}
256 124 }
257 22 }
258  
259 sub function {
260 2 ▶ my ($attr) = @_;
Subroutine function: called CC=1 SCAR=0.0
Pod undocumented
261 return sub {
262 3 ▶ my ($self, @params) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.1
263 3 $attr = scoped($self, 'function', $attr);
264 3 ▶ before($attr, $self, @params) if ($attr->{before});
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"before"} 1 2 3
265 3 ▶ @params = around($attr, $self, @params) if ($attr->{around});
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"around"} 1 2 3
266 3 @params = $attr->{function}->($self, @params);
267 3 ▶ after($attr, $self, @params) if ($attr->{after});
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"after"} 1 2 3
268 3 ▶ return wantarray ? @params : $params[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 3 3
269 2 };
270 }
271  
272 sub scoped {
273 131 ▶ my ($self, $type, $attr) = @_;
Subroutine scoped: called CC=1 SCAR=0.0
Pod undocumented
274 131 my $extend;
275 {
276 5 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
277 131 $extend = meta::get_package( ref $self );
278 }
279 131 my %local = $extend->get_symbol('%META')->value;
280 131 return $local{$type}{$attr->{name}};
281 }
282  
283 sub before {
284 3 ▶ my ($attr, @params) = @_;
Subroutine before: called CC=2 SCAR=6.9
Pod undocumented
285 3 for (@{$attr->{before}}) {
286 3 $_->(@params);
287 }
288 }
289  
290 sub around {
291 3 ▶ my ($attr, $self, @val) = @_;
Subroutine around: called CC=2 SCAR=6.9
Pod undocumented
292 3 for (@{$attr->{around}}) {
293 3 @val = $_->($self, @val);
294 }
295 3 return @val;
296 }
297  
298 sub after {
299 3 ▶ my ($attr, @params) = @_;
Subroutine after: called CC=2 SCAR=6.9
Pod undocumented
300 3 for (@{$attr->{after}}) {
301 3 $_->(@params);
302 }
303 }
304  
305  
306 5 1;
307  
308 __END__
↑ Summary