blib/lib/Venus/Data.pm

filter: Statementstmt 88.1% 89 / 101 Branchbran 68.1% 30 / 44 Conditioncond 66.6% 14 / 21 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 86.6% 13 / 15 Podpod 80.0% 8 / 10 totaltotal 78.8% 160 / 203 SCAR 37.3
CC
33
Cov
80%
search
32.9
explode
6.0
__ANON__
6.0
CRAP 41.6
« blib/lib/Venus/Dump.pm ↑ Summary blib/lib/Venus/Gather.pm »
1 package Venus::Data;
2  
3 96 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 96 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 96 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 96 ▶use Venus::Class 'attr', 'base';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 96 base 'Venus::Path';
11  
12 # ATTRIBUTES
13  
14 96 attr 'from';
15 96 attr 'stag';
16 96 attr 'etag';
17  
18 # BUILDERS
19  
20 sub build_self {
21 271 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=1 SCAR=0.0
Pod undocumented
22  
23 271 return $self->docs;
24 };
25  
26 # METHODS
27  
28 sub assertion {
29 0 ▶ my ($self) = @_;
Subroutine assertion: not called CC=1 SCAR=6.9
Pod documented
30  
31 0 my $assertion = $self->SUPER::assertion;
32  
33 $assertion->match('string')->format(sub{
34 0 ▶ (ref $self || $self)->new($_)
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
35 0 });
36  
37 0 return $assertion;
38 }
39  
40 sub count {
41 1263 ▶ my ($self, $data) = @_;
Subroutine count: called CC=1 SCAR=0.0
Pod documented
42  
43 1263 my @result = ($self->search($data));
44  
45 1263 return scalar @result;
46 }
47  
48 sub data {
49 8 ▶ my ($self) = @_;
Subroutine data: called CC=3 SCAR=11.3
Pod documented
50  
51 8 my $data = $self->read;
52  
53 8 ▶ $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
80
(split(/^__DATA__/mu, $data, 0))[1] || ''
l!l
80
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
54  
55 8 $data =~ s/^\s+|\s+$//g;
56  
57 8 return $data;
58 }
59  
60 sub docs {
61 280 ▶ my ($self) = @_;
Subroutine docs: called CC=1 SCAR=0.0
Pod documented
62  
63 280 $self->stag('=');
64 280 $self->etag('=cut');
65 280 $self->from('read');
66  
67 280 return $self;
68 }
69  
70 sub explode {
71 7601 ▶ my ($self) = @_;
Subroutine explode: called CC=6 SCAR=17.9
Pod undocumented
72  
73 7601 my $from = $self->from;
74 7601 my $data = $self->$from;
75 7601 my $stag = $self->stag;
76 7601 my $etag = $self->etag;
77  
78 7601 my @chunks = split /^(?:\@$stag|$stag)\s*(.+?)\s*\r?\n/m, ($data . "\n");
79  
80 7601 shift @chunks;
81  
82 7601 my $items = [];
83  
84 7601 ▶ while (my ($meta, $data) = splice @chunks, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@chunks, 0, 2)) 2508983 7601 2516584
85 2508983 ▶ next unless $meta && $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta and $data 196008 2312975 2508983
Condition66%2/3
$meta and $data
!ll&&!rl&&r
01960082312975
MC/DC50%1/2
$meta and $data
$meta $data
Truth table66%2/3
A and B
A= $metaB= $data
ABresult
0X0
100
111
86 2312975 ▶ next unless $meta ne $etag;
Branch: 1 branch
BranchTrueFalseTotal
unless $meta ne $etag 0 2312975 2312975
87  
88 2312975 my @info = split /\s/, $meta, 2;
89 2312975 ▶ my ($list, $name) = @info == 2 ? @info : (undef, @info);
Branch: 1 branch
BranchTrueFalseTotal
@info == 2 ? : 1507145 805830 2312975
90  
91 2312975 $data =~ s/(\r?\n)\+$stag/$1$stag/g; # auto-escape nested syntax
92 2312975 $data = [split /\r?\n\r?\n/, $data];
93  
94 2312975 my $item = {name => $name, data => $data, index => @$items + 1, list => $list};
95  
96 2312975 push @$items, $item;
97 }
98  
99 7601 return $items;
100 }
101  
102 sub find {
103 6178 ▶ my ($self, $list, $name) = @_;
Subroutine find: called CC=1 SCAR=0.0
Pod documented
104  
105 6178 return $self->search({list => $list, name => $name});
106 }
107  
108 sub search {
109 7601 ▶ my ($self, $data) = @_;
Subroutine search: called CC=23 SCAR=34.9
Pod documented
110  
111 7601 ▶ $data //= {};
Condition100%2/2
$data //= {}
l!l
75992
MC/DC100%1/1
$data //= {}
$data
Truth table100%2/2
A
A= $data
Aresult
11
00
112  
113 7601 my $exploded = $self->explode;
114  
115 7601 ▶ return wantarray ? (@$exploded) : $exploded if !keys %$data;
Branch: 2 branches
BranchTrueFalseTotal
unless keys %$data 2 7599 7601
wantarray ? : 2 0 2
116  
117 7599 my @result;
118  
119 7599 my $sought = {map +($_, 1), keys %$data};
120  
121 7599 for my $item (sort {$a->{index} <=> $b->{index}} @$exploded) {
122 2312965 my $found = {};
123  
124 2312965 my $text;
125 2312965 ▶ if ($text = $data->{data}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text = $data->{"data"}) 0 2312965 2312965
126 0 ▶ $text = ref($text) eq 'Regexp' ? $text : qr/^@{[quotemeta($text)]}$/;
Branch: 1 branch
BranchTrueFalseTotal
ref $text eq 'Regexp' ? : 0 0 0
127 0 ▶ $found->{data} = 1 if "@{$item->{data}}" =~ $text;
Branch: 1 branch
BranchTrueFalseTotal
if join($", @{$item->{"data"};}) =~ /$text/u 0 0 0
128 }
129  
130 2312965 my $index;
131 2312965 ▶ if ($index = $data->{index}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index = $data->{"index"}) 0 2312965 2312965
132 0 ▶ $index = ref($index) eq 'Regexp' ? $index : qr/^@{[quotemeta($index)]}$/;
Branch: 1 branch
BranchTrueFalseTotal
ref $index eq 'Regexp' ? : 0 0 0
133 0 ▶ $found->{index} = 1 if $item->{index} =~ $index;
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"index"} =~ /$index/u 0 0 0
134 }
135  
136 2312965 my $list;
137 2312965 ▶ if ($list = $data->{list}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list = $data->{'list'}) { } 1435488 877477 2312965
138 1435488 ▶ $list = (ref($list) eq 'Regexp' ? $list : qr/^@{[quotemeta($list)]}$/);
Branch: 1 branch
BranchTrueFalseTotal
ref $list eq 'Regexp' ? : 0 1435488 1435488
139 1435488 ▶ $found->{list} = 1 if defined $item->{list} && $item->{list} =~ $list;
Branch: 1 branch
BranchTrueFalseTotal
if defined $item->{"list"} and $item->{"list"} =~ /$list/u 132774 1302714 1435488
Condition100%3/3
defined $item->{"list"} and $item->{"list"} =~ /$list/u
!ll&&!rl&&r
496420806294132774
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
140 }
141 else {
142 $found->{list} = 1 if (exists $data->{list} && !defined $data->{list})
143 877477 ▶ && !defined $item->{list};
Branch: 1 branch
BranchTrueFalseTotal
if exists $data->{"list"} and not defined $data->{"list"} and not defined $item->{"list"} 281308 596169 877477
Condition83%5/6
exists $data->{"list"} and not defined $data->{"list"} and not defined $item->{"list"}
!ll&&!rl&&r
106544489625281308
exists $data->{"list"} and not defined $data->{"list"}
!ll&&!rl&&r
1065440770933
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
144 }
145  
146 2312965 my $name;
147 2312965 ▶ if ($name = $data->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name = $data->{"name"}) 2310033 2932 2312965
148 2310033 ▶ $name = ref($name) eq 'Regexp' ? $name : qr/^@{[quotemeta($name)]}$/;
Branch: 1 branch
BranchTrueFalseTotal
ref $name eq 'Regexp' ? : 0 2310033 2310033
149 2310033 ▶ $found->{name} = 1 if $item->{name} =~ $name;
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"name"} =~ /$name/u 31475 2278558 2310033
150 }
151  
152 2312965 ▶ if (not(grep(not(defined($found->{$_})), keys(%$sought)))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep((!defined($found->{$_})), keys %$sought)) 7984 2304981 2312965
153 7984 push @result, $item;
154 }
155 }
156  
157 7599 ▶ return wantarray ? (@result) : \@result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7591 8 7599
158 }
159  
160 sub string {
161 5 ▶ my ($self, $list, $name) = @_;
Subroutine string: called CC=3 SCAR=11.0
Pod documented
162  
163 5 my @result;
164  
165 5 for my $item ($self->find($list, $name)) {
166 8 push @result, join "\n\n", @{$item->{data}};
167 }
168  
169 5 ▶ return wantarray ? (@result) : join "\n", @result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 4 5
170 }
171  
172 sub text {
173 8 ▶ my ($self) = @_;
Subroutine text: called CC=1 SCAR=0.0
Pod documented
174  
175 8 $self->stag('@@ ');
176 8 $self->etag('@@ end');
177 8 $self->from('data');
178  
179 8 return $self;
180 }
181  
182 96 1;
183  
184  
185  
186 =head1 NAME
187  
188 Venus::Data - Data Class
189  
190 =cut
191  
192 =head1 ABSTRACT
193  
194 Data Class for Perl 5
195  
196 =cut
197  
198 =head1 SYNOPSIS
199  
200 package main;
201  
202 use Venus::Data;
203  
204 my $data = Venus::Data->new('t/data/sections');
205  
206 # $data->find(undef, 'name');
207  
208 =cut
209  
210 =head1 DESCRIPTION
211  
212 This package provides methods for extracting C<DATA> sections and POD blocks
213 from any file or package. The package can be configured to parse either POD or
214 DATA blocks, and it defaults to being configured for POD blocks.
215  
216 =head2 DATA syntax
217  
218 __DATA__
219  
220 # data syntax
221  
222 @@ name
223  
224 Example Name
225  
226 @@ end
227  
228 @@ titles #1
229  
230 Example Title #1
231  
232 @@ end
233  
234 @@ titles #2
235  
236 Example Title #2
237  
238 @@ end
239  
240 =head2 DATA syntax (nested)
241  
242 __DATA__
243  
244 # data syntax (nested)
245  
246 @@ nested
247  
248 Example Nested
249  
250 +@@ demo
251  
252 blah blah blah
253  
254 +@@ end
255  
256 @@ end
257  
258 =head2 POD syntax
259  
260 # pod syntax
261  
262 =head1 NAME
263  
264 Example #1
265  
266 =cut
267  
268 =head1 NAME
269  
270 Example #2
271  
272 =cut
273  
274 # pod-ish syntax
275  
276 =name
277  
278 Example #1
279  
280 =cut
281  
282 =name
283  
284 Example #2
285  
286 =cut
287  
288 =head2 POD syntax (nested)
289  
290 # pod syntax (nested)
291  
292 =nested
293  
294 Example #1
295  
296 +=head1 WHY?
297  
298 blah blah blah
299  
300 +=cut
301  
302 More information on the same topic as was previously mentioned in the
303 previous section demonstrating the topic, obviously from said section.
304  
305 =cut
306  
307 =cut
308  
309 =head1 INHERITS
310  
311 This package inherits behaviors from:
312  
313 L<Venus::Path>
314  
315 =cut
316  
317 =head1 METHODS
318  
319 This package provides the following methods:
320  
321 =cut
322  
323 =head2 count
324  
325 count(hashref $criteria) (number)
326  
327 The count method uses the criteria provided to L</search> for and return the
328 number of blocks found.
329  
330 I<Since C<0.01>>
331  
332 =over 4
333  
334 =item count example 1
335  
336 # given: synopsis;
337  
338 my $count = $data->docs->count;
339  
340 # 6
341  
342 =back
343  
344 =over 4
345  
346 =item count example 2
347  
348 # given: synopsis;
349  
350 my $count = $data->text->count;
351  
352 # 3
353  
354 =back
355  
356 =cut
357  
358 =head2 data
359  
360 data() (string)
361  
362 The data method returns the text between the C<DATA> and C<END> sections of a
363 Perl package or file.
364  
365 I<Since C<0.01>>
366  
367 =over 4
368  
369 =item data example 1
370  
371 # given: synopsis;
372  
373 $data = $data->data;
374  
375 # ...
376  
377 =back
378  
379 =cut
380  
381 =head2 docs
382  
383 docs() (Venus::Data)
384  
385 The docs method configures the instance for parsing POD blocks.
386  
387 I<Since C<0.01>>
388  
389 =over 4
390  
391 =item docs example 1
392  
393 # given: synopsis;
394  
395 my $docs = $data->docs;
396  
397 # bless({ etag => "=cut", from => "read", stag => "=", ... }, "Venus::Data")
398  
399 =back
400  
401 =cut
402  
403 =head2 find
404  
405 find(maybe[string] $list, maybe[string] $name) (arrayref)
406  
407 The find method is a wrapper around L</search> as shorthand for searching by
408 C<list> and C<name>.
409  
410 I<Since C<0.01>>
411  
412 =over 4
413  
414 =item find example 1
415  
416 # given: synopsis;
417  
418 my $find = $data->docs->find(undef, 'name');
419  
420 # [
421 # { data => ["Example #1"], index => 4, list => undef, name => "name" },
422 # { data => ["Example #2"], index => 5, list => undef, name => "name" },
423 # ]
424  
425 =back
426  
427 =over 4
428  
429 =item find example 2
430  
431 # given: synopsis;
432  
433 my $find = $data->docs->find('head1', 'NAME');
434  
435 # [
436 # { data => ["Example #1"], index => 1, list => "head1", name => "NAME" },
437 # { data => ["Example #2"], index => 2, list => "head1", name => "NAME" },
438 # ]
439  
440 =back
441  
442 =over 4
443  
444 =item find example 3
445  
446 # given: synopsis;
447  
448 my $find = $data->text->find(undef, 'name');
449  
450 # [
451 # { data => ["Example Name"], index => 1, list => undef, name => "name" },
452 # ]
453  
454 =back
455  
456 =over 4
457  
458 =item find example 4
459  
460 # given: synopsis;
461  
462 my $find = $data->text->find('titles', '#1');
463  
464 # [
465 # { data => ["Example Title #1"], index => 2, list => "titles", name => "#1" },
466 # ]
467  
468 =back
469  
470 =cut
471  
472 =head2 search
473  
474 The search method returns the set of blocks matching the criteria provided.
475 This method can return a list of values in list-context.
476  
477 =over 4
478  
479 =item search example 1
480  
481 # given: synopsis;
482  
483 my $search = $data->docs->search({list => undef, name => 'name'});
484  
485 # [
486 # { data => ["Example #1"], index => 4, list => undef, name => "name" },
487 # { data => ["Example #2"], index => 5, list => undef, name => "name" },
488 # ]
489  
490 =back
491  
492 =over 4
493  
494 =item search example 2
495  
496 # given: synopsis;
497  
498 my $search = $data->docs->search({list => 'head1', name => 'NAME'});
499  
500 # [
501 # { data => ["Example #1"], index => 1, list => "head1", name => "NAME" },
502 # { data => ["Example #2"], index => 2, list => "head1", name => "NAME" },
503 # ]
504  
505 =back
506  
507 =over 4
508  
509 =item search example 3
510  
511 # given: synopsis;
512  
513 my $find = $data->text->search({list => undef, name => 'name'});
514  
515 # [
516 # { data => ["Example Name"], index => 1, list => undef, name => "name" },
517 # ]
518  
519 =back
520  
521 =over 4
522  
523 =item search example 4
524  
525 # given: synopsis;
526  
527 my $search = $data->text->search({list => 'titles', name => '#1'});
528  
529 # [
530 # { data => ["Example Title #1"], index => 2, list => "titles", name => "#1" },
531 # ]
532  
533 =back
534  
535 =cut
536  
537 =head2 string
538  
539 string(maybe[string] $list, maybe[string] $name) (string)
540  
541 The string method is a wrapper around L</find> as shorthand for searching by
542 C<list> and C<name>, returning only the strings found.
543  
544 I<Since C<1.67>>
545  
546 =over 4
547  
548 =item string example 1
549  
550 # given: synopsis;
551  
552 my $string = $data->docs->string(undef, 'name');
553  
554 # "Example #1\nExample #2"
555  
556 =back
557  
558 =over 4
559  
560 =item string example 2
561  
562 # given: synopsis;
563  
564 my $string = $data->docs->string('head1', 'NAME');
565  
566 # "Example #1\nExample #2"
567  
568 =back
569  
570 =over 4
571  
572 =item string example 3
573  
574 # given: synopsis;
575  
576 my $string = $data->text->string(undef, 'name');
577  
578 # "Example Name"
579  
580 =back
581  
582 =over 4
583  
584 =item string example 4
585  
586 # given: synopsis;
587  
588 my $string = $data->text->string('titles', '#1');
589  
590 # "Example Title #1"
591  
592 =back
593  
594 =over 4
595  
596 =item string example 5
597  
598 # given: synopsis;
599  
600 my @string = $data->docs->string('head1', 'NAME');
601  
602 # ("Example #1", "Example #2")
603  
604 =back
605  
606 =cut
607  
608 =head2 text
609  
610 text() (Venus::Data)
611  
612 The text method configures the instance for parsing DATA blocks.
613  
614 I<Since C<0.01>>
615  
616 =over 4
617  
618 =item text example 1
619  
620 # given: synopsis;
621  
622 my $text = $data->text;
623  
624 # bless({ etag => '@@ end', from => 'data', stag => '@@ ', ... }, "Venus::Data")
625  
626 =back
627  
628 =cut
629  
630 =head1 AUTHORS
631  
632 Awncorp, C<awncorp@cpan.org>
633  
634 =cut
635  
636 =head1 LICENSE
637  
638 Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>.
639  
640 This program is free software, you can redistribute it and/or modify it under
641 the terms of the Apache license version 2.0.
642  
643 =cut
« blib/lib/Venus/Dump.pm ↑ Summary blib/lib/Venus/Gather.pm »