File Coverage

blib/lib/Dist/Zilla/Plugin/MetaResources/Template.pm
Criterion Covered Total %
statement 24 26 92.3
branch 8 10 80.0
condition n/a
subroutine 5 5 100.0
pod 1 1 100.0
total 38 42 90.4


line stmt bran cond sub pod time code
1             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
2             #
3             # file: lib/Dist/Zilla/Plugin/MetaResources/Template.pm
4             #
5             # Copyright © 2015 Van de Bugger
6             #
7             # This file is part of perl-Dist-Zilla-Plugin-MetaResources-Template.
8             #
9             # perl-Dist-Zilla-Plugin-MetaResources-Template is free software: you can redistribute it and/or
10             # modify it under the terms of the GNU General Public License as published by the Free Software
11             # Foundation, either version 3 of the License, or (at your option) any later version.
12             #
13             # perl-Dist-Zilla-Plugin-MetaResources-Template is distributed in the hope that it will be
14             # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15             # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16             #
17             # You should have received a copy of the GNU General Public License along with
18             # perl-Dist-Zilla-Plugin-MetaResources-Template. If not, see <http://www.gnu.org/licenses/>.
19             #
20             # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
21              
22             #pod =for :this This is C<Dist::Zilla::Plugin::MetaResources::Template> module documentation. Read this if you are going to hack or
23             #pod extend C<Dist-Zilla-Plugin-MetaResources-Template>.
24             #pod
25             #pod =for :that If you want to use Perl code in distribution "resource" metadata, read the L<manual|Dist::Zilla::Plugin::MetaResources::Template::Manual>. General
26             #pod topics like getting source, building, installing, bug reporting and some others are covered in the
27             #pod F<README>.
28             #pod
29             #pod =head1 SYNOPSIS
30             #pod
31             #pod Oops.
32             #pod
33             #pod =head1 DESCRIPTION
34             #pod
35             #pod C<Dist::Zilla::Plugin::MetaResources::Template> extends C<Dist::Zilla::Plugin::MetaResources>. The class implements C<BUILD>
36             #pod method, which expand templates in option values, all other work is done by the parent class.
37             #pod
38             #pod Template processing abilities achieved by consuming C<Dist::Zilla::Role::TextTemplater> role.
39             #pod
40             #pod =head1 SEE ALSO
41             #pod
42             #pod =for :list
43             #pod = L<Dist::Zilla>
44             #pod = L<Dist::Zilla::Role>
45             #pod = L<Dist::Zilla::Role::Plugin>
46             #pod = L<Dist::Zilla::Role::TextTemplater>
47             #pod = L<Dist::Zilla::Plugin::MetaResources>
48             #pod = L<Dist::Zilla::Plugin::MetaResources::Template::Manual>
49             #pod = L<CPAN::Meta::Spec>
50             #pod = L<CPAN::Meta::Spec/"resources">
51             #pod = L<Moose::Manual::Construction/"BUILD">
52             #pod
53             #pod =cut
54              
55             # --------------------------------------------------------------------------------------------------
56              
57             package Dist::Zilla::Plugin::MetaResources::Template;
58              
59 1     1   2326425 use Moose;
  1         4  
  1         7  
60 1     1   6609 use namespace::autoclean;
  1         2  
  1         11  
61 1     1   97 use version 0.77;
  1         33  
  1         8  
62              
63             # ABSTRACT: Use Perl code in distribution "resource" metadata
64             our $VERSION = 'v0.4.5'; # VERSION
65              
66             extends 'Dist::Zilla::Plugin::MetaResources';
67             with 'Dist::Zilla::Role::TextTemplater' => { -version => 'v0.4.0' };
68             # ^ `TextTemplater` v0.4.0 supports `filename` option.
69              
70             # --------------------------------------------------------------------------------------------------
71              
72             #pod =Method BUILDARGS
73             #pod
74             #pod Parent's C<BUILDARGS> mangles all the arguments: it moves them into resources, including
75             #pod C<TextTemplater> arguments: C<delimiters>, C<package>, C<prepend>. We have to protect
76             #pod C<TextTemplater> arguments from being misinterpreted.
77             #pod
78             #pod =cut
79              
80             around BUILDARGS => sub {
81             my ( $orig, $class, $args ) = @_;
82             my $save = {};
83             for my $attr ( qw{ delimiters package prepend } ) {
84             if ( exists( $args->{ $attr } ) ) {
85             $save->{ $attr } = delete( $args->{ $attr } );
86             };
87             };
88             $args = $class->$orig( $args );
89             for my $attr ( keys( %$save ) ) {
90             $args->{ $attr } = $save->{ $attr };
91             };
92             return $args;
93             };
94              
95             # --------------------------------------------------------------------------------------------------
96              
97             #pod =method BUILD
98             #pod
99             #pod The method is automatically called after object creation. The method recursively walks through the
100             #pod C<< $self->{resources} >> and expand templates in string by calling C<< $self->fill_in_string >>
101             #pod (it is a method from C<TextTemplater> role).
102             #pod
103             #pod Defining C<BUILDARGS> seems like a simpler approach because all the options are in plain list and
104             #pod so there is no need in recursive walking trough a complex data structure (C<< $self->{ resources }
105             #pod >>), but we need C<$self> to perform template expansion while C<BUILDARGS> is a class method.
106             #pod
107             #pod =cut
108              
109             sub BUILD {
110 8     8 1 13 my ( $self ) = @_;
111 8         28 $self->{ resources } = $self->_expand( $self->{ resources } );
112 5         238 return;
113             };
114              
115             # --------------------------------------------------------------------------------------------------
116              
117             sub _expand {
118 30     30   52 my ( $self, $item, $name ) = @_;
119 30 100       113 if ( 0 ) {
    100          
    50          
120 0         0 } elsif ( ref( $item ) eq '' ) {
121 15         85 $item = $self->fill_in_string( $item, undef, { filename => $name } );
122             } elsif ( ref( $item ) eq 'ARRAY' ) {
123 2 50       9 my $prefix = defined( $name ) ? $name . '#' : '';
124 2         2 my $idx = 0;
125 2         7 for my $elem ( @$item ) {
126 2         3 ++ $idx;
127 2         37 $elem = $self->_expand( $elem, $prefix . $idx );
128             };
129             } elsif ( ref( $item ) eq 'HASH' ) {
130 13 100       31 my $prefix = defined( $name ) ? $name . '.' : '';
131 13         41 for my $key ( keys( %$item ) ) {
132 20         149 $item->{ $key } = $self->_expand( $item->{ $key }, $prefix . $key );
133             };
134             } else {
135 0         0 die "Unexpected ref type: " . ref( $item ); ## no critic ( RequireCarping )
136             };
137 22         27193 return $item;
138             };
139              
140             # --------------------------------------------------------------------------------------------------
141              
142             __PACKAGE__->meta->make_immutable;
143              
144             1;
145              
146             # --------------------------------------------------------------------------------------------------
147              
148             #pod =head1 COPYRIGHT AND LICENSE
149             #pod
150             #pod Copyright (C) 2015 Van de Bugger
151             #pod
152             #pod License GPLv3+: The GNU General Public License version 3 or later
153             #pod <http://www.gnu.org/licenses/gpl-3.0.txt>.
154             #pod
155             #pod This is free software: you are free to change and redistribute it. There is
156             #pod NO WARRANTY, to the extent permitted by law.
157             #pod
158             #pod
159             #pod =cut
160              
161             # doc/what.pod #
162              
163             #pod =encoding UTF-8
164             #pod
165             #pod =head1 WHAT?
166             #pod
167             #pod C<Dist-Zilla-Plugin-MetaResources-Template> is a C<Dist::Zilla> plugin, a replacement for standard plugin C<MetaResources>.
168             #pod Both provide resources for distribution metadata, but this one treats values as text templates.
169             #pod
170             #pod =cut
171              
172             # end of file #
173              
174              
175             # end of file #
176              
177             __END__
178              
179             =pod
180              
181             =encoding UTF-8
182              
183             =head1 NAME
184              
185             Dist::Zilla::Plugin::MetaResources::Template - Use Perl code in distribution "resource" metadata
186              
187             =head1 VERSION
188              
189             Version v0.4.5, released on 2015-10-31 22:53 UTC.
190              
191             =head1 WHAT?
192              
193             C<Dist-Zilla-Plugin-MetaResources-Template> is a C<Dist::Zilla> plugin, a replacement for standard plugin C<MetaResources>.
194             Both provide resources for distribution metadata, but this one treats values as text templates.
195              
196             This is C<Dist::Zilla::Plugin::MetaResources::Template> module documentation. Read this if you are going to hack or
197             extend C<Dist-Zilla-Plugin-MetaResources-Template>.
198              
199             If you want to use Perl code in distribution "resource" metadata, read the L<manual|Dist::Zilla::Plugin::MetaResources::Template::Manual>. General
200             topics like getting source, building, installing, bug reporting and some others are covered in the
201             F<README>.
202              
203             =head1 SYNOPSIS
204              
205             Oops.
206              
207             =head1 DESCRIPTION
208              
209             C<Dist::Zilla::Plugin::MetaResources::Template> extends C<Dist::Zilla::Plugin::MetaResources>. The class implements C<BUILD>
210             method, which expand templates in option values, all other work is done by the parent class.
211              
212             Template processing abilities achieved by consuming C<Dist::Zilla::Role::TextTemplater> role.
213              
214             =head1 CLASS METHODS
215              
216             =head2 BUILDARGS
217              
218             Parent's C<BUILDARGS> mangles all the arguments: it moves them into resources, including
219             C<TextTemplater> arguments: C<delimiters>, C<package>, C<prepend>. We have to protect
220             C<TextTemplater> arguments from being misinterpreted.
221              
222             =head1 OBJECT METHODS
223              
224             =head2 BUILD
225              
226             The method is automatically called after object creation. The method recursively walks through the
227             C<< $self->{resources} >> and expand templates in string by calling C<< $self->fill_in_string >>
228             (it is a method from C<TextTemplater> role).
229              
230             Defining C<BUILDARGS> seems like a simpler approach because all the options are in plain list and
231             so there is no need in recursive walking trough a complex data structure (C<< $self->{ resources }
232             >>), but we need C<$self> to perform template expansion while C<BUILDARGS> is a class method.
233              
234             =head1 SEE ALSO
235              
236             =over 4
237              
238             =item L<Dist::Zilla>
239              
240             =item L<Dist::Zilla::Role>
241              
242             =item L<Dist::Zilla::Role::Plugin>
243              
244             =item L<Dist::Zilla::Role::TextTemplater>
245              
246             =item L<Dist::Zilla::Plugin::MetaResources>
247              
248             =item L<Dist::Zilla::Plugin::MetaResources::Template::Manual>
249              
250             =item L<CPAN::Meta::Spec>
251              
252             =item L<CPAN::Meta::Spec/"resources">
253              
254             =item L<Moose::Manual::Construction/"BUILD">
255              
256             =back
257              
258             =head1 AUTHOR
259              
260             Van de Bugger <van.de.bugger@gmail.com>
261              
262             =head1 COPYRIGHT AND LICENSE
263              
264             Copyright (C) 2015 Van de Bugger
265              
266             License GPLv3+: The GNU General Public License version 3 or later
267             <http://www.gnu.org/licenses/gpl-3.0.txt>.
268              
269             This is free software: you are free to change and redistribute it. There is
270             NO WARRANTY, to the extent permitted by law.
271              
272             =cut