Venus/Data.pm

filter: Statementstmt 94.1% 81 / 86 Branchbran 66.6% 28 / 42 Conditioncond 77.7% 14 / 18 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 12 / 12 Podpod 75.0% 6 / 8 totaltotal 83.5% 147 / 176 CC 30 SCAR 29.5
CC
30
Cov
84%
search
31.6
explode
6.0
data
3.1
CRAP 19.1
« Venus/Role/Mappable.pm ↑ Summary Venus/Boolean.pm »
1 package Venus::Data;
2  
3 61 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 61 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 61 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 61 ▶use Venus::Class 'base', 'attr';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 61 base 'Venus::Path';
11  
12 # ATTRIBUTES
13  
14 61 attr 'from';
15 61 attr 'stag';
16 61 attr 'etag';
17  
18 # BUILDERS
19  
20 sub build_self {
21 91 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=1 SCAR=0.0
Pod undocumented
22  
23 91 return $self->docs;
24 };
25  
26 # METHODS
27  
28 sub count {
29 2 ▶ my ($self, $data) = @_;
Subroutine count: called CC=1 SCAR=0.0
Pod documented
30  
31 2 my @result = ($self->search($data));
32  
33 2 return scalar @result;
34 }
35  
36 sub data {
37 6 ▶ my ($self) = @_;
Subroutine data: called CC=3 SCAR=11.3
Pod documented
38  
39 6 my $data = $self->read;
40  
41 6 ▶ $data = (split(/^__END__/m, (split(/^__DATA__/m, $data))[1] || ''))[0] || '';
Condition50%2/4
(split(/^__END__/mu, (split(/^__DATA__/mu, $data, 0))[1] || '', 0))[0] || ''
l!l
60
(split(/^__DATA__/mu, $data, 0))[1] || ''
l!l
60
MC/DC0%0/1
(split(/^__END__/mu, (split(/^__DATA__/mu, $data, 0))[1] || '', 0))[0] || ''
(split(/^__END__/mu, (split(/^__DATA__/mu, $data, 0))[1] || '', 0))[0]
MC/DC0%0/1
(split(/^__DATA__/mu, $data, 0))[1] || ''
(split(/^__DATA__/mu, $data, 0))[1]
Truth table50%1/2
A
A= (split(/^__END__/mu, (split(/^__DATA__/mu, $data, 0))[1] || '', 0))[0]
Aresult
11
00
Truth table50%1/2
A
A= (split(/^__DATA__/mu, $data, 0))[1]
Aresult
11
00
42  
43 6 $data =~ s/^\s+|\s+$//g;
44  
45 6 return $data;
46 }
47  
48 sub docs {
49 98 ▶ my ($self) = @_;
Subroutine docs: called CC=1 SCAR=0.0
Pod documented
50  
51 98 $self->stag('=');
52 98 $self->etag('=cut');
53 98 $self->from('read');
54  
55 98 return $self;
56 }
57  
58 sub explode {
59 3989 ▶ my ($self) = @_;
Subroutine explode: called CC=6 SCAR=17.9
Pod undocumented
60  
61 3989 my $from = $self->from;
62 3989 my $data = $self->$from;
63 3989 my $stag = $self->stag;
64 3989 my $etag = $self->etag;
65  
66 3989 my @chunks = split /^(?:\@$stag|$stag)\s*(.+?)\s*\r?\n/m, ($data . "\n");
67  
68 3989 shift @chunks;
69  
70 3989 my $items = [];
71  
72 3989 ▶ while (my ($meta, $data) = splice @chunks, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@chunks, 0, 2)) 872086 3989 876075
73 872086 ▶ next unless $meta && $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta and $data 393 871693 872086
Condition66%2/3
$meta and $data
!ll&&!rl&&r
0393871693
MC/DC50%1/2
$meta and $data
$meta $data
Truth table66%2/3
A and B
A= $metaB= $data
ABresult
0X0
100
111
74 871693 ▶ next unless $meta ne $etag;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta ne $etag 0 871693 871693
75  
76 871693 my @info = split /\s/, $meta, 2;
77 871693 ▶ my ($list, $name) = @info == 2 ? @info : (undef, @info);
Branch: 1 branch
BranchTrueFalseTotal
@info == 2 ? : 527570 344123 871693
78  
79 871693 $data =~ s/(\r?\n)\+$stag/$1$stag/g; # auto-escape nested syntax
80 871693 $data = [split /\r?\n\r?\n/, $data];
81  
82 871693 my $item = {name => $name, data => $data, index => @$items + 1, list => $list};
83  
84 871693 push @$items, $item;
85 }
86  
87 3989 return $items;
88 }
89  
90 sub find {
91 1419 ▶ my ($self, $list, $name) = @_;
Subroutine find: called CC=1 SCAR=0.0
Pod documented
92  
93 1419 return $self->search({list => $list, name => $name});
94 }
95  
96 sub search {
97 3989 ▶ my ($self, $data) = @_;
Subroutine search: called CC=23 SCAR=34.5
Pod documented
98  
99 3989 ▶ $data //= {};
Condition100%2/2
$data //= {}
l!l
39872
MC/DC100%1/1
$data //= {}
$data
Truth table100%2/2
A
A= $data
Aresult
11
00
100  
101 3989 my $exploded = $self->explode;
102  
103 3989 ▶ return wantarray ? (@$exploded) : $exploded if !keys %$data;
Branch: 2 branches
BranchTrueFalseTotal
unless keys %$data 2 3987 3989
wantarray ? : 2 0 2
104  
105 3987 my @result;
106  
107 3987 my $sought = {map +($_, 1), keys %$data};
108  
109 3987 for my $item (sort {$a->{index} <=> $b->{index}} @$exploded) {
110 871684 my $found = {};
111  
112 871684 my $text;
113 871684 ▶ if ($text = $data->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text = $data->{"data"}) 0 871684 871684
114 0 ▶ $text = ref($text) eq 'Regexp' ? $text : qr/^$text$/;
Branch: 1 branch
BranchTrueFalseTotal
ref $text eq 'Regexp' ? : 0 0 0
115 0 ▶ $found->{data} = 1 if "@{$item->{data}}" =~ $text;
Branch: 1 branch
BranchTrueFalseTotal
if join($", @{$item->{"data"};}) =~ /$text/u 0 0 0
116 }
117  
118 871684 my $index;
119 871684 ▶ if ($index = $data->{index}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index = $data->{"index"}) 0 871684 871684
120 0 ▶ $index = ref($index) eq 'Regexp' ? $index : qr/^$index$/;
Branch: 1 branch
BranchTrueFalseTotal
ref $index eq 'Regexp' ? : 0 0 0
121 0 ▶ $found->{index} = 1 if $item->{index} =~ $index;
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"index"} =~ /$index/u 0 0 0
122 }
123  
124 871684 my $list;
125 871684 ▶ if ($list = $data->{list}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list = $data->{'list'}) { } 651224 220460 871684
126 651224 ▶ $list = (ref($list) eq 'Regexp' ? $list : qr/^$list$/);
Branch: 1 branch
BranchTrueFalseTotal
ref $list eq 'Regexp' ? : 0 651224 651224
127 651224 ▶ $found->{list} = 1 if defined $item->{list} && $item->{list} =~ $list;
Branch: 1 branch
BranchTrueFalseTotal
if defined $item->{"list"} and $item->{"list"} =~ /$list/u 45668 605556 651224
Condition100%3/3
defined $item->{"list"} and $item->{"list"} =~ /$list/u
!ll&&!rl&&r
25642734912945668
MC/DC100%2/2
defined $item->{"list"} and $item->{"list"} =~ /$list/u
defined $item->{"list"} $item->{"list"} =~ /$list/u
Truth table100%3/3
A and B
A= defined $item->{"list"}B= $item->{"list"} =~ /$list/u
ABresult
0X0
100
111
128 }
129 else {
130 $found->{list} = 1 if (exists $data->{list} && !defined $data->{list})
131 220460 ▶ && !defined $item->{list};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"list"} and not defined $data->{"list"} and not defined $item->{"list"} 87523 132937 220460
Condition83%5/6
exists $data->{"list"} and not defined $data->{"list"} and not defined $item->{"list"}
!ll&&!rl&&r
36013257787523
exists $data->{"list"} and not defined $data->{"list"}
!ll&&!rl&&r
3600220100
MC/DC66%2/3
exists $data->{"list"} and not defined $data->{"list"} and not defined $item->{"list"}
exists $data->{"list"} not defined $data->{"list"} not defined $item->{"list"}
Truth table75%3/4
A and not(B) and not(C)
A= exists $data->{"list"}B= not defined $data->{"list"}C= not defined $item->{"list"}
ABCresult
0XX0
10X0
1100
1111
132 }
133  
134 871684 my $name;
135 871684 ▶ if ($name = $data->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name = $data->{"name"}) 871564 120 871684
136 871564 ▶ $name = ref($name) eq 'Regexp' ? $name : qr/^$name$/;
Branch: 1 branch
BranchTrueFalseTotal
ref $name eq 'Regexp' ? : 0 871564 871564
137 871564 ▶ $found->{name} = 1 if $item->{name} =~ $name;
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"name"} =~ /$name/u 21676 849888 871564
138 }
139  
140 871684 ▶ if (not(grep(not(defined($found->{$_})), keys(%$sought)))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep((!defined($found->{$_})), keys %$sought)) 4007 867677 871684
141 4007 push @result, $item;
142 }
143 }
144  
145 3987 ▶ return wantarray ? (@result) : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 617 3370 3987
146 }
147  
148 sub text {
149 7 ▶ my ($self) = @_;
Subroutine text: called CC=1 SCAR=0.0
Pod documented
150  
151 7 $self->stag('@@ ');
152 7 $self->etag('@@ end');
153 7 $self->from('data');
154  
155 7 return $self;
156 }
157  
158 61 1;
159  
160  
161  
162 =head1 NAME
163  
164 Venus::Data - Data Class
165  
166 =cut
167  
168 =head1 ABSTRACT
169  
170 Data Class for Perl 5
171  
172 =cut
173  
174 =head1 SYNOPSIS
175  
176 package main;
177  
178 use Venus::Data;
179  
180 my $data = Venus::Data->new('t/data/sections');
181  
182 # $data->find(undef, 'name');
183  
184 =cut
185  
186 =head1 DESCRIPTION
187  
188 This package provides methods for extracting C<DATA> sections and POD blocks
189 from any file or package. The package can be configured to parse either POD or
190 DATA blocks, and it defaults to being configured for POD blocks.
191  
192 =head2 DATA syntax
193  
194 __DATA__
195  
196 # data syntax
197  
198 @@ name
199  
200 Example Name
201  
202 @@ end
203  
204 @@ titles #1
205  
206 Example Title #1
207  
208 @@ end
209  
210 @@ titles #2
211  
212 Example Title #2
213  
214 @@ end
215  
216 =head2 DATA syntax (nested)
217  
218 __DATA__
219  
220 # data syntax (nested)
221  
222 @@ nested
223  
224 Example Nested
225  
226 +@@ demo
227  
228 blah blah blah
229  
230 +@@ end
231  
232 @@ end
233  
234 =head2 POD syntax
235  
236 # pod syntax
237  
238 =head1 NAME
239  
240 Example #1
241  
242 =cut
243  
244 =head1 NAME
245  
246 Example #2
247  
248 =cut
249  
250 # pod-ish syntax
251  
252 =name
253  
254 Example #1
255  
256 =cut
257  
258 =name
259  
260 Example #2
261  
262 =cut
263  
264 =head2 POD syntax (nested)
265  
266 # pod syntax (nested)
267  
268 =nested
269  
270 Example #1
271  
272 +=head1 WHY?
273  
274 blah blah blah
275  
276 +=cut
277  
278 More information on the same topic as was previously mentioned in the
279 previous section demonstrating the topic, obviously from said section.
280  
281 =cut
282  
283 =cut
284  
285 =head1 INHERITS
286  
287 This package inherits behaviors from:
288  
289 L<Venus::Path>
290  
291 =cut
292  
293 =head1 METHODS
294  
295 This package provides the following methods:
296  
297 =cut
298  
299 =head2 count
300  
301 count(HashRef $criteria) (Int)
302  
303 The count method uses the criteria provided to L</search> for and return the
304 number of blocks found.
305  
306 I<Since C<0.01>>
307  
308 =over 4
309  
310 =item count example 1
311  
312 # given: synopsis;
313  
314 my $count = $data->docs->count;
315  
316 # 6
317  
318 =back
319  
320 =over 4
321  
322 =item count example 2
323  
324 # given: synopsis;
325  
326 my $count = $data->text->count;
327  
328 # 3
329  
330 =back
331  
332 =cut
333  
334 =head2 data
335  
336 data() (Str)
337  
338 The data method returns the text between the C<DATA> and C<END> sections of a
339 Perl package or file.
340  
341 I<Since C<0.01>>
342  
343 =over 4
344  
345 =item data example 1
346  
347 # given: synopsis;
348  
349 $data = $data->data;
350  
351 # ...
352  
353 =back
354  
355 =cut
356  
357 =head2 docs
358  
359 docs() (Data)
360  
361 The docs method configures the instance for parsing POD blocks.
362  
363 I<Since C<0.01>>
364  
365 =over 4
366  
367 =item docs example 1
368  
369 # given: synopsis;
370  
371 my $docs = $data->docs;
372  
373 # bless({ etag => "=cut", from => "read", stag => "=", ... }, "Venus::Data")
374  
375 =back
376  
377 =cut
378  
379 =head2 find
380  
381 find(Maybe[Str] $list, Maybe[Str] $name) (ArrayRef)
382  
383 The find method is a wrapper around L</search> as shorthand for searching by
384 C<list> and C<name>.
385  
386 I<Since C<0.01>>
387  
388 =over 4
389  
390 =item find example 1
391  
392 # given: synopsis;
393  
394 my $find = $data->docs->find(undef, 'name');
395  
396 # [
397 # { data => ["Example #1"], index => 4, list => undef, name => "name" },
398 # { data => ["Example #2"], index => 5, list => undef, name => "name" },
399 # ]
400  
401 =back
402  
403 =over 4
404  
405 =item find example 2
406  
407 # given: synopsis;
408  
409 my $find = $data->docs->find('head1', 'NAME');
410  
411 # [
412 # { data => ["Example #1"], index => 1, list => "head1", name => "NAME" },
413 # { data => ["Example #2"], index => 2, list => "head1", name => "NAME" },
414 # ]
415  
416 =back
417  
418 =over 4
419  
420 =item find example 3
421  
422 # given: synopsis;
423  
424 my $find = $data->text->find(undef, 'name');
425  
426 # [
427 # { data => ["Example Name"], index => 1, list => undef, name => "name" },
428 # ]
429  
430 =back
431  
432 =over 4
433  
434 =item find example 4
435  
436 # given: synopsis;
437  
438 my $find = $data->text->find('titles', '#1');
439  
440 # [
441 # { data => ["Example Title #1"], index => 2, list => "titles", name => "#1" },
442 # ]
443  
444 =back
445  
446 =cut
447  
448 =head2 search
449  
450 The search method returns the set of blocks matching the criteria provided.
451 This method can return a list of values in list-context.
452  
453 =over 4
454  
455 =item search example 1
456  
457 # given: synopsis;
458  
459 my $search = $data->docs->search({list => undef, name => 'name'});
460  
461 # [
462 # { data => ["Example #1"], index => 4, list => undef, name => "name" },
463 # { data => ["Example #2"], index => 5, list => undef, name => "name" },
464 # ]
465  
466 =back
467  
468 =over 4
469  
470 =item search example 2
471  
472 # given: synopsis;
473  
474 my $search = $data->docs->search({list => 'head1', name => 'NAME'});
475  
476 # [
477 # { data => ["Example #1"], index => 1, list => "head1", name => "NAME" },
478 # { data => ["Example #2"], index => 2, list => "head1", name => "NAME" },
479 # ]
480  
481 =back
482  
483 =over 4
484  
485 =item search example 3
486  
487 # given: synopsis;
488  
489 my $find = $data->text->search({list => undef, name => 'name'});
490  
491 # [
492 # { data => ["Example Name"], index => 1, list => undef, name => "name" },
493 # ]
494  
495 =back
496  
497 =over 4
498  
499 =item search example 4
500  
501 # given: synopsis;
502  
503 my $search = $data->text->search({list => 'titles', name => '#1'});
504  
505 # [
506 # { data => ["Example Title #1"], index => 2, list => "titles", name => "#1" },
507 # ]
508  
509 =back
510  
511 =cut
512  
513 =head2 text
514  
515 text() (Data)
516  
517 The text method configures the instance for parsing DATA blocks.
518  
519 I<Since C<0.01>>
520  
521 =over 4
522  
523 =item text example 1
524  
525 # given: synopsis;
526  
527 my $text = $data->text;
528  
529 # bless({ etag => '@@ end', from => 'data', stag => '@@ ', ... }, "Venus::Data")
530  
531 =back
532  
533 =cut
« Venus/Role/Mappable.pm ↑ Summary Venus/Boolean.pm »