File Coverage

blib/lib/Statocles/App/Role/Store.pm
Criterion Covered Total %
statement 32 32 100.0
branch 8 10 80.0
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 45 47 95.7


line stmt bran cond sub pod time code
1             package Statocles::App::Role::Store;
2             our $VERSION = '0.084';
3             # ABSTRACT: Role for applications using files
4              
5             #pod =head1 SYNOPSIS
6             #pod
7             #pod package MyApp;
8             #pod use Statocles::Base 'Class';
9             #pod with 'Statocles::App::Role::Store';
10             #pod
11             #pod around pages => sub {
12             #pod my ( $orig, $self, %options ) = @_;
13             #pod my @pages = $self->$orig( %options );
14             #pod
15             #pod # ... Add/remove pages
16             #pod
17             #pod return @pages;
18             #pod };
19             #pod
20             #pod =head1 DESCRIPTION
21             #pod
22             #pod This role provides some basic functionality for those applications that want
23             #pod to use L<store objects|Statocles::Store> to manage content with Markdown files.
24             #pod
25             #pod =cut
26              
27 59     59   50650 use Statocles::Base 'Role';
  59         139  
  59         553  
28 59     59   437097 use Statocles::Page::Document;
  59         207  
  59         1925  
29 59     59   25511 use Statocles::Page::File;
  59         213  
  59         28027  
30             with 'Statocles::App';
31              
32             #pod =attr store
33             #pod
34             #pod The directory path or L<store object|Statocles::Store> containing this app's
35             #pod documents. Required.
36             #pod
37             #pod =cut
38              
39             has store => (
40             is => 'ro',
41             isa => Store,
42             required => 1,
43             coerce => Store->coercion,
44             );
45              
46             #pod =method pages
47             #pod
48             #pod my @pages = $app->pages;
49             #pod
50             #pod Get all the pages for this application. Markdown documents are wrapped
51             #pod in L<Statocles::Page::Document> objects, and everything else is wrapped in
52             #pod L<Statocles::Page::File> objects.
53             #pod
54             #pod =cut
55              
56             sub pages {
57 151     151 1 484 my ( $self, %options ) = @_;
58 151         340 my @pages;
59              
60             my @paths;
61 151         932 my $iter = $self->store->find_files( include_documents => 1 );
62 151         493 while ( my $path = $iter->() ) {
63 1875         450370 push @paths, $path;
64             }
65              
66             PATH:
67 151         439 for my $path ( @paths ) {
68              
69             # Check for hidden files and folders
70 1875 50       69557 next if $path->basename =~ /^[.]/;
71 1875         45891 my $parent = $path->parent;
72 1875         75720 while ( !$parent->is_rootdir ) {
73 4147 50       143061 next PATH if $parent->basename =~ /^[.]/;
74 4147         35043 $parent = $parent->parent;
75             }
76              
77 1875 100       97739 if ( $self->store->is_document( $path ) ) {
78 497         4533 my $page_path = $path;
79 497         1125 $page_path =~ s{[.]\w+$}{.html};
80              
81 497         6315 my %args = (
82             path => $page_path,
83             app => $self,
84             layout => $self->template( 'layout.html' ),
85             document => $self->store->read_document( $path ),
86             );
87              
88 497         10758 push @pages, Statocles::Page::Document->new( %args );
89             }
90             else {
91             # If there's a markdown file, don't keep the html file, since
92             # we'll be building it from the markdown
93 1378 100       8537 if ( $path =~ /[.]html$/ ) {
94 167         1003 my $doc_path = "$path";
95 167         1071 $doc_path =~ s/[.]html$/.markdown/;
96 167 100       393 next if grep { $_ eq $doc_path } @paths;
  15961         48722  
97             }
98              
99 1333         8175 push @pages, Statocles::Page::File->new(
100             path => $path,
101             file_path => $self->store->path->child( $path ),
102             );
103             }
104             }
105              
106 151         8769 return @pages;
107             }
108              
109             1;
110              
111             __END__
112              
113             =pod
114              
115             =encoding UTF-8
116              
117             =head1 NAME
118              
119             Statocles::App::Role::Store - Role for applications using files
120              
121             =head1 VERSION
122              
123             version 0.084
124              
125             =head1 SYNOPSIS
126              
127             package MyApp;
128             use Statocles::Base 'Class';
129             with 'Statocles::App::Role::Store';
130              
131             around pages => sub {
132             my ( $orig, $self, %options ) = @_;
133             my @pages = $self->$orig( %options );
134              
135             # ... Add/remove pages
136              
137             return @pages;
138             };
139              
140             =head1 DESCRIPTION
141              
142             This role provides some basic functionality for those applications that want
143             to use L<store objects|Statocles::Store> to manage content with Markdown files.
144              
145             =head1 ATTRIBUTES
146              
147             =head2 store
148              
149             The directory path or L<store object|Statocles::Store> containing this app's
150             documents. Required.
151              
152             =head1 METHODS
153              
154             =head2 pages
155              
156             my @pages = $app->pages;
157              
158             Get all the pages for this application. Markdown documents are wrapped
159             in L<Statocles::Page::Document> objects, and everything else is wrapped in
160             L<Statocles::Page::File> objects.
161              
162             =head1 AUTHOR
163              
164             Doug Bell <preaction@cpan.org>
165              
166             =head1 COPYRIGHT AND LICENSE
167              
168             This software is copyright (c) 2016 by Doug Bell.
169              
170             This is free software; you can redistribute it and/or modify it under
171             the same terms as the Perl 5 programming language system itself.
172              
173             =cut