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   905 use strict;
  15         29  
  15         337  
4 15     15   65 use warnings;
  15         36  
  15         348  
5              
6 15     15   81 use base 'Mojo::Base';
  15         40  
  15         1470  
7              
8 15     15   17305 use Bootylicious::DocumentStatLoader;
  15         43  
  15         81  
9 15     15   4984 use Bootylicious::DocumentMetadataLoader;
  15         44  
  15         81  
10 15     15   4706 use Bootylicious::DocumentContentLoader;
  15         45  
  15         114  
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 1149 my $self = shift;
36 96         185 my $path = shift;
37              
38 96 100       1553 return unless -e $path;
39              
40 95         370 $self->path($path);
41              
42 95         723 return $self;
43             }
44              
45 47     47 0 2503 sub name { shift->stat(name => @_) }
46 6     6 0 26 sub format { shift->stat(format => @_) }
47 0     0 0 0 sub filename { shift->stat(filename => @_) }
48 238     238 0 4772 sub created { shift->stat(created => @_) }
49 15     15 0 562 sub modified { shift->stat(modified => @_) }
50              
51 16     16 0 87 sub author { shift->metadata(author => @_) }
52              
53 20     20 0 116 sub content { shift->inner(content => @_) }
54              
55 306     306 0 631 sub stat { shift->_group(stat => @_) }
56 90     90 0 221 sub metadata { shift->_group(metadata => @_) }
57 30     30 0 72 sub inner { shift->_group(inner => @_) }
58              
59             sub _group {
60 426     426   632 my $self = shift;
61 426         664 my $group = shift;
62 426         596 my $method = shift;
63              
64 426 100       961 if (@_) {
65 22         63 $self->{$group}->{$method} = $_[0];
66 22         87 return $self;
67             }
68              
69             return $method ? $self->{$group}->{$method} : $self->{$group}
70 404 100       1736 if exists $self->{$group};
    100          
71              
72 117         244 my $group_loader = "${group}_loader";
73 117         379 $self->{$group} = $self->$group_loader->load;
74              
75 115 50       542 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 19 my $self = shift;
86 6         9 my $path = shift;
87 6         12 my $hash = shift;
88              
89 6 50 33     19 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       493 open my $file, '>:encoding(UTF-8)', $path or return;
101              
102 6         433 $self->path($path);
103              
104 6         44 my $metadata = '';
105 6         12 foreach my $key (sort keys %{$self->metadata}) {
  6         15  
106 10         27 my $value = $self->metadata->{$key};
107 10 50 33     51 next unless $value && $value ne '';
108 10         30 $metadata .= ucfirst $key . ': ' . $value;
109 10         20 $metadata .= "\n";
110             }
111              
112 6         34 print $file $metadata;
113 6         13 print $file "\n";
114 6   50     21 print $file $self->content || '';
115              
116 6         311 return $self;
117             }
118              
119             sub update {
120 1     1 0 3 my $self = shift;
121 1         2 my $hash = shift;
122              
123 1   50     7 $hash ||= {};
124 1         3 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 2 my $self = shift;
133              
134 1         4 unlink $self->path;
135             }
136              
137             1;