blib/lib/Statocles/App.pm

filter: Statementstmt 97.8% 46 / 47 Branchbran 78.5% 11 / 14 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 94.8% 73 / 77 CC 11 SCAR 12.8
CC
11
Cov
94%
__ANON__
6.1
template
4.0
link
2.0
CRAP 3.6
« blib/lib/Statocles/Theme.pm ↑ Summary blib/lib/Statocles/Page/ListItem.pm »
1 package Statocles::App;
2 58 our $VERSION = '0.099';
3 # ABSTRACT: Base role for Statocles applications
4  
5 58 ▶use Statocles::Base 'Role', 'Emitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 58 ▶use Statocles::Link;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 58 requires 'pages';
8  
9 #pod =attr site
10 #pod
11 #pod The site this app is part of.
12 #pod
13 #pod =cut
14  
15 58 has site => (
16 is => 'rw',
17 isa => InstanceOf['Statocles::Site'],
18 );
19  
20 #pod =attr data
21 #pod
22 #pod A hash of arbitrary data available to theme templates. This is a good place to
23 #pod put extra structured data like social network links or make easy customizations
24 #pod to themes like header image URLs.
25 #pod
26 #pod =cut
27  
28 has data => (
29 is => 'ro',
30 isa => HashRef,
31 137 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
32 58 );
33  
34 #pod =attr url_root
35 #pod
36 #pod The URL root of this application. All pages from this app will be under this
37 #pod root. Use this to ensure two apps do not try to write the same path.
38 #pod
39 #pod =cut
40  
41 58 has url_root => (
42 is => 'ro',
43 isa => Str,
44 required => 1,
45 );
46  
47 #pod =attr templates
48 #pod
49 #pod The templates to use for this application. A mapping of template names to
50 #pod template paths (relative to the theme root directory).
51 #pod
52 #pod Developers should get application templates using L<the C<template>
53 #pod method|/template>.
54 #pod
55 #pod =cut
56  
57 has _templates => (
58 is => 'ro',
59 isa => HashRef,
60 45 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
61 58 init_arg => 'templates',
62 );
63  
64 #pod =attr template_dir
65 #pod
66 #pod The directory (inside the theme directory) to use for this app's templates.
67 #pod
68 #pod =cut
69  
70 58 has template_dir => (
71 is => 'ro',
72 isa => Str,
73 );
74  
75 #pod =attr disable_content_template
76 #pod
77 #pod This disables processing the content in this application as a template.
78 #pod This can speed up processing when the content is not using template
79 #pod directives.
80 #pod
81 #pod This can be also set in the document
82 #pod (L<Statocles::Document/disable_content_template>), or for the entire site
83 #pod (L<Statocles::Site/disable_content_template>).
84 #pod
85 #pod =cut
86  
87 58 has disable_content_template => (
88 is => 'ro',
89 isa => Bool,
90 lazy => 1,
91 default => 0,
92 predicate => 'has_disable_content_template',
93 );
94  
95 #pod =method pages
96 #pod
97 #pod my @pages = $app->pages;
98 #pod
99 #pod Get the pages for this app. Must return a list of L<Statocles::Page> objects.
100 #pod
101 #pod =cut
102  
103 22 ▶around pages => sub {
Subroutine __ANON__: called CC=6 SCAR=18.2
104 22 my ( $orig, $self, @args ) = @_;
105 22 my @pages = $self->$orig( @args );
106  
107 # Add the url_root
108 13 my $url_root = $self->url_root;
109 13 for my $page ( @pages ) {
110 21 my @url_attrs = qw( path );
111  
112 21 ▶ if ( $page->isa( 'Statocles::Page::List' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page->isa("Statocles::Page::List")) 0 21 21
113 0 push @url_attrs, qw( next prev );
114 }
115  
116 21 for my $attr ( @url_attrs ) {
117 21 ▶ next unless my $value = $page->$attr;
Branch: 1 branch
BranchTrueFalseTotal
unless my $value = $page->$attr 0 21 21
118 21 ▶ next if index( $value, $url_root ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($value, $url_root) == 0 7 14 21
119 14 $page->$attr( $self->url( $value, 1 ) );
120 }
121 }
122  
123 13 $self->emit( 'build' => class => 'Statocles::Event::Pages', pages => \@pages );
124  
125 13 return @pages;
126 58 };
127  
128 #pod =method url
129 #pod
130 #pod my $app_url = $app->url( $path[, $keep_index] );
131 #pod
132 #pod Get a URL to a page in this application. Prepends the app's L<url_root
133 #pod attribute|/url_root>. Strips "index.html" if C<$keep_index> is not given,
134 #pod or false. Avoids double-C</> in the result.
135 #pod
136 #pod =cut
137  
138 79 ▶sub url {
Subroutine url: called CC=2 SCAR=6.9
Pod documented
139 79 my ( $self, $url, $keep_index ) = @_;
140 79 my $base = $self->url_root;
141 79 ▶ $url =~ s{/index[.]html$}{/} unless $keep_index;
Branch: 1 branch
BranchTrueFalseTotal
unless $keep_index 65 14 79
142  
143 # Remove the / from both sides of the join so we don't double up
144 79 $base =~ s{/$}{};
145 79 $url =~ s{^/}{};
146  
147 79 return join "/", $base, $url;
148 }
149  
150 #pod =method link
151 #pod
152 #pod my $link = $app->link( %args )
153 #pod
154 #pod Create a link to a page in this application. C<%args> are attributes to be
155 #pod given to L<Statocles::Link> constructor. The app's L<url_root
156 #pod attribute|/url_root> is prepended, if necessary.
157 #pod
158 #pod =cut
159  
160 24 ▶sub link {
Subroutine link: called CC=2 SCAR=7.0
Pod documented
161 24 my ( $self, %args ) = @_;
162 24 my $url_root = $self->url_root;
163 24 ▶ if ( index( $args{href}, $url_root ) != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($args{"href"}, $url_root) != 0) 24 0 24
164 24 $args{href} = $self->url( $args{href} );
165 }
166 24 return Statocles::Link->new( %args );
167 }
168  
169 #pod =method template
170 #pod
171 #pod my $template = $app->template( $tmpl_name );
172 #pod
173 #pod Get a L<template object|Statocles::Template> for the given template
174 #pod name. The default template is determined by the app's class name and the
175 #pod template name passed in.
176 #pod
177 #pod Applications should list the templates they have and describe what L<page
178 #pod class|Statocles::Page> they use.
179 #pod
180 #pod =cut
181  
182 33 ▶sub template {
Subroutine template: called CC=4 SCAR=13.9
Pod documented
183 33 my ( $self, $name ) = @_;
184  
185 # Allow the site object to set the default layout
186 33 ▶ if ( $name eq 'layout.html' && !$self->_templates->{ $name } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq "layout.html" and not $self->_templates->{$name}) 16 17 33
Condition100%3/3
$name eq "layout.html" and not $self->_templates->{$name}
!ll&&!rl&&r
16116
MC/DC100%2/2
$name eq "layout.html" and not $self->_templates->{$name}
$name eq "layout.html" not $self->_templates->{$name}
Truth table100%3/3
A and not(B)
A= $name eq "layout.html"B= not $self->_templates->{$name}
ABresult
0X0
100
111
187 16 return $self->site->template( $name );
188 }
189  
190 my $path = $self->_templates->{ $name }
191 17 ▶ ? $self->_templates->{ $name }
Branch: 1 branch
BranchTrueFalseTotal
$self->_templates->{$name} ? : 2 15 17
192 : join "/", $self->template_dir, $name;
193  
194 17 return $self->site->theme->template( $path );
195 }
196  
197 58 1;
198  
199 __END__
« blib/lib/Statocles/Theme.pm ↑ Summary blib/lib/Statocles/Page/ListItem.pm »