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             our $DATE = '2016-02-19'; # DATE
4             our $VERSION = '0.09'; # VERSION
5              
6 2     2   32932 use 5.010001;
  2         6  
7 2     2   10 use strict;
  2         3  
  2         45  
8 2     2   8 use warnings;
  2         3  
  2         62  
9              
10             use overload
11 2         11 '""' => 'as_string',
12 2     2   1520 ;
  2         1272  
13              
14             sub new {
15 4     4 1 1590 my $class = shift;
16              
17 4         13 my $self = bless {@_}, $class;
18 4         15 $self->empty;
19             $self->{header} //= sub {
20 4     4   4 my ($self, $name, $content, $extra) = @_;
21 4         11 "### $name ###\n";
22 4   100     35 };
23 4         10 $self;
24             }
25              
26             sub empty {
27 4     4 1 6 my $self = shift;
28 4         78 $self->{_toc} = [];
29 4         11 $self->{_content} = '';
30 4         11 $self->{_part_names} = {};
31             }
32              
33             sub header {
34 5     5 1 10 my $self = shift;
35 5 100       14 $self->{header} = $_[0] if @_;
36 5         15 $self->{header};
37             }
38              
39             sub add_part {
40 12     12 1 597 my ($self, $name, $content, $extra) = @_;
41 12 100       43 die "Name cannot be empty" unless length($name);
42 11 100       69 die "Name cannot contain comma/newline" if $name =~ /,|\R/;
43 9 100 100     47 die "Extra cannot contain newline" if defined($extra) && $extra =~ /\R/;
44              
45 8 100       69 die "Duplicate part name '$name'" if $self->{_part_names}{$name}++;
46              
47 7         9 my $header;
48 7 100       20 if (ref($self->{header}) eq 'CODE') {
49 5         14 $header = $self->{header}->($self, $name, $content, $extra);
50             } else {
51 2         3 $header = $self->{header};
52             }
53 7 50       24 $self->{_content} .= $header if defined($header);
54              
55 7         21 push @{ $self->{_toc} }, [
56             $name,
57 7         7 length($self->{_content}),
58             length($content),
59             $extra,
60             ];
61 7         18 $self->{_content} .= $content;
62             }
63              
64             sub as_string {
65 4     4 1 410 my $self = shift;
66              
67             join(
68             "",
69             "Data::Section::Seekable v1\n",
70 7 100       51 (map {"$_->[0],$_->[1],$_->[2]".(defined($_->[3]) ? ",$_->[3]":"")."\n"}
71 4         18 @{ $self->{_toc} }),
72             "\n",
73             $self->{_content},
74 4         7 );
75             }
76              
77             1;
78             # ABSTRACT: Generate data section with multiple parts
79              
80             __END__
81              
82             =pod
83              
84             =encoding UTF-8
85              
86             =head1 NAME
87              
88             Data::Section::Seekable::Writer - Generate data section with multiple parts
89              
90             =head1 VERSION
91              
92             This document describes version 0.09 of Data::Section::Seekable::Writer (from Perl distribution Data-Section-Seekable), released on 2016-02-19.
93              
94             =head1 SYNOPSIS
95              
96             In your script:
97              
98             use Data::Section::Seekable::Writer;
99              
100             my $writer = Data::Section::Seekable::Writer->new;
101              
102             $writer->add_part(part1 => "This is part1\n");
103             $writer->add_part(part2 => This is part\ntwo\n", "very,important");
104             print "__DATA__\n", $writer;
105              
106             will print:
107              
108             __DATA__
109             Data::Section::Seekable v1
110             part1,0,14
111             part2,14,17,very,important
112              
113             This is part1
114             This is part
115             two
116              
117             =head1 DESCRIPTION
118              
119             This class lets you generate data section which can contain multiple part in the
120             format described by L<Data::Section::Seekable>.
121              
122             =head1 METHODS
123              
124             =head2 new(%attrs) => obj
125              
126             Constructor. Attributes:
127              
128             =over
129              
130             =item * header => str|code (default: code to list filename)
131              
132             Header string (or code which should return a string) to add before each part's
133             content. The default is to print:
134              
135             ### <name> ###
136              
137             Code will get these arguments:
138              
139             ($writer, $name, $content, $extra)
140              
141             =back
142              
143             =head2 $writer->add_part($name => $content)
144              
145             =head2 $writer->as_string => str
146              
147             Get the final data section as string. You can also use the object as a string,
148             e.g.:
149              
150             print $writer;
151              
152             because this method is used for stringification overloading.
153              
154             =head2 $writer->header([ $str_or_code ]) => value
155              
156             Get/set header attribute.
157              
158             =head2 $writer->empty
159              
160             Empty content.
161              
162             =head1 HOMEPAGE
163              
164             Please visit the project's homepage at L<https://metacpan.org/release/Data-Section-Seekable>.
165              
166             =head1 SOURCE
167              
168             Source repository is at L<https://github.com/perlancar/perl-Data-Section-Seekable>.
169              
170             =head1 BUGS
171              
172             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Section-Seekable>
173              
174             When submitting a bug or request, please include a test-file or a
175             patch to an existing test-file that illustrates the bug or desired
176             feature.
177              
178             =head1 SEE ALSO
179              
180             L<Data::Section::Seekable> for the description of the data format.
181              
182             L<Data::Section::Seekable::Reader> for the reader class.
183              
184             =head1 AUTHOR
185              
186             perlancar <perlancar@cpan.org>
187              
188             =head1 COPYRIGHT AND LICENSE
189              
190             This software is copyright (c) 2016 by perlancar@cpan.org.
191              
192             This is free software; you can redistribute it and/or modify it under
193             the same terms as the Perl 5 programming language system itself.
194              
195             =cut