lib/Rapi/Blog/Scaffold.pm

filter: Statementstmt 52.2% 80 / 153 Branchbran 25.0% 10 / 40 Conditioncond 8.3% 1 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 36.0% 18 / 50 Podpod 0.0% 0 / 12 totaltotal 39.6% 109 / 275 CC 26 SCAR 17.4
CC
26
Cov
44%
resolve_ViewWrapper
20.0
factory
12.0
_resolve_path_to_post
12.0
CRAP 5.7
« lib/Rapi/Blog/DB/ResultSet/Section.pm ↑ Summary lib/Rapi/Blog/Module/PostPage.pm »
1 package Rapi::Blog::Scaffold;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 2 ▶use RapidApp::Util qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Rapi::Blog::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use String::Random;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 require Text::Glob;
11
12 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Types::Standard ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
14
15 2 ▶use Rapi::Blog::Scaffold::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Rapi::Blog::Scaffold::ViewWrapper;
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 2 ▶use Plack::App::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Plack::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Plack::Middleware::ConditionalGET;
Subroutine BEGIN: called CC=1 SCAR=0.0
21
22 2 require Path::Class;
23 2 ▶use YAML::XS 0.64 'LoadFile';
Subroutine BEGIN: called CC=1 SCAR=0.0
24
25
26 0 ▶sub factory {
Subroutine factory: not called CC=3 SCAR=24.8
Pod undocumented
27 0 my ($self, $new) = @_;
28
29 # passthrough is its already one of us:
30 0 ▶ return $new if (ref($new) && ref($new) eq __PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if ref $new and ref $new eq __PACKAGE__ 0 0 0
Condition0%0/3
ref $new and ref $new eq __PACKAGE__
!ll&&!rl&&r
000
MC/DC0%0/2
ref $new and ref $new eq __PACKAGE__
ref $new ref $new eq __PACKAGE__
Truth table0%0/3
A and B
A= ref $newB= ref $new eq __PACKAGE__
ABresult
0X0
100
111
31
32 0 __PACKAGE__->new( dir => $new )
33 }
34
35
36 has 'uuid', is => 'ro', init_arg => undef,
37 0 ▶ default => sub { join('-','scfld',String::Random->new->randregex('[a-z0-9A-Z]{20}')) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
38
39 has 'dir',
40 is => 'ro',
41 required => 1,
42 isa => InstanceOf['Path::Class::Dir'],
43 0 ▶ coerce => sub { Path::Class::dir($_[0]) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
44
45
46 has 'config',
47 is => 'ro',
48 isa => InstanceOf['Rapi::Blog::Scaffold::Config'],
49 0 ▶ default => sub {{}},
Subroutine __ANON__: not called CC=1 SCAR=6.9
50 0 ▶ coerce => sub { blessed $_[0] ? $_[0] : Rapi::Blog::Scaffold::Config->new($_[0]) };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) ? : 0 0 0
51
52 # The Scaffold needs to be able to check if a given Post exists in the database
53 #has 'Post_exists_fn', is => 'ro', required => 1, isa => CodeRef;
54
55
56 0 ▶sub static_paths { (shift)->config->static_paths }
Subroutine static_paths: not called CC=1 SCAR=6.9
Pod undocumented
57 0 ▶sub template_names { (shift)->config->template_names }
Subroutine template_names: not called CC=1 SCAR=6.9
Pod undocumented
58 0 ▶sub private_paths { (shift)->config->private_paths }
Subroutine private_paths: not called CC=1 SCAR=6.9
Pod undocumented
59 0 ▶sub default_ext { (shift)->config->default_ext }
Subroutine default_ext: not called CC=1 SCAR=6.9
Pod undocumented
60 0 ▶sub view_wrappers { (shift)->config->view_wrappers }
Subroutine view_wrappers: not called CC=1 SCAR=6.9
Pod undocumented
61 0 ▶sub internal_post_path { (shift)->config->internal_post_path }
Subroutine internal_post_path: not called CC=1 SCAR=6.9
Pod undocumented
62
63 # This is a unique, private path which is automatically generated that allows this
64 # scaffold to own a path which it can use fetch a post, and be sure another scaffold
65 # wont claim the path
66 0 ▶has 'unique_int_post_path', is => 'ro', init_arg => undef, lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 0 my $self = shift;
68 0 join('','_',$self->uuid,'/private/post/')
69 2 };
70
71 0 ▶sub not_found_template { (shift)->config->not_found }
Subroutine not_found_template: not called CC=1 SCAR=6.9
Pod undocumented
72
73
74
75 0 ▶has 'ViewWrappers', is => 'ro', init_arg => undef, lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 my $self = shift;
77 return [ map {
78 0 Rapi::Blog::Scaffold::ViewWrapper->new(
79 Scaffold => $self, %$_
80 )
81 0 } @{$self->config->view_wrappers} ]
82 2 }, isa => ArrayRef[InstanceOf['Rapi::Blog::Scaffold::ViewWrapper']];
83
84
85
86
87 0 ▶sub BUILD {
Subroutine BUILD: not called CC=1 SCAR=6.9
Pod undocumented
88 0 my $self = shift;
89 0 $self->_load_yaml_config;
90 }
91
92
93 0 ▶sub _load_yaml_config {
Subroutine _load_yaml_config: not called CC=2 SCAR=17.9
94 0 my $self = shift;
95
96 0 my $yaml_file = $self->dir->file('scaffold.yml');
97 0 ▶ $self->config->_load_from_yaml($yaml_file) if (-f $yaml_file);
Branch: 1 branch
BranchTrueFalseTotal
if -f $yaml_file 0 0 0
98 }
99
100
101 0 ▶sub resolve_ViewWrapper {
Subroutine resolve_ViewWrapper: not called CC=4 SCAR=30.0
Pod undocumented
102 0 my $self = shift;
103 0 ▶ my $path = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $path = shift() or return undef 0 0 0
104
105 0 my $subpath;
106 0 ▶ my $VW = List::Util::first { $subpath = $_->resolve_subpath($path) } @{ $self->ViewWrappers };
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 0 ▶ return undef unless $VW;
Branch: 1 branch
BranchTrueFalseTotal
unless $VW 0 0 0
108
109 0 ▶ wantarray ? ($VW, $subpath) : $VW
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
110 }
111
112
113
114 0 ▶sub _resolve_path_to_post {
Subroutine _resolve_path_to_post: not called CC=3 SCAR=24.8
115 0 my ($self, $path) = @_;
116
117 0 my ($pfx,$name) = split($self->unique_int_post_path,$path,2);
118 0 ▶ ($name && $pfx eq '') ? $name : undef
Branch: 1 branch
BranchTrueFalseTotal
$name && $pfx eq '' ? : 0 0 0
Condition0%0/3
$name and $pfx eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
$name and $pfx eq ""
$name $pfx eq ""
Truth table0%0/3
A and B
A= $nameB= $pfx eq ""
ABresult
0X0
100
111
119 }
120
121
122
123 0 ▶has '_static_path_regexp', is => 'ro', lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
124 0 my $self = shift;
125 0 return $self->_compile_path_list_regex(@{$self->static_paths});
126 2 };
127
128 0 ▶has '_template_name_regexp', is => 'ro', lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 0 my $self = shift;
130 0 return $self->_compile_path_list_regex(@{$self->template_names});
131 2 };
132
133 0 ▶has '_private_path_regexp', is => 'ro', lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
134 0 my $self = shift;
135 0 return $self->_compile_path_list_regex(@{$self->private_paths});
136 2 };
137
138
139 26 ▶sub _compile_path_list_regex {
Subroutine _compile_path_list_regex: called CC=1 SCAR=0.0
140 26 my ($self, @paths) = @_;
141 26 my $reStr = $self->_get_path_list_regex_string(@paths);# or return undef;
142 26 qr/$reStr/
143 }
144
145
146 0 ▶sub __re_always { '(^.+$)' } # Always matches
Subroutine __re_always: not called CC=1 SCAR=6.9
147 0 ▶sub __re_never { '(?=a)[^a]' } # Never matches
Subroutine __re_never: not called CC=1 SCAR=6.9
148
149 36 ▶sub _glob_to_re_str {
Subroutine _glob_to_re_str: called CC=2 SCAR=6.9
150 36 my ($self,$glob) = @_;
151
152 36 my $re = Text::Glob::glob_to_regex_string($glob);
153
154 # unless the glob ends in a *, we do not
155 # want to match past the end of the pattern
156 36 ▶ $glob =~ /\*$/ ? $re : $re.'$'
Branch: 1 branch
BranchTrueFalseTotal
$glob =~ /\*$/ ? : 21 15 36
157 }
158
159 26 ▶sub _get_path_list_regex_string {
Subroutine _get_path_list_regex_string: called CC=9 SCAR=22.5
160 26 my ($self, @paths) = @_;
161 26 ▶ return $self->__re_never unless (scalar(@paths) > 0);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @paths > 0 0 26 26
162
163 26 my @gList = ();
164 26 my @eqList = ();
165 26 for my $path (@paths) {
166 84 ▶ next if ($path eq ''); # empty string match nothing
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "" 0 84 84
167
168 # Either * or / match everything. Bail out and return the match everything regex
169 84 ▶ return $self->__re_always if ($path eq '*' || $path eq '/');
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "*" or $path eq "/" 0 84 84
Condition33%1/3
$path eq "*" or $path eq "/"
l!l&&r!l&&!r
0084
MC/DC0%0/2
$path eq "*" or $path eq "/"
$path eq "*" $path eq "/"
Truth table33%1/3
A or B
A= $path eq "*"B= $path eq "/"
ABresult
1X1
011
000
170
171 84 $path =~ s/^\///; # strip and ignore leading /
172 84 ▶ $path .= '*' if ($path =~ /\/$/); # append wildcard if we end in / we want to match everything that follows
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[/$] 21 63 84
173
174 # Check for any glob pattern special characters:
175 84 ▶ $path =~ /[\*\?\[\]\{\}]/ ? push(@gList, $path) : push(@eqList,$path);
Branch: 1 branch
BranchTrueFalseTotal
$path =~ /[\*\?\[\]\{\}]/ ? : 36 48 84
176 }
177
178 26 local $Text::Glob::strict_leading_dot = 0;
179 26 local $Text::Glob::strict_wildcard_slash = 0;
180
181 my @re_list = (
182 36 (map { $self->_glob_to_re_str($_) } @gList ),
183 26 (map { join('','^',quotemeta($_),'$') } @eqList )
184 );
185
186 26 my $reStr = join('','(',join('|', @re_list ),')');
187
188 26 ▶ $reStr ? $reStr : $self->__re_never
Branch: 1 branch
BranchTrueFalseTotal
$reStr ? : 26 0 26
189 }
190
191
192
193
194 0 ▶has 'static_path_app', is => 'ro', lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=5.6
195 0 my $self = shift;
196 0 ▶ my $app = builder {
Subroutine __ANON__: not called CC=1 SCAR=6.9
197 0 enable "ConditionalGET";
198 0 Plack::App::File->new(root => $self->dir)->to_app;
199 0 };
200
201 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
202 0 my $env = shift;
203 0 my $res = $app->($env);
204 # limit caching to 10 minutes now that we return 304s
205 0 push @{$res->[1]}, 'Cache-Control', 'public, max-age=600';
206
207 0 $res
208 }
209 0 };
210
211
212
213 0 ▶sub _is_static_path {
Subroutine _is_static_path: not called CC=2 SCAR=17.9
214 0 my ($self, $template) = @_;
215 0 my $Regexp = $self->_static_path_regexp;
216 0 ▶ $Regexp ? $template =~ $Regexp : 0
Branch: 1 branch
BranchTrueFalseTotal
$Regexp ? : 0 0 0
217 }
218
219 0 ▶sub _is_valid_template_name {
Subroutine _is_valid_template_name: not called CC=2 SCAR=17.9
220 0 my ($self, $name) = @_;
221 0 my $Regexp = $self->_template_name_regexp;
222 0 ▶ $Regexp ? $name =~ $Regexp : 0
Branch: 1 branch
BranchTrueFalseTotal
$Regexp ? : 0 0 0
223 }
224
225 0 ▶sub _is_private_path {
Subroutine _is_private_path: not called CC=2 SCAR=17.9
226 0 my ($self, $template) = @_;
227 0 my $Regexp = $self->_private_path_regexp;
228 0 ▶ $Regexp ? $template =~ $Regexp : 0
Branch: 1 branch
BranchTrueFalseTotal
$Regexp ? : 0 0 0
229 }
230
231
232 0 ▶sub resolve_file {
Subroutine resolve_file: not called CC=3 SCAR=24.8
Pod undocumented
233 0 my $self = shift;
234 0 ▶ my $path = shift or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $path = shift() or return undef 0 0 0
235
236 0 my $File = $self->dir->file($path);
237 0 ▶ -f $File ? $File : undef
Branch: 1 branch
BranchTrueFalseTotal
-f $File ? : 0 0 0
238 }
239
240
241 0 ▶sub resolve_static_file {
Subroutine resolve_static_file: not called CC=3 SCAR=24.8
Pod undocumented
242 0 my ($self, $path) = @_;
243 0 ▶ $path && $self->_is_static_path($path) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$path and $self->_is_static_path($path) or return undef 0 0 0
Condition0%0/3
$path and $self->_is_static_path($path)
!ll&&!rl&&r
000
MC/DC0%0/2
$path and $self->_is_static_path($path)
$path $self->_is_static_path($path)
Truth table0%0/3
A and B
A= $pathB= $self->_is_static_path($path)
ABresult
0X0
100
111
244 0 $self->resolve_file($path)
245 }
246
247
248 2 1;
« lib/Rapi/Blog/DB/ResultSet/Section.pm ↑ Summary lib/Rapi/Blog/Module/PostPage.pm »