File Coverage

blib/lib/Bootylicious/Document.pm
Criterion Covered Total %
statement 66 73 90.4
branch 12 16 75.0
condition 4 10 40.0
subroutine 20 22 90.9
pod 0 15 0.0
total 102 136 75.0


line stmt bran cond sub pod time code
1             package Bootylicious::Document;
2              
3 15     15   1442 use strict;
  15         23  
  15         558  
4 15     15   73 use warnings;
  15         21  
  15         520  
5              
6 15     15   111 use base 'Mojo::Base';
  15         29  
  15         2322  
7              
8 15     15   23803 use Bootylicious::DocumentStatLoader;
  15         44  
  15         118  
9 15     15   8427 use Bootylicious::DocumentMetadataLoader;
  15         34  
  15         126  
10 15     15   6419 use Bootylicious::DocumentContentLoader;
  15         51  
  15         98  
11              
12             require Carp;
13              
14             __PACKAGE__->attr('path');
15              
16             __PACKAGE__->attr(
17             stat_loader => sub {
18             Bootylicious::DocumentStatLoader->new(path => shift->path);
19             }
20             );
21              
22             __PACKAGE__->attr(
23             metadata_loader => sub {
24             Bootylicious::DocumentMetadataLoader->new(path => shift->path);
25             }
26             );
27              
28             __PACKAGE__->attr(
29             inner_loader => sub {
30             Bootylicious::DocumentContentLoader->new(path => shift->path);
31             }
32             );
33              
34             sub load {
35 96     96 0 1305 my $self = shift;
36 96         5401 my $path = shift;
37              
38 96 100       2881 return unless -e $path;
39              
40 95         428 $self->path($path);
41              
42 95         834 return $self;
43             }
44              
45 47     47 0 3257 sub name { shift->stat(name => @_) }
46 6     6 0 40 sub format { shift->stat(format => @_) }
47 0     0 0 0 sub filename { shift->stat(filename => @_) }
48 238     238 0 5169 sub created { shift->stat(created => @_) }
49 15     15 0 707 sub modified { shift->stat(modified => @_) }
50              
51 16     16 0 194 sub author { shift->metadata(author => @_) }
52              
53 20     20 0 97 sub content { shift->inner(content => @_) }
54              
55 306     306 0 742 sub stat { shift->_group(stat => @_) }
56 90     90 0 269 sub metadata { shift->_group(metadata => @_) }
57 30     30 0 79 sub inner { shift->_group(inner => @_) }
58              
59             sub _group {
60 426     426   496 my $self = shift;
61 426         508 my $group = shift;
62 426         425 my $method = shift;
63              
64 426 100       1162 if (@_) {
65 22         70 $self->{$group}->{$method} = $_[0];
66 22         73 return $self;
67             }
68              
69 404 100       2177 return $method ? $self->{$group}->{$method} : $self->{$group}
    100          
70             if exists $self->{$group};
71              
72 117         235 my $group_loader = "${group}_loader";
73 117         468 $self->{$group} = $self->$group_loader->load;
74              
75 115 50       723 return $method ? $self->{$group}->{$method} : $self->{$group};
76             }
77              
78             sub is_modified {
79 0     0 0 0 my $self = shift;
80              
81 0         0 return $self->created != $self->modified;
82             }
83              
84             sub create {
85 6     6 0 20 my $self = shift;
86 6         11 my $path = shift;
87 6         11 my $hash = shift;
88              
89 6 50 33     23 if ($hash && ref $hash eq 'HASH') {
90 0         0 foreach my $key (keys %$hash) {
91 0         0 $self->$key($hash->{$key});
92             }
93              
94 0         0 $path .= '/'.
95             Bootylicious::Timestamp->new(epoch => time)->timestamp . '-'
96             . $self->name . '.'
97             . $self->format;
98             }
99              
100 6 50       746 open my $file, '>:encoding(UTF-8)', $path or return;
101              
102 6         458 $self->path($path);
103              
104 6         45 my $metadata = '';
105 6         10 foreach my $key (sort keys %{$self->metadata}) {
  6         18  
106 10         23 my $value = $self->metadata->{$key};
107 10 50 33     70 next unless $value && $value ne '';
108 10         36 $metadata .= ucfirst $key . ': ' . $value;
109 10         21 $metadata .= "\n";
110             }
111              
112 6         72 print $file $metadata;
113 6         17 print $file "\n";
114 6   50     27 print $file $self->content || '';
115              
116 6         464 return $self;
117             }
118              
119             sub update {
120 1     1 0 3 my $self = shift;
121 1         1 my $hash = shift;
122              
123 1   50     9 $hash ||= {};
124 1         6 foreach my $key (keys %$hash) {
125 0         0 $self->$key($hash->{$key});
126             }
127              
128 1         4 return $self->create($self->path);
129             }
130              
131             sub delete {
132 1     1 0 3 my $self = shift;
133              
134 1         4 unlink $self->path;
135             }
136              
137             1;