File Coverage

blib/lib/Data/Section/Seekable/Writer.pm
Criterion Covered Total %
statement 42 42 100.0
branch 15 16 93.7
condition 5 5 100.0
subroutine 10 10 100.0
pod 5 5 100.0
total 77 78 98.7


line stmt bran cond sub pod time code
1             package Data::Section::Seekable::Writer;
2              
3 2     2   73113 use 5.010001;
  2         20  
4 2     2   10 use strict;
  2         5  
  2         36  
5 2     2   10 use warnings;
  2         4  
  2         54  
6              
7             use overload
8 2         15 '""' => 'as_string',
9 2     2   1241 ;
  2         980  
10              
11             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12             our $DATE = '2023-01-20'; # DATE
13             our $DIST = 'Data-Section-Seekable'; # DIST
14             our $VERSION = '0.091'; # VERSION
15              
16             sub new {
17 4     4 1 1487 my $class = shift;
18              
19 4         14 my $self = bless {@_}, $class;
20 4         15 $self->empty;
21             $self->{header} //= sub {
22 4     4   13 my ($self, $name, $content, $extra) = @_;
23 4         12 "### $name ###\n";
24 4   100     39 };
25 4         10 $self;
26             }
27              
28             sub empty {
29 4     4 1 28 my $self = shift;
30 4         69 $self->{_toc} = [];
31 4         9 $self->{_content} = '';
32 4         9 $self->{_part_names} = {};
33             }
34              
35             sub header {
36 5     5 1 11 my $self = shift;
37 5 100       15 $self->{header} = $_[0] if @_;
38 5         19 $self->{header};
39             }
40              
41             sub add_part {
42 12     12 1 482 my ($self, $name, $content, $extra) = @_;
43 12 100       41 die "Name cannot be empty" unless length($name);
44 11 100       64 die "Name cannot contain comma/newline" if $name =~ /,|\R/;
45 9 100 100     39 die "Extra cannot contain newline" if defined($extra) && $extra =~ /\R/;
46              
47 8 100       34 die "Duplicate part name '$name'" if $self->{_part_names}{$name}++;
48              
49 7         14 my $header;
50 7 100       20 if (ref($self->{header}) eq 'CODE') {
51 5         25 $header = $self->{header}->($self, $name, $content, $extra);
52             } else {
53 2         5 $header = $self->{header};
54             }
55 7 50       28 $self->{_content} .= $header if defined($header);
56              
57 7         22 push @{ $self->{_toc} }, [
58             $name,
59 7         11 length($self->{_content}),
60             length($content),
61             $extra,
62             ];
63 7         20 $self->{_content} .= $content;
64             }
65              
66             sub as_string {
67 4     4 1 300 my $self = shift;
68              
69             join(
70             "",
71             "Data::Section::Seekable v1\n",
72 7 100       56 (map {"$_->[0],$_->[1],$_->[2]".(defined($_->[3]) ? ",$_->[3]":"")."\n"}
73 4         16 @{ $self->{_toc} }),
74             "\n",
75             $self->{_content},
76 4         10 );
77             }
78              
79             1;
80             # ABSTRACT: Generate data section with multiple parts
81              
82             __END__