File Coverage

blib/lib/Stenciller/Stencil.pm
Criterion Covered Total %
statement 160 175 91.4
branch 36 80 45.0
condition 4 6 66.6
subroutine 22 28 78.5
pod n/a
total 222 289 76.8


line stmt bran cond sub pod time code
1 5     5   23 use Stenciller::Standard;
  10         6062  
  10         3001  
2              
3             our $VERSION = '0.1212'; # VERSION:
4             # ABSTRACT: One part of a file
5             # PODNAME: Stenciller::Stencil
6              
7 10     5   69631 class Stenciller::Stencil using Moose {
  10     5   3103  
  10     5   3039  
  10         3070  
  10         1009  
  5         23  
  5         6  
  5         48  
  5         1403  
  5         8  
  5         33  
  5         267  
  5         8  
  5         297  
  5         26  
  5         6  
  5         438  
  5         140  
  5         24  
  5         9  
  5         59  
  5         15743  
  5         10  
  5         38  
  5         29796  
  5         408698  
  5         60  
  5         72143  
  5         12  
  5         48  
  5         3659  
  5         33529  
  5         60  
  5         3657  
  5         11303  
  5         30  
  5         4576  
  5         10  
  5         53  
  5         238130  
  5         25  
  5         165  
  5         22  
  5         8  
  5         224  
  5         35  
  5         9  
  5         232  
  5         20  
  5         7  
  5         723  
  5         46  
  5         18155  
  5         198  
  5         679  
8              
9 5         228 my @attrs = qw/before_input input between output after_output/;
10              
11 5         10 my $order = 1;
12 5         10 foreach my $attr (@attrs) {
13             has $attr => (
14             is => 'ro',
15             isa => ArrayRef[Str],
16 55         1299 default => sub { [] },
17 25         1174733 traits => ['Array'],
18             init_arg => undef,
19             documentation_order => ++$order,
20             documentation => sprintf ('Holds all lines of the %s section.', $attr),
21             handles => {
22             "has_$attr" => 'count',
23             "add_$attr" => 'push',
24             "all_$attr" => 'elements',
25             "map_$attr" => 'map',
26             "get_$attr" => 'get',
27             },
28             );
29             }
30 5         59673 has skip => (
31             is => 'ro',
32             isa => Bool,
33             default => 0,
34             documentation => 'Should the Stencil not be included in the result?',
35             );
36              
37 5         18097 has line_number => (
38             is => 'ro',
39             isa => Int,
40             documentation => 'Can be referenced in the output for easier backtracking.',
41             );
42             has extra_settings => (
43             is => 'ro',
44             isa => HashRef,
45 9         206 default => sub { { } },
46 5         15557 traits => ['Hash'],
47             documentation => 'Any extra key-value pairs in the stencil header.',
48             handles => {
49             get_extra_setting => 'get',
50             set_extra_setting => 'set',
51             keys_extra_settings => 'keys',
52             },
53             );
54             has loop_values => (
55             is => 'ro',
56             isa => ArrayRef,
57 0         0 default => sub { [] },
58 5         575214 traits => ['Array'],
59             documentation_order => 0,
60             handles => {
61             has_loop_values => 'count',
62             add_loop_value => 'get',
63             all_loop_values => 'elements',
64             },
65             );
66              
67 5 50   5   511261 around BUILDARGS($orig: $class, @args) {
  5 50   11   11  
  5 50       1044  
  5 50       43529  
  11         45  
  11         43  
  31         127  
  31         60  
  31         56  
  31         55  
68 31         61 my %args = @args;
69 31 50       29 $args{'loop_values'} = [] if !defined $args{'loop_values'};
70              
71 31         1058 return $class->$orig(%args);
72             }
73              
74             # Remove all empty lines for each group until we have a line with content, then keep everything
75 5 50   5   9416 around add_before_input($orig: $self, $text) {
  5 50   31   9  
  5 50       759  
  11 50       71  
  29         112  
  29         58  
  29         54  
  29         51  
  29         63  
  29         24  
76 29         1090 return $self->ensure_content($orig, $self->has_before_input, $text);
77             }
78 5 50   5   9147 around add_input($orig: $self, $text) {
  5 50   29   10  
  5 50       797  
  11 50       76  
  29         108  
  29         53  
  29         55  
  29         47  
  29         55  
  29         21  
79 29         902 return $self->ensure_content($orig, $self->has_input, $text);
80             }
81 5 50   5   9252 around add_between($orig: $self, $text) {
  5 50   29   9  
  5 50       973  
  11 50       49  
  29         136  
  29         53  
  29         58  
  29         48  
  29         57  
  29         28  
82 29         1120 return $self->ensure_content($orig, $self->has_between, $text);
83             }
84 5 50   5   9396 around add_output($orig: $self, $text) {
  5 50   29   12  
  5 50       722  
  11 50       28  
  7         38  
  7         22  
  7         16  
  7         19  
  7         16  
  7         7  
85 7         261 return $self->ensure_content($orig, $self->has_output, $text);
86             }
87 5 50   5   9373 around add_after_output($orig: $self, $text) {
  5 50   7   9  
  5 50       765  
  11 50       58  
  125         764  
  125         195  
  125         178  
  125         199  
  125         87  
  125         199  
88 125         172 return $self->ensure_content($orig, $self->has_after_output, $text);
89             }
90 5 50 33 5   59544 method ensure_content(CodeRef $orig, Int $already_have, $text) {
  5 50   5   14  
  5 50   5   673  
  5 50   125   24  
  5 50       7  
  5 50       509  
  5 50       21  
  5 50       8  
  5 50       847  
  11         14  
  125         188  
  125         91  
  125         624  
  125         205  
  125         100  
  125         2684  
  125         1283  
  0            
  0            
  0            
  0            
  0            
91 0 100 100       $self->$orig($text) if $already_have || $text !~ m{^\s*$};
92 0           return $self;
93             }
94              
95 5 0   5   9121 method clone_with_loop_value(Str $loop_value) {
  5 0   5   11  
  5 0   0   635  
  5 0       23  
  5 0       14  
  5         1688  
  11         40  
  0            
  0            
  0            
  0            
  0            
  0            
  0            
96             return Stenciller::Stencil->new(
97       0     before_input => $self->map_before_input( sub { $_ =~ s{ \[ var \] }{$loop_value}x }),
98       0     input => $self->map_input( sub { $_ =~ s{ \[ var \] }{$loop_value}x }),
99       0     between => $self->map_between( sub { $_ =~ s{ \[ var \] }{$loop_value}x }),
100       0     output => $self->map_output( sub { $_ =~ s{ \[ var \] }{$loop_value}x }),
101       0     after => $self->map_after( sub { $_ =~ s{ \[ var \] }{$loop_value}x }),
102             (map { $_ => $self->$_ } qw/line_number extra_settings/)
103             );
104             }
105              
106             }
107              
108             1;
109              
110             __END__
111              
112             =pod
113              
114             =encoding UTF-8
115              
116             =head1 NAME
117              
118             Stenciller::Stencil - One part of a file
119              
120             =head1 VERSION
121              
122             Version 0.1212, released 2015-02-10.
123              
124              
125              
126             =head1 SYNOPSIS
127              
128             # In a plugin (this is pretty similar to what ToUnparsedText does)
129             sub render {
130             my $self = shift;
131             my @out = ();
132              
133             STENCIL:
134             foreach my $stencil ($self->stenciller->all_stencils) {
135             push @out => join "\n" => $stencil->all_before_input;
136             push @out => join "\n" => $stencil->all_input;
137             push @out => join "\n" => $stencil->all_between;
138             push @out => join "\n" => $stencil->all_output;
139             push @out => join "\n" => $stencil->all_after_output;
140             }
141             return join "\n" => @out;
142             }
143              
144             =head1 DESCRIPTION
145              
146             A C<Stencil> is one section of the file format defined in L<Stenciller>.
147              
148             =head1 ATTRIBUTES
149              
150              
151             =head2 before_input
152              
153             =begin HTML
154              
155             <table cellpadding="0" cellspacing="0">
156             <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>
157             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">not in constructor</td>
158             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
159             </table>
160              
161             <p>Holds all lines of the before_input section.</p>
162              
163             =end HTML
164              
165             =head2 input
166              
167             =begin HTML
168              
169             <table cellpadding="0" cellspacing="0">
170             <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>
171             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">not in constructor</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 input section.</p>
176              
177             =end HTML
178              
179             =head2 between
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;">not in constructor</td>
186             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
187             </table>
188              
189             <p>Holds all lines of the between section.</p>
190              
191             =end HTML
192              
193             =head2 output
194              
195             =begin HTML
196              
197             <table cellpadding="0" cellspacing="0">
198             <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>
199             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">not in constructor</td>
200             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
201             </table>
202              
203             <p>Holds all lines of the output section.</p>
204              
205             =end HTML
206              
207             =head2 after_output
208              
209             =begin HTML
210              
211             <table cellpadding="0" cellspacing="0">
212             <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>
213             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">not in constructor</td>
214             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
215             </table>
216              
217             <p>Holds all lines of the after_output section.</p>
218              
219             =end HTML
220              
221             =head2 extra_settings
222              
223             =begin HTML
224              
225             <table cellpadding="0" cellspacing="0">
226             <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>
227              
228             </td>
229             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default is a <code>coderef</code>
230              
231             </td>
232             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
233             </table>
234              
235             <p>Any extra key-value pairs in the stencil header.</p>
236              
237             =end HTML
238              
239             =head2 line_number
240              
241             =begin HTML
242              
243             <table cellpadding="0" cellspacing="0">
244             <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>
245              
246             </td>
247             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional</td>
248             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
249             </table>
250              
251             <p>Can be referenced in the output for easier backtracking.</p>
252              
253             =end HTML
254              
255             =head2 skip
256              
257             =begin HTML
258              
259             <table cellpadding="0" cellspacing="0">
260             <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>
261              
262             </td>
263             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default: <code>0</code>
264              
265             </td>
266             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
267             </table>
268              
269             <p>Should the Stencil not be included in the result?</p>
270              
271             =end HTML
272              
273             =head1 SOURCE
274              
275             L<https://github.com/Csson/p5-Stenciller>
276              
277             =head1 HOMEPAGE
278              
279             L<https://metacpan.org/release/Stenciller>
280              
281             =head1 AUTHOR
282              
283             Erik Carlsson <info@code301.com>
284              
285             =head1 COPYRIGHT AND LICENSE
286              
287             This software is copyright (c) 2015 by Erik Carlsson <info@code301.com>.
288              
289             This is free software; you can redistribute it and/or modify it under
290             the same terms as the Perl 5 programming language system itself.
291              
292             =cut