blib/lib/Catalyst/ComponentRole/PathFrom.pm

filter: Statementstmt 98.3% 61 / 62 Branchbran 77.2% 17 / 22 Conditioncond 78.5% 11 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 100% 12 / 12 Podpod 100% 1 / 1 totaltotal 88.2% 105 / 119 CC 17 SCAR 14.0
CC
17
Cov
91%
path_from
8.2
_path_from_action_attribute
4.0
_path_from_proto
3.0
CRAP 4.1
↑ Summary
1 package Catalyst::ComponentRole::PathFrom;
2  
3 1 our $VERSION = '0.002';
4  
5 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 with 'Catalyst::Component::ApplicationAttribute';
9  
10 1 has extension => (
11 is=>'ro',
12 predicate=>'has_extension');
13  
14 1 has stash_key => (
15 is=>'ro',
16 required=>1,
17 lazy=>1,
18 builder=>'_build_stash_key');
19  
20 1 ▶ sub _build_stash_key { return 'path_from' }
Subroutine _build_stash_key: called CC=1 SCAR=0.0
21  
22 1 has action_attribute => (
23 is=>'ro',
24 required=>1,
25 lazy=>1,
26 builder=>'_build_action_attribute');
27  
28 1 ▶ sub _build_action_attribute { return 'PathFrom' }
Subroutine _build_action_attribute: called CC=1 SCAR=0.0
29  
30 1 has path_base => (
31 is=>'ro',
32 required=>1,
33 lazy=>1,
34 builder=>'_build_path_base');
35  
36 1 ▶ sub _build_path_base {
Subroutine _build_path_base: called CC=1 SCAR=0.0
37 1 my $self = shift;
38 1 my $app = $self->_application;
39 1 return $app->config->{root};
40 }
41  
42 10 ▶sub _normalized_extension {
Subroutine _normalized_extension: called CC=1 SCAR=0.0
43 10 my $self = shift;
44 # Because some people think the '.' is always needed...
45 10 my $ext = $self->extension;
46 10 $ext =~s/^\.?(.+)$/$1/;
47  
48 10 return $ext;
49 }
50  
51 10 ▶sub _path_from_proto {
Subroutine _path_from_proto: called CC=3 SCAR=11.0
52 10 my ($self, @proto) = @_;
53 10 ▶ my $filepath = $proto[0] eq '' ?
Branch: 1 branch
BranchTrueFalseTotal
$proto[0] eq '' ? : 1 9 10
54 File::Spec->catfile(@proto) :
55 $self->path_base->file(@proto);
56  
57 10 ▶ if($self->has_extension) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_extension) 10 0 10
58 10 $filepath = $filepath .'.'. $self->_normalized_extension;
59 }
60  
61 10 return $filepath;
62 }
63  
64 10 ▶sub path_from {
Subroutine path_from: called CC=8 SCAR=21.1
Pod documented
65 10 my $self = shift;
66 10 my $proto = shift;
67  
68 10 my ($c, $action) = ();
69 10 ▶ if($proto->isa('Catalyst::Action')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto->isa('Catalyst::Action')) { } 0 10 10
70 0 $action = $proto;
71 } else {
72 10 $c = $proto;
73 10 my $proto2 = shift;
74 10 ▶ if(defined $proto2 && (ref(\$proto2) eq 'SCALAR')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proto2 and ref \$proto2 eq 'SCALAR') { } 1 9 10
Condition100%3/3
defined $proto2 and ref \$proto2 eq "SCALAR"
!ll&&!rl&&r
811
MC/DC100%2/2
defined $proto2 and ref \$proto2 eq "SCALAR"
defined $proto2 ref \$proto2 eq "SCALAR"
Truth table100%3/3
A and B
A= defined $proto2B= ref \$proto2 eq "SCALAR"
ABresult
0X0
100
111
75 1 my @string_path = $self->_expand_template($c->action, $proto2);
76 1 return $self->_path_from_proto(@string_path);
77 }
78 else {
79 9 $action = $proto2;
80 }
81 }
82
83 9 ▶ if(defined $action) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $action) { } 1 8 9
84 # If an action was submitted, create path ONLY from that.
85 1 ▶ return $self->_path_from_action_attribute($action)||
Condition33%1/3
$self->_path_from_action_attribute($action) || $self->_path_from_action($action)
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->_path_from_action_attribute($action) || $self->_path_from_action($action)
$self->_path_from_action_attribute($action) $self->_path_from_action($action)
Truth table33%1/3
A || B
A= $self->_path_from_action_attribute($action)B= $self->_path_from_action($action)
ABresult
1X1
011
000
86 $self->_path_from_action($action);
87 } else {
88 8 $action = $c->action;
89 }
90
91 8 ▶ return $self->_path_from_stash($c) ||
Condition83%5/6
$self->_path_from_stash($c) || $self->_path_from_action_attribute($action) || $self->_path_from_action($action)
l!l&&r!l&&!r
710
$self->_path_from_stash($c) or $self->_path_from_action_attribute($action)
l!l&&r!l&&!r
521
MC/DC0%0/3
$self->_path_from_stash($c) || $self->_path_from_action_attribute($action) || $self->_path_from_action($action)
$self->_path_from_stash($c) $self->_path_from_action_attribute($action) $self->_path_from_action($action)
Truth table75%3/4
A or B || C
A= $self->_path_from_stash($c)B= $self->_path_from_action_attribute($action)C= $self->_path_from_action($action)
ABCresult
1XX1
01X1
0011
0000
92 $self->_path_from_action_attribute($action) ||
93 $self->_path_from_action($action);
94 }
95  
96 8 ▶sub _expand_template {
Subroutine _expand_template: called CC=3 SCAR=11.0
97 8 my ($self, $action, $pattern) = @_;
98 8 my %template_args = (
99 ':namespace' => $action->namespace,
100 ':reverse' => $action->reverse,
101 ':actionname' => $action->name,
102 );
103  
104 return my @parts =
105 13 ▶ map { ref $_ ? @$_ : $_ }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 13 13
106 8 ▶ map { defined($template_args{$_}) ? $template_args{$_} : $_ }
Branch: 1 branch
BranchTrueFalseTotal
defined $template_args{$_} ? : 4 9 13
107 split('/', $pattern);
108 }
109  
110 8 ▶sub _path_from_stash {
Subroutine _path_from_stash: called CC=2 SCAR=6.9
111 8 my ($self, $c) = @_;
112 8 my $proto = $c->stash->{$self->stash_key};
113 8 ▶ return unless defined $proto;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $proto 3 5 8
114  
115 5 my @expanded_proto = $self->_expand_template($c->action, $proto);
116 5 return $self->_path_from_proto(@expanded_proto);
117 }
118  
119 4 ▶sub _path_from_action_attribute {
Subroutine _path_from_action_attribute: called CC=4 SCAR=13.9
120 4 my ($self, $action) = @_;
121 4 ▶ my ($proto, @more) = @{$action->attributes->{$self->action_attribute} || []};
Condition100%2/2
$action->attributes->{$self->action_attribute} || []
l!l
22
MC/DC100%1/1
$action->attributes->{$self->action_attribute} || []
$action->attributes->{$self->action_attribute}
Truth table100%2/2
A
A= $action->attributes->{$self->action_attribute}
Aresult
11
00
122 4 ▶ return unless defined $proto;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $proto 2 2 4
123  
124 2 ▶ die "Too many action attributes for $action" if @more;
Branch: 1 branch
BranchTrueFalseTotal
if @more 0 2 2
125  
126 2 my @expanded_proto = $self->_expand_template($action, $proto);
127 2 return $self->_path_from_proto(@expanded_proto);
128 }
129  
130 2 ▶sub _path_from_action {
Subroutine _path_from_action: called CC=2 SCAR=7.1
131 2 my ($self, $action) = @_;
132 2 ▶ return unless defined $action;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $action 0 2 2
133 2 return $self->_path_from_proto("$action");
134 }
135  
136 1 1;
137  
138 =head1 NAME
139  
140 Catalyst::ComponentRole::PathFrom - Derive a path using common Catalyst patterns
141  
142 =head1 SYNOPSIS
143  
144 package MyApp::Model::Path;
145  
146 use Moose;
147  
148 extends 'Catalyst::Component';
149 with 'Catalyst::ComponentRole::PathFrom',
150 'Catalyst::Component::InstancePerContext';
151  
152 has ctx => (is=>'rw', weak_ref=>1);
153  
154 sub build_per_context_instance {
155 my ($self, $c) = @_;
156 $self->ctx($c);
157 return $self;
158 }
159  
160 around 'path_from', sub {
161 my ($orig, $self, @args) = @_;
162 return $self->$orig($self->ctx, @args);
163 };
164  
165 __PACKAGE__->meta->make_immutable;
166  
167 package MyApp::Controller::Example;
168 use base 'Catalyst::Controller';
169  
170 sub test_a :Local {
171 my ($self, $c) = @_;
172 }
173  
174 sub test_b :Local PathFrom('ffffff') {
175 my ($self, $c) = @_;
176 }
177  
178 sub test_c :Local {
179 my ($self, $c) = @_;
180 $c->stash(path_from=>'foo/bar');
181 }
182  
183 =head1 DESCRIPTION
184  
185 Common L<Catalyst> views set a template path using a standard process,
186 typically one based on the action or from a stash key. This component
187 role trys to encapsulate that common pattern, with the hope that it makes
188 it easier for people to make new Views in a consistent way. For example
189 if you make your own custom Views this could save you some time in getting
190 a common and expected setup.
191  
192 =head1 ATTRIBUTES
193  
194 This role exposes the following attributes for configuration
195  
196 =head2 extension
197  
198 Optional. This is a file extension added to the end of your generated file
199 path. For example 'html', 'tt2'. You don't need to include the '.' separator.
200  
201 =head2 stash_key
202  
203 Has default, 'path_from'. Used to set the stash key you wish to use to
204 programmatically set the file path pattern in your action body.
205  
206 =head2 action_attribute
207  
208 Has default, 'PathFrom'. Used to set the action attribute we use to get a file
209 path pattern.
210  
211 =head2 path_base
212  
213 Has default "$app->config->{root}". Used to set the base path for relative
214 paths. Usually you leave this one alone :)
215  
216 =head1 METHODS
217  
218 This role exposes the following public methods
219  
220 =head2 path_from ( $action | $c | $c, $action | $c, $string_path )
221  
222 Builds a full path to a file on the filesystem using common L<Catalyst> conventions.
223  
224 Given an $action, will return $base_path + $action->reverse + $extension OR if
225 the $action has an attribute value for $action_attribute, return $base_path +
226 $action_attribute + $extension.
227  
228 Given $c, will do all the above (using $c->action for $action), but also check if
229 the stash contains $stash_key and if so use that path instead.
230  
231 Given $c, $action, does as above but uses the given $action instead of $c->action
232  
233 Given $c, $string_path, uses $string_path instead of $action->reverse.
234  
235 When using a $string_path, a $stash_key value or a value in $action_attribute you
236 may use the following placeholders in the string (for example ':namespace/foo')
237  
238 =over4
239  
240 =item :namespace
241  
242 The action namespace ($action->namespace), which is typically the controller
243 namespace
244  
245 =item :reverse
246  
247 "$action->reverse" (which is basically the default
248  
249 =item :actionname
250  
251 "action->name" (the subroutine method name, typically).
252  
253 =back
254  
255 B<NOTE>: if you use a $string_path, a $stash_key value or a value in
256 $action_attribute and that value starts with '/', that is a signal you wish to
257 use an absolute path, and we don't prepend $self->base_path. You probably
258 won't need this...
259  
260 =head1 SEE ALSO
261  
262 L<Catalyst>, L<Catalyst::Component>, L<File::Spec>, L<Moose::Role>.
263  
264 =head1 AUTHOR
265
266 John Napiorkowski L<email:jjnapiork@cpan.org>
267
268 =head1 COPYRIGHT & LICENSE
269
270 Copyright 2015, John Napiorkowski L<email:jjnapiork@cpan.org>
271
272 This library is free software; you can redistribute it and/or modify it under
273 the same terms as Perl itself.
274  
275 =cut
↑ Summary