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   914 use strict;
  15         15  
  15         321  
4 15     15   43 use warnings;
  15         15  
  15         305  
5              
6 15     15   75 use base 'Mojo::Base';
  15         25  
  15         1739  
7              
8 15     15   18802 use Bootylicious::DocumentStatLoader;
  15         29  
  15         85  
9 15     15   7793 use Bootylicious::DocumentMetadataLoader;
  15         25  
  15         78  
10 15     15   5244 use Bootylicious::DocumentContentLoader;
  15         35  
  15         81  
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 822 my $self = shift;
36 96         98 my $path = shift;
37              
38 96 100       1632 return unless -e $path;
39              
40 95         242 $self->path($path);
41              
42 95         495 return $self;
43             }
44              
45 47     47 0 1917 sub name { shift->stat(name => @_) }
46 6     6 0 19 sub format { shift->stat(format => @_) }
47 0     0 0 0 sub filename { shift->stat(filename => @_) }
48 238     238 0 3255 sub created { shift->stat(created => @_) }
49 15     15 0 439 sub modified { shift->stat(modified => @_) }
50              
51 16     16 0 69 sub author { shift->metadata(author => @_) }
52              
53 20     20 0 47 sub content { shift->inner(content => @_) }
54              
55 306     306 0 394 sub stat { shift->_group(stat => @_) }
56 90     90 0 154 sub metadata { shift->_group(metadata => @_) }
57 30     30 0 50 sub inner { shift->_group(inner => @_) }
58              
59             sub _group {
60 426     426   321 my $self = shift;
61 426         313 my $group = shift;
62 426         286 my $method = shift;
63              
64 426 100       621 if (@_) {
65 22         46 $self->{$group}->{$method} = $_[0];
66 22         47 return $self;
67             }
68              
69             return $method ? $self->{$group}->{$method} : $self->{$group}
70 404 100       1373 if exists $self->{$group};
    100          
71              
72 117         152 my $group_loader = "${group}_loader";
73 117         295 $self->{$group} = $self->$group_loader->load;
74              
75 115 50       473 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 12 my $self = shift;
86 6         4 my $path = shift;
87 6         5 my $hash = shift;
88              
89 6 50 33     17 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       436 open my $file, '>:encoding(UTF-8)', $path or return;
101              
102 6         308 $self->path($path);
103              
104 6         26 my $metadata = '';
105 6         8 foreach my $key (sort keys %{$self->metadata}) {
  6         10  
106 10         16 my $value = $self->metadata->{$key};
107 10 50 33     44 next unless $value && $value ne '';
108 10         20 $metadata .= ucfirst $key . ': ' . $value;
109 10         15 $metadata .= "\n";
110             }
111              
112 6         28 print $file $metadata;
113 6         9 print $file "\n";
114 6   50     17 print $file $self->content || '';
115              
116 6         250 return $self;
117             }
118              
119             sub update {
120 1     1 0 1 my $self = shift;
121 1         1 my $hash = shift;
122              
123 1   50     6 $hash ||= {};
124 1         2 foreach my $key (keys %$hash) {
125 0         0 $self->$key($hash->{$key});
126             }
127              
128 1         3 return $self->create($self->path);
129             }
130              
131             sub delete {
132 1     1 0 1 my $self = shift;
133              
134 1         3 unlink $self->path;
135             }
136              
137             1;