Stenciller/Stencil.pm

filter: Statementstmt 97.9% 191 / 195 Branchbran 52.5% 42 / 80 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 88.2% 30 / 34 Podpod n/a totaltotal 85.3% 268 / 314 SCAR 39.0
CC
43
Cov
85%
ensure_content
12.9
clone_with_loop_value
6.5
:around
6.2
CRAP 49.4
« Stenciller/Wrap.pm ↑ Summary Stenciller/Transformer.pm »
1 7 ▶use Stenciller::Standard;
Subroutine __ANON__: called CC=1 SCAR=0.0
2 7 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
4  
5 # PODNAME: Stenciller::Stencil
6 7  
7 7 ▶class Stenciller::Stencil using Moose {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
8  
9 7 our $VERSION = '0.1302'; # VERSION:
10 # ABSTRACT: One part of a file
11  
12 7 my @attrs = qw/before_input input between output after_output/;
13  
14 7 my $order = 1;
15 7 foreach my $attr (@attrs) {
16 has $attr => (
17 is => 'ro',
18 isa => ArrayRef[Str],
19 75 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 35 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 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 has line_number => (
41 is => 'ro',
42 isa => Int,
43 documentation => 'Can be referenced in the output for easier backtracking.',
44 );
45 7 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 ▶ default => sub { { } },
Subroutine __ANON__: called CC=1 SCAR=0.0
55 7 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 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 7 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 ▶ around BUILDARGS($orig: $class, @args) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine :around: called CC=6 SCAR=18.2
Branch: 4 branches
BranchTrueFalseTotal
@_ ? : 17 0 17
if @_ < 1 0 17 17
$#_ >= 0 ? : 17 0 17
$#_ >= 1 ? : 17 0 17
77 17 my %args = @args;
78 17 ▶ $args{'loop_values'} = [] if !defined $args{'loop_values'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"loop_values"} 16 1 17
79  
80 17 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 ▶ around add_before_input($orig: $self, $text) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine :around: called CC=5 SCAR=16.6
Branch: 4 branches
BranchTrueFalseTotal
@_ ? : 41 0 41
if @_ != 2 0 41 41
$#_ >= 0 ? : 41 0 41
$#_ >= 1 ? : 41 0 41
85 41 return $self->ensure_content($orig, $self->has_before_input, $text);
86 }
87 7 ▶ around add_input($orig: $self, $text) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine :around: called CC=5 SCAR=16.6
Branch: 4 branches
BranchTrueFalseTotal
@_ ? : 41 0 41
if @_ != 2 0 41 41
$#_ >= 0 ? : 41 0 41
$#_ >= 1 ? : 41 0 41
88 41 return $self->ensure_content($orig, $self->has_input, $text);
89 }
90 7 ▶ around add_between($orig: $self, $text) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine :around: called CC=5 SCAR=16.6
Branch: 4 branches
BranchTrueFalseTotal
@_ ? : 38 0 38
if @_ != 2 0 38 38
$#_ >= 0 ? : 38 0 38
$#_ >= 1 ? : 38 0 38
91 38 return $self->ensure_content($orig, $self->has_between, $text);
92 }
93 7 ▶ around add_output($orig: $self, $text) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine :around: called CC=5 SCAR=16.6
Branch: 4 branches
BranchTrueFalseTotal
@_ ? : 41 0 41
if @_ != 2 0 41 41
$#_ >= 0 ? : 41 0 41
$#_ >= 1 ? : 41 0 41
94 41 return $self->ensure_content($orig, $self->has_output, $text);
95 }
96 7 ▶ around add_after_output($orig: $self, $text) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine :around: called CC=5 SCAR=16.6
Branch: 4 branches
BranchTrueFalseTotal
@_ ? : 11 0 11
if @_ != 2 0 11 11
$#_ >= 0 ? : 11 0 11
$#_ >= 1 ? : 11 0 11
97 11 return $self->ensure_content($orig, $self->has_after_output, $text);
98 }
99 7 ▶ method ensure_content(CodeRef $orig, Int $already_have, $text) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine ensure_content: called CC=12 SCAR=25.6
Branch: 9 branches
BranchTrueFalseTotal
@_ ? : 172 0 172
if @_ != 3 0 172 172
$#_ >= 0 ? : 172 0 172
if ($#_ >= 0) 172 0 172
Ref::Util::XS::is_plain_coderef($orig) or _failed_check(17, "CodeRef", $orig, "exception_class", "Error::TypeTiny::Assertion") 0 172 172
$#_ >= 1 ? : 172 0 172
if ($#_ >= 1) 172 0 172
Type::Tiny::XS::Int($already_have) or _failed_check(13, "Int", $already_have, "exception_class", "Error::TypeTiny::Assertion") 0 172 172
$#_ >= 2 ? : 172 0 172
100 172 ▶ $self->$orig($text) if $already_have || $text !~ m{^\s*$};
Branch: 1 branch
BranchTrueFalseTotal
if $already_have or not $text =~ /^\s*$/u 107 65 172
Condition100%3/3
$already_have or not $text =~ /^\s*$/u
l!l&&r!l&&!r
565165
MC/DC100%2/2
$already_have or not $text =~ /^\s*$/u
$already_have not $text =~ /^\s*$/u
Truth table100%3/3
A or not(B)
A= $already_haveB= not $text =~ /^\s*$/u
ABresult
1X1
011
000
101 172 return $self;
102 }
103  
104 7 ▶ method clone_with_loop_value(Str $loop_value) {
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine clone_with_loop_value: called CC=6 SCAR=18.7
Branch: 5 branches
BranchTrueFalseTotal
@_ ? : 2 0 2
if @_ != 1 0 2 2
$#_ >= 0 ? : 2 0 2
if ($#_ >= 0) 2 0 2
Type::Tiny::XS::Str($loop_value) or _failed_check(9, "Str", $loop_value, "exception_class", "Error::TypeTiny::Assertion") 0 2 2
105 my $clone = Stenciller::Stencil->new(
106 0 ▶ before_input => [$self->map_before_input( sub { $_ =~ s{ \[ var \] }{$loop_value}rx })],
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 2 ▶ input => [$self->map_input( sub { $_ =~ s{ \[ var \] }{$loop_value}rx })],
Subroutine __ANON__: called CC=1 SCAR=0.0
108 0 ▶ between => [$self->map_between( sub { $_ =~ s{ \[ var \] }{$loop_value}rx })],
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 2 ▶ output => [$self->map_output( sub { $_ =~ s{ \[ var \] }{$loop_value}rx })],
Subroutine __ANON__: called CC=1 SCAR=0.0
110 0 ▶ after_output => [$self->map_after_output( sub { $_ =~ s{ \[ var \] }{$loop_value}rx })],
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 stencil_name => $self->stencil_name . "_$loop_value",
112 2 (map { $_ => $self->$_ } qw/line_number extra_settings/)
113 );
114  
115 2 return $clone;
116 }
117  
118 }
119  
120 7 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
« Stenciller/Wrap.pm ↑ Summary Stenciller/Transformer.pm »