Caribou/Files.pm

filter: Statementstmt 98.7% 76 / 77 Branchbran 66.6% 8 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 95.2% 20 / 21 Podpod n/a totaltotal 92.1% 106 / 115 CC 10 SCAR 8.1
CC
10
Cov
93%
_load_template_file
6.1
__ANON__
3.0
__ANON__
2.0
CRAP 2.3
« Caribou/Role.pm ↑ Summary Caribou.pm »
1 package Template::Caribou::Files;
2 5 our $AUTHORITY = 'cpan:YANICK';
3 #ABSTRACT: Role to load templates from files
4 5 $Template::Caribou::Files::VERSION = '1.1.0';
5  
6 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶use MooseX::Role::Parameterized;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 ▶use Module::Runtime qw/ module_notional_filename /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 5 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 ▶use experimental 'postderef';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 parameter dirs => (
18 default => undef,
19 );
20  
21 parameter intro => (
22 3 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 5 );
24  
25 sub _load_template_file {
26 7 ▶ my $self = shift;
Subroutine _load_template_file: called CC=6 SCAR=18.0
27 7 my $target = shift;
28  
29 7 ▶ my( $name, $file ) = @_ == 2 ? @_ : ( undef, @_ );
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 ? : 0 7 7
30  
31 7 $file = path($file);
32  
33 7 ▶ unless( $name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 7 0 7
34 7 $name = $file->basename =~ s/\.bou//r;
35 }
36  
37 7 ▶ my $class = ref $target || $target;
Condition66%2/3
ref $target || $target
l!l&&r!l&&!r
250
MC/DC0%0/2
ref $target || $target
ref $target $target
Truth table66%2/3
A || B
A= ref $targetB= $target
ABresult
1X1
011
000
38  
39 7 my $code = join "\n",
40 "package $class;",
41 $self->intro->@*,
42 "# line 1 ". $file,
43 $file->slurp;
44  
45 7 ▶ my $coderef = eval $code;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
46 7 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
47  
48 7 ▶ Template::Caribou::Role::template( (ref $target ? ( $target->meta, $target ) : $target->meta), $name, $coderef );
Branch: 1 branch
BranchTrueFalseTotal
ref $target ? : 2 5 7
49 };
50  
51  
52 sub _load_template_dir {
53 7 ▶ my ( $self, $target, $dir ) = @_;
Subroutine _load_template_dir: called CC=2 SCAR=6.9
54  
55 7 $dir = path($dir);
56  
57 7 Template::Caribou::Files::_load_template_file($self,$target,$_) for $dir->children( qr/\.bou$/ );
58 };
59  
60  
61 role {
62 7 ▶ my $p = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
63 7 my %arg = @_;
64  
65 7 has template_dirs => (
66 traits => [ 'Array' ],
67 isa => 'ArrayRef',
68 builder => '_build_template_dirs',
69 handles => {
70 all_template_dirs => 'elements',
71 add_template_dirs => 'push',
72 },
73 );
74  
75 7 my $intro = $p->intro;
76 has file_intro => (
77 is => 'ro',
78 5 ▶ default => sub { $intro },
Subroutine __ANON__: called CC=1 SCAR=0.0
79 7 );
80  
81 7 my $dirs = $p->dirs;
82  
83 7 ▶ unless ( $dirs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dirs) 2 5 7
84 2 my $name = $arg{consumer}->name;
85  
86 try {
87 2 ▶ my $path = path( $INC{ module_notional_filename( $name )} =~ s/\.pm$//r );
Subroutine __ANON__: called CC=2 SCAR=7.1
88 2 ▶ die unless $path->is_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $path->is_dir 0 2 2
89 2 $dirs = [ $path ];
90 } catch {
91 0 ▶ die "can't find directory for module '$name'";
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 2 };
93 }
94  
95 # so that we can call the role many times,
96 # and the defaults will telescope into each other
97 5 ▶ sub _build_template_dirs { [] }
Subroutine _build_template_dirs: called CC=1 SCAR=0.0
98  
99 around _build_template_dirs => sub {
100 7 ▶ my( $ref, $self ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
101  
102 7 return [ @{ $ref->($self) }, @$dirs ];
103 7 };
104  
105  
106 7 Template::Caribou::Files::_load_template_dir( $p, $arg{consumer}->name, $_) for @$dirs;
107  
108 method add_template_file => sub {
109 2 ▶ my( $self, $file ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
110 2 $file = path($file);
111  
112 2 Template::Caribou::Files::_load_template_file(
113 $p,
114 $self,
115 $file
116 );
117 7 };
118  
119  
120  
121  
122 5 };
123  
124  
125  
126 5 1;
127  
128 __END__
129  
130 =pod
131  
132 =encoding UTF-8
133  
134 =head1 NAME
135  
136 Template::Caribou::Files - Role to load templates from files
137  
138 =head1 VERSION
139  
140 version 1.1.0
141  
142 =head1 SYNOPSIS
143  
144 package MyTemplate;
145  
146 use Template::Caribou;
147  
148 with 'Template::Caribou::Files' => {
149 dirs => [ './my_templates/' ],
150 intro => [ 'use 5.10.0;' ],
151 };
152  
153 1;
154  
155 =head1 DESCRIPTION
156  
157 A Caribou class consuming the C<Template::Caribou::Files> role
158 will automatically import
159 all template files (i.e., all files with a C<.bou> extension) under the given directories.
160  
161 The names of the imported templates will be their path, relative to the
162 imported directories, without their extension. To take the example in the
163 L</SYNOPSIS>, if the content of C<my_templates/> is:
164  
165 ./foo.bou
166 ./bar.bou
167  
168 then the templates C<foo.bou> and C<bar.bou> will be created.
169  
170 The template files themselves will be eval'ed in the context of
171 the parent class/namespace, and must return a coderef. E.g.,
172  
173 # file ./foo.bou
174  
175 # should be done in the class declaration, but
176 # can be done here as well
177 use Template::Caribou::Tags::HTML ':all';
178  
179 # likewise, would be better if added to
180 # as a Template::Caribou::Files's intro
181 use experimental 'signatures';
182  
183 sub ($self) {
184 div {
185 say 'this is foo';
186 $self->bar;
187 }
188 }
189  
190 =head1 METHODS
191  
192 =head2 all_template_dirs
193  
194 Returns a list of all template directories loaded by the
195 class (directories included by parent classes included).
196  
197 =head2 file_intro
198  
199 Returns the arrayref of the intro lines added to the F<.bou>
200 templates.
201  
202 =head1 ROLE PARAMETERS
203  
204 =head2 dirs
205  
206 The array ref of directories to scan for templates.
207  
208 If not provided,
209 it defaults to the directory associated with the template class.
210 For example, for
211  
212 package MyTemplates::Foo;
213  
214 use Template::Caribou;
215  
216 with 'Template::Caribou::Files';
217  
218 1;
219  
220 located at F<lib/MyTemplates/Foo.pm>, the default directory
221 will be F<lib/MyTemplates/Foo/>.
222  
223 =head2 intro
224  
225 Arrayref of lines to add at the beginning of all F<.bou> templates.
226  
227 package MyTemplates::Foo;
228  
229 use Template::Caribou;
230  
231 with 'Template::Caribou::Files' => {
232 intro => [
233 q{ use 5.10.0; },
234 q{ use experimental 'signatures'; },
235 ];
236 };
237  
238 =head1 AUTHOR
239  
240 Yanick Champoux <yanick@cpan.org>
241  
242 =head1 COPYRIGHT AND LICENSE
243  
244 This software is copyright (c) 2017 by Yanick Champoux.
245  
246 This is free software; you can redistribute it and/or modify it under
247 the same terms as the Perl 5 programming language system itself.
248  
249 =cut
« Caribou/Role.pm ↑ Summary Caribou.pm »