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.086';
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   50747 use Statocles::Base 'Role';
  59         142  
  59         571  
28 59     59   425193 use Statocles::Page::Document;
  59         195  
  59         1939  
29 59     59   25734 use Statocles::Page::File;
  59         239  
  59         29167  
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 515 my ( $self, %options ) = @_;
58 151         370 my @pages;
59              
60             my @paths;
61 151         913 my $iter = $self->store->find_files( include_documents => 1 );
62 151         453 while ( my $path = $iter->() ) {
63 1875         454019 push @paths, $path;
64             }
65              
66             PATH:
67 151         438 for my $path ( @paths ) {
68              
69             # Check for hidden files and folders
70 1875 50       69717 next if $path->basename =~ /^[.]/;
71 1875         46606 my $parent = $path->parent;
72 1875         77197 while ( !$parent->is_rootdir ) {
73 4147 50       149277 next PATH if $parent->basename =~ /^[.]/;
74 4147         36018 $parent = $parent->parent;
75             }
76              
77 1875 100       99284 if ( $self->store->is_document( $path ) ) {
78 497         4197 my $page_path = $path;
79 497         1145 $page_path =~ s{[.]\w+$}{.html};
80              
81 497         6055 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         11062 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       8604 if ( $path =~ /[.]html$/ ) {
94 167         1107 my $doc_path = "$path";
95 167         1206 $doc_path =~ s/[.]html$/.markdown/;
96 167 100       423 next if grep { $_ eq $doc_path } @paths;
  15961         54288  
97             }
98              
99 1333         8283 push @pages, Statocles::Page::File->new(
100             path => $path,
101             file_path => $self->store->path->child( $path ),
102             );
103             }
104             }
105              
106 151         9704 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.086
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