File Coverage

blib/lib/Stenciller/Stencil.pm
Criterion Covered Total %
statement 179 183 97.8
branch 42 80 52.5
condition 4 6 66.6
subroutine 27 30 90.0
pod n/a
total 252 299 84.2


line stmt bran cond sub pod time code
1 7     7   35 use Stenciller::Standard;
  14         9343  
  14         5580  
2 14     7   17064 use strict;
  14         5378  
  14         5332  
3 14     7   5390 use warnings;
  14         1377  
  7         347  
4              
5             # PODNAME: Stenciller::Stencil
6              
7 7     7   109083 class Stenciller::Stencil using Moose {
  7     7   224  
  7     7   51  
  7         13  
  7         410  
  7         39  
  7         10  
  7         76  
  7         2144  
  7         15  
  7         57  
  7         467  
  7         15  
  7         386  
  7         35  
  7         13  
  7         727  
  7         286  
  7         39  
  7         20  
  7         51  
  7         28986  
  7         14  
  7         62  
  7         51058  
  7         786503  
  7         55  
  7         130991  
  7         20  
  7         90  
  7         6870  
  7         66983  
  7         85  
  7         7388  
  7         22081  
  7         52  
  7         8881  
  7         14  
  7         60  
  7         435552  
  7         29  
  7         40  
  7         12  
  7         188  
  7         33  
  7         19  
  7         307  
  7         37  
  7         14  
  7         1138  
  7         71  
  7         37666  
  7         187  
  7         1233  
  17         83  
8              
9 7         403 our $VERSION = '0.1302'; # VERSION:
10             # ABSTRACT: One part of a file
11              
12 7         57 my @attrs = qw/before_input input between output after_output/;
13              
14 7         16 my $order = 1;
15 7         18 foreach my $attr (@attrs) {
16             has $attr => (
17             is => 'ro',
18             isa => ArrayRef[Str],
19 75         2458 default => sub { [] },
20 35         2094562 traits => ['Array'],
21             #init_arg => undef,
22             documentation_order => ++$order,
23             documentation => sprintf ('Holds all lines of the %s section.', $attr),
24             handles => {
25             "has_$attr" => 'count',
26             "add_$attr" => 'push',
27             "all_$attr" => 'elements',
28             "map_$attr" => 'map',
29             "get_$attr" => 'get',
30             "count_$attr" => 'count',
31             },
32             );
33             }
34 7         119517 has skip => (
35             is => 'ro',
36             isa => Bool,
37             default => 0,
38             documentation => 'Should the Stencil not be included in the result?',
39             );
40 7         32363 has line_number => (
41             is => 'ro',
42             isa => Int,
43             documentation => 'Can be referenced in the output for easier backtracking.',
44             );
45 7         29081 has stencil_name => (
46             is => 'ro',
47             isa => Str,
48             documentation => q{Can be given in the stencil hash with 'name'. Depends on used plugins if it is necessary/useful.},
49             documentation_default => '[filename]_[linenumber]',
50             );
51             has extra_settings => (
52             is => 'ro',
53             isa => HashRef,
54 13         429 default => sub { { } },
55 7         33736 traits => ['Hash'],
56             documentation => 'Any extra key-value pairs in the stencil header.',
57             handles => {
58             get_extra_setting => 'get',
59             set_extra_setting => 'set',
60             keys_extra_settings => 'keys',
61             },
62             );
63             has loop_values => (
64             is => 'ro',
65             isa => ArrayRef,
66 0         0 default => sub { [] },
67 7         1020053 traits => ['Array'],
68             documentation_order => 0,
69             handles => {
70             has_loop_values => 'count',
71             add_loop_value => 'get',
72             all_loop_values => 'elements',
73             },
74             );
75              
76 7 50   7   912604 around BUILDARGS($orig: $class, @args) {
  7 50   17   17  
  7 50       1867  
  7 50       76514  
  17         91  
  41         499  
  41         113  
  41         106  
  41         103  
  41         105  
77 41         54 my %args = @args;
78 41 100       1858 $args{'loop_values'} = [] if !defined $args{'loop_values'};
79              
80 41         488 return $class->$orig(%args);
81             }
82              
83             # Remove all empty lines for each group until we have a line with content, then keep everything
84 7 50   7   17342 around add_before_input($orig: $self, $text) {
  7 50   41   20  
  7 50       1369  
  17 50       262  
  41         107  
  41         102  
  41         98  
  41         101  
  41         63  
  41         1760  
85 38         437 return $self->ensure_content($orig, $self->has_before_input, $text);
86             }
87 7 50   7   16854 around add_input($orig: $self, $text) {
  7 50   41   21  
  7 50       1373  
  17 50       59  
  38         106  
  38         97  
  38         96  
  38         103  
  38         49  
  38         1586  
88 41         465 return $self->ensure_content($orig, $self->has_input, $text);
89             }
90 7 50   7   17326 around add_between($orig: $self, $text) {
  7 50   38   17  
  7 50       1355  
  17 50       63  
  41         107  
  41         104  
  41         100  
  41         106  
  41         63  
  41         1790  
91 11         134 return $self->ensure_content($orig, $self->has_between, $text);
92             }
93 7 50   7   17041 around add_output($orig: $self, $text) {
  7 50   41   17  
  7 50       1394  
  17 50       49  
  11         31  
  11         32  
  11         27  
  11         32  
  11         17  
  11         470  
94 172         1405 return $self->ensure_content($orig, $self->has_output, $text);
95             }
96 7 50   7   17608 around add_after_output($orig: $self, $text) {
  7 50   11   16  
  7 50       1433  
  17 50       96  
  172         430  
  172         368  
  172         407  
  172         186  
  172         448  
  172         370  
97 172         382 return $self->ensure_content($orig, $self->has_after_output, $text);
98             }
99 7 50 33 7   112007 method ensure_content(CodeRef $orig, Int $already_have, $text) {
  7 50   7   19  
  7 50   7   1197  
  7 50   172   38  
  7 50       15  
  7 50       995  
  7 50       40  
  7 50       14  
  7 50       1367  
  17         37  
  172         215  
  172         1108  
  172         428  
  172         210  
  172         5120  
  172         2372  
  2         6  
  2         7  
  2         10  
  2         16  
  2         4  
  2         7  
100 2 100 100     4 $self->$orig($text) if $already_have || $text !~ m{^\s*$};
101 2         88 return $self;
102             }
103              
104 7 50   7   16498 method clone_with_loop_value(Str $loop_value) {
  7 50   7   17  
  7 50   2   1187  
  7 50       38  
  7 50       13  
  7         3184  
  17         76  
  0         0  
  2         208  
  0         0  
  2         202  
  0         0  
  4         220  
  2         19  
105             my $clone = Stenciller::Stencil->new(
106       0     before_input => [$self->map_before_input( sub { $_ =~ s{ \[ var \] }{$loop_value}rx })],
107       2     input => [$self->map_input( sub { $_ =~ s{ \[ var \] }{$loop_value}rx })],
108       0     between => [$self->map_between( sub { $_ =~ s{ \[ var \] }{$loop_value}rx })],
109       2     output => [$self->map_output( sub { $_ =~ s{ \[ var \] }{$loop_value}rx })],
110       0     after_output => [$self->map_after_output( sub { $_ =~ s{ \[ var \] }{$loop_value}rx })],
111             stencil_name => $self->stencil_name . "_$loop_value",
112             (map { $_ => $self->$_ } qw/line_number extra_settings/)
113             );
114              
115             return $clone;
116             }
117              
118             }
119              
120             1;
121              
122             __END__
123              
124             =pod
125              
126             =encoding UTF-8
127              
128             =head1 NAME
129              
130             Stenciller::Stencil - One part of a file
131              
132             =head1 VERSION
133              
134             Version 0.1302, released 2015-11-28.
135              
136              
137              
138             =head1 SYNOPSIS
139              
140             # In a plugin (this is pretty similar to what ToUnparsedText does)
141             sub render {
142             my $self = shift;
143             my @out = ();
144              
145             STENCIL:
146             foreach my $stencil ($self->stenciller->all_stencils) {
147             push @out => join "\n" => $stencil->all_before_input;
148             push @out => join "\n" => $stencil->all_input;
149             push @out => join "\n" => $stencil->all_between;
150             push @out => join "\n" => $stencil->all_output;
151             push @out => join "\n" => $stencil->all_after_output;
152             }
153             return join "\n" => @out;
154             }
155              
156             =head1 DESCRIPTION
157              
158             A C<Stencil> is one section of the file format defined in L<Stenciller>.
159              
160             =head1 ATTRIBUTES
161              
162              
163             =head2 before_input
164              
165             =begin HTML
166              
167             <table cellpadding="0" cellspacing="0">
168             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Types::Standard#Str">Str</a> ]</td>
169             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default is a <code>coderef</code>
170              
171             </td>
172             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
173             </table>
174              
175             <p>Holds all lines of the before_input section.</p>
176              
177             =end HTML
178              
179             =head2 input
180              
181             =begin HTML
182              
183             <table cellpadding="0" cellspacing="0">
184             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Types::Standard#Str">Str</a> ]</td>
185             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default is a <code>coderef</code>
186              
187             </td>
188             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
189             </table>
190              
191             <p>Holds all lines of the input section.</p>
192              
193             =end HTML
194              
195             =head2 between
196              
197             =begin HTML
198              
199             <table cellpadding="0" cellspacing="0">
200             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Types::Standard#Str">Str</a> ]</td>
201             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default is a <code>coderef</code>
202              
203             </td>
204             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
205             </table>
206              
207             <p>Holds all lines of the between section.</p>
208              
209             =end HTML
210              
211             =head2 output
212              
213             =begin HTML
214              
215             <table cellpadding="0" cellspacing="0">
216             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Types::Standard#Str">Str</a> ]</td>
217             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default is a <code>coderef</code>
218              
219             </td>
220             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
221             </table>
222              
223             <p>Holds all lines of the output section.</p>
224              
225             =end HTML
226              
227             =head2 after_output
228              
229             =begin HTML
230              
231             <table cellpadding="0" cellspacing="0">
232             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#ArrayRef">ArrayRef</a> [ <a href="https://metacpan.org/pod/Types::Standard#Str">Str</a> ]</td>
233             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default is a <code>coderef</code>
234              
235             </td>
236             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
237             </table>
238              
239             <p>Holds all lines of the after_output section.</p>
240              
241             =end HTML
242              
243             =head2 extra_settings
244              
245             =begin HTML
246              
247             <table cellpadding="0" cellspacing="0">
248             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#HashRef">HashRef</a>
249              
250             </td>
251             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default is a <code>coderef</code>
252              
253             </td>
254             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
255             </table>
256              
257             <p>Any extra key-value pairs in the stencil header.</p>
258              
259             =end HTML
260              
261             =head2 line_number
262              
263             =begin HTML
264              
265             <table cellpadding="0" cellspacing="0">
266             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Int">Int</a>
267              
268             </td>
269             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional</td>
270             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
271             </table>
272              
273             <p>Can be referenced in the output for easier backtracking.</p>
274              
275             =end HTML
276              
277             =head2 skip
278              
279             =begin HTML
280              
281             <table cellpadding="0" cellspacing="0">
282             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Bool">Bool</a>
283              
284             </td>
285             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default: <code>0</code>
286              
287             </td>
288             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
289             </table>
290              
291             <p>Should the Stencil not be included in the result?</p>
292              
293             =end HTML
294              
295             =head2 stencil_name
296              
297             =begin HTML
298              
299             <table cellpadding="0" cellspacing="0">
300             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Str">Str</a>
301              
302             </td>
303             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional</td>
304             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
305             </table>
306              
307             <p>Can be given in the stencil hash with 'name'. Depends on used plugins if it is necessary/useful.</p>
308              
309             =end HTML
310              
311             =head1 SOURCE
312              
313             L<https://github.com/Csson/p5-Stenciller>
314              
315             =head1 HOMEPAGE
316              
317             L<https://metacpan.org/release/Stenciller>
318              
319             =head1 AUTHOR
320              
321             Erik Carlsson <info@code301.com>
322              
323             =head1 COPYRIGHT AND LICENSE
324              
325             This software is copyright (c) 2015 by Erik Carlsson.
326              
327             This is free software; you can redistribute it and/or modify it under
328             the same terms as the Perl 5 programming language system itself.
329              
330             =cut