File Coverage

lib/Contenticious/Content.pm
Criterion Covered Total %
statement 27 27 100.0
branch 2 2 100.0
condition 2 2 100.0
subroutine 9 9 100.0
pod 3 4 75.0
total 43 44 97.7


line stmt bran cond sub pod time code
1             package Contenticious::Content;
2 4     4   856 use Mojo::Base -base;
  4         6584  
  4         22  
3              
4 4     4   1876 use Contenticious::Content::Node::Directory;
  4         6  
  4         34  
5 4     4   2390 use File::Copy::Recursive 'dircopy';
  4         10686  
  4         214  
6 4     4   18 use Carp;
  4         6  
  4         1198  
7              
8             has pages_dir => sub { croak 'no pages_dir given' };
9             has root_node => sub { shift->build_root_node };
10              
11             # root_node builder
12             sub build_root_node {
13 15     15 0 24 my $self = shift;
14              
15             # let there be root!
16 15         39 return Contenticious::Content::Node::Directory->new(
17             filename => $self->pages_dir,
18             is_root => 1,
19             );
20             }
21              
22             # find a content node for a given path like foo/bar/baz
23             sub find {
24 16     16 1 410 my $self = shift;
25 16   100     48 my $path = shift // '';
26              
27             # split path and find content node
28 16         51 my @names = split m|/| => $path;
29 16         46 return $self->root_node->find(@names);
30             }
31              
32             # execute a subroutine for all content nodes
33             # the given subroutine gets the node as a single argument
34             sub for_all_nodes {
35 2     2 1 335 my ($self, $sub) = @_;
36 2         8 _walk_tree($self->root_node, $sub);
37             }
38              
39             # not a public method but a recursive utitlity function
40             sub _walk_tree {
41 14     14   74 my ($node, $sub) = @_;
42            
43             # execute
44 14         27 $sub->($node);
45              
46             # walk the tree if possible (duck typing)
47 14 100       118 if ($node->can('children')) {
48 6         7 _walk_tree($_, $sub) foreach @{$node->children};
  6         16  
49             }
50             }
51              
52             # delete cached content
53             sub empty_cache {
54 12     12 1 69 my $self = shift;
55              
56             # urgs
57 12         123 delete $self->{root_node};
58             }
59              
60             1;
61              
62             __END__