File Coverage

blib/lib/Stenciller/Transformer.pm
Criterion Covered Total %
statement 42 42 100.0
branch 7 8 87.5
condition 6 9 66.6
subroutine 11 11 100.0
pod 0 3 0.0
total 66 73 90.4


line stmt bran cond sub pod time code
1 6     6   3416 use 5.10.1;
  6         19  
2 6     6   33 use strict;
  6         10  
  6         121  
3 6     6   25 use warnings;
  6         12  
  6         282  
4              
5             package Stenciller::Transformer;
6              
7             our $VERSION = '0.1400'; # VERSION:
8             # ABSTRACT: A role for transformer plugins to consume
9              
10 6     6   33 use Moose::Role;
  6         10  
  6         48  
11 6     6   28441 use Types::Standard qw/HashRef/;
  6         16  
  6         76  
12 6     6   3270 use Types::Stenciller qw/Stenciller/;
  6         13  
  6         54  
13 6     6   2205 use List::MoreUtils qw/first_index/;
  6         15  
  6         46  
14              
15             requires 'transform';
16              
17             has stenciller => (
18             is => 'ro',
19             isa => Stenciller,
20             required => 1,
21             );
22              
23             sub init_out {
24 7     7 0 16 my $self = shift;
25 7         12 my $stenciller = shift;
26 7   50     32 my $transform_args = shift || {};
27              
28 7         17 my @out = ('');
29 7 50       222 push (@out => $stenciller->all_header_lines, '') if !$transform_args->{'skip_header_lines'};
30 7         30 return @out;
31             }
32              
33             sub should_skip_stencil_by_index {
34 17     17 0 27 my $self = shift;
35 17         24 my $index = shift;
36 17   50     35 my $transform_args = shift || {};
37 17 100 100 17   78 return 1 if exists $transform_args->{'stencils'} && -1 == first_index { $_ == $index } @{ $transform_args->{'stencils'} };
  17         57  
  12         37  
38 10         34 return 0;
39             }
40              
41             sub should_skip_stencil {
42 10     10 0 52 my $self = shift;
43 10         28 my $stencil = shift;
44 10   50     38 my $transform_args = shift || {};
45              
46 10 100       39 return 0 if !exists $transform_args->{'require_in_extra'};
47 3         6 my $wanted_key = $transform_args->{'require_in_extra'}{'key'};
48 3         6 my $required_value = $transform_args->{'require_in_extra'}{'value'};
49 3         5 my $default_value = $transform_args->{'require_in_extra'}{'default'};
50              
51 3 100       91 return !$default_value if !defined $stencil->get_extra_setting($wanted_key);
52 2         68 return !$stencil->get_extra_setting($wanted_key);
53             }
54              
55             1;
56              
57             __END__
58              
59             =pod
60              
61             =encoding UTF-8
62              
63             =head1 NAME
64              
65             Stenciller::Transformer - A role for transformer plugins to consume
66              
67             =head1 VERSION
68              
69             Version 0.1400, released 2016-02-03.
70              
71              
72              
73             =head1 SYNOPSIS
74              
75             package Stenciller::Plugin::MyNewRenderer;
76              
77             use Moose;
78             with 'Stenciller::Transformer';
79              
80             sub transformer {
81             ...
82             }
83              
84             =head1 DESCRIPTION
85              
86             This is the role that all L<Stenciller> plugins must consume. It requires a C<transformer> method to be implemented.
87              
88             =head1 METHODS
89              
90             =head2 transform
91              
92             This method must be implemented by classes consuming this role.
93              
94             It takes one attribute:
95              
96             B<C<$transform_args>>
97              
98             C<$transform_args> is a hash reference with the following structure:
99              
100             $transform_args => {
101             skip_header_lines => 0|1,
102             stencils => [...],
103             require_in_extra => {
104             key => '...',
105             value => '...',
106             default => '...',
107             },
108             }
109              
110             B<C<skip_header_lines =E<gt> 1>>
111              
112             C<skip_header_lines> takes a boolean indicating if the L<Stenciller's|Stenciller> header_lines should be skipped. Default is C<0>.
113              
114             B<C<stencils =E<gt> [ ]>>
115              
116             C<stencils> takes an array reference of which stencils in the currently parsed file that should be included in the output. The index is zero based. If C<stencils> is not given, all stencils are parsed.
117              
118             B<C<require_in_extra =E<gt> { }>>
119              
120             C<require_in_extra> allows finer filtering than C<stencils>. Usually, the point to using Stenciller, and
121             related modules, is to use the same content more than once (eg. include it in pod, create html files with
122             examples, and create tests). It is not always necessary to include every stencil in every end product.
123              
124             If C<require_in_extra> is given, it looks in the options hash for every stencil for the C<key> key.
125              
126             =over 4
127              
128             =item *
129              
130             If C<key> exists in the stencil's hash, and it has the C<value> value, then the stencil is parsed.
131              
132             =item *
133              
134             If C<key> exists in the stencil's hash, and it doesn't have the C<value> value, then the stencil is not parsed.
135              
136             =item *
137              
138             If C<key> doesn't exist in the stencil's hash, then the two first rules are applied as if the stencil had the C<default> value.
139              
140             =back
141              
142             =head1 ATTRIBUTES
143              
144             =head2 stenciller
145              
146             The L<Stenciller> object is passed automatically to plugins.
147              
148             =head1 SOURCE
149              
150             L<https://github.com/Csson/p5-Stenciller>
151              
152             =head1 HOMEPAGE
153              
154             L<https://metacpan.org/release/Stenciller>
155              
156             =head1 AUTHOR
157              
158             Erik Carlsson <info@code301.com>
159              
160             =head1 COPYRIGHT AND LICENSE
161              
162             This software is copyright (c) 2016 by Erik Carlsson.
163              
164             This is free software; you can redistribute it and/or modify it under
165             the same terms as the Perl 5 programming language system itself.
166              
167             =cut