blib/lib/Yukki/Model/File.pm

filter: Statementstmt 25.0% 40 / 160 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 33.3% 11 / 33 Podpod 100% 22 / 22 totaltotal 27.1% 73 / 269 CC 29 SCAR 28.7
CC
29
Cov
19%
store
56.0
title
42.0
rename
20.0
CRAP 17.6
« blib/lib/Yukki/Web/Plugin/YukkiText.pm ↑ Summary blib/lib/Yukki/Model/Root.pm »
1 package Yukki::Model::File;
2 6 $Yukki::Model::File::VERSION = '0.991_007'; # TRIAL
3 6 $Yukki::Model::File::VERSION = '0.991007';
4 6 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use Class::Load;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Digest::SHA1 qw( sha1_hex );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Number::Bytes::Human qw( format_bytes );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use LWP::MediaTypes qw( guess_media_type );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Type::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Types::Standard qw( Maybe Str );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Yukki::Error qw( http_throw );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 6 extends 'Yukki::Model';
19  
20 # ABSTRACT: the model for loading and saving files in the wiki
21  
22  
23 6 has path => (
24 is => 'ro',
25 isa => Str,
26 required => 1,
27 );
28  
29  
30 6 has filetype => (
31 is => 'ro',
32 isa => Maybe[Str],
33 required => 1,
34 default => 'yukki',
35 );
36  
37  
38 6 has repository => (
39 is => 'ro',
40 isa => class_type('Yukki::Model::Repository'),
41 required => 1,
42 handles => {
43 'make_blob' => 'make_blob',
44 'make_blob_from_file' => 'make_blob_from_file',
45 'find_root' => 'find_root',
46 'branch' => 'branch',
47 'show' => 'show',
48 'make_tree' => 'make_tree',
49 'commit_tree' => 'commit_tree',
50 'update_root' => 'update_root',
51 'find_path' => 'find_path',
52 'fetch_date' => 'fetch_date',
53 'fetch_size' => 'fetch_size',
54 'repository_name' => 'name',
55 'author_name' => 'author_name',
56 'author_email' => 'author_email',
57 'log' => 'log',
58 'diff_blobs' => 'diff_blobs',
59 },
60 );
61  
62  
63 6 has revision => (
64 is => 'ro',
65 isa => Str,
66 required => 1,
67 default => 'HEAD',
68 );
69  
70  
71  
72 0 ▶sub BUILDARGS {
Subroutine BUILDARGS: not called CC=3 SCAR=24.8
Pod documented
73 0 my $class = shift;
74  
75 0 my %args;
76 0 ▶ if (@_ == 1) { %args = %{ $_[0] }; }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
77 0 else { %args = @_; }
78  
79 0 ▶ if ($args{full_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"full_path"}) 0 0 0
80 0 my $full_path = delete $args{full_path};
81  
82 0 my ($path, $filetype) = $full_path =~ m{^(.*)(?:\.(\w+))?$};
83  
84 0 $args{path} = $path;
85 0 $args{filetype} = $filetype;
86 }
87  
88 0 return \%args;
89 }
90  
91  
92 0 ▶sub full_path {
Subroutine full_path: not called CC=2 SCAR=17.9
Pod documented
93 0 my $self = shift;
94  
95 0 my $full_path;
96 0 ▶ if (defined $self->filetype) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->filetype) { } 0 0 0
97 0 $full_path = join '.', $self->path, $self->filetype;
98 }
99 else {
100 0 $full_path = $self->path;
101 }
102  
103 0 return $full_path;
104 }
105  
106  
107 0 ▶sub file_name {
Subroutine file_name: not called CC=1 SCAR=6.9
Pod documented
108 0 my $self = shift;
109 0 my $full_path = $self->full_path;
110 0 my ($file_name) = $full_path =~ m{([^/]+)$};
111 0 return $file_name;
112 }
113  
114  
115 0 ▶sub file_id {
Subroutine file_id: not called CC=1 SCAR=6.9
Pod documented
116 0 my $self = shift;
117 0 return sha1_hex($self->file_name);
118 }
119  
120  
121 0 ▶sub object_id {
Subroutine object_id: not called CC=1 SCAR=6.9
Pod documented
122 0 my $self = shift;
123 0 return $self->find_path($self->full_path, $self->revision);
124 }
125  
126  
127 0 ▶sub title {
Subroutine title: not called CC=6 SCAR=37.4
Pod documented
128 0 my $self = shift;
129  
130 0 ▶ if ($self->filetype eq 'yukki') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->filetype eq "yukki") 0 0 0
131 0 LINE: for my $line ($self->fetch) {
132 0 ▶ if ($line =~ /^#\s*(.*)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^#\s*(.*)$/u) { } 0 0 0
elsif ($line =~ /:/u) { } 0 0 0
133 0 return $1;
134 }
135 elsif ($line =~ /:/) {
136 0 my ($name, $value) = split m{\s*:\s*}, $line, 2;
137 0 ▶ return $value if lc($name) eq 'title';
Branch: 1 branch
BranchTrueFalseTotal
if lc $name eq "title" 0 0 0
138 }
139 else {
140 0 last LINE;
141 }
142 }
143 }
144  
145 0 my $title = $self->file_name;
146 0 $title =~ s/\.(\w+)$//g;
147 0 return $title;
148 }
149  
150  
151 0 ▶sub file_date {
Subroutine file_date: not called CC=1 SCAR=6.9
Pod documented
152 0 my ($self, $format) = @_;
153 0 return $self->fetch_date($self->full_path, $self->revision, $format);
154 }
155  
156  
157 0 ▶sub file_size {
Subroutine file_size: not called CC=1 SCAR=6.9
Pod documented
158 0 my $self = shift;
159 0 return $self->fetch_size($self->full_path, $self->revision);
160 }
161  
162  
163 0 ▶sub formatted_file_size {
Subroutine formatted_file_size: not called CC=1 SCAR=6.9
Pod documented
164 0 my $self = shift;
165 0 return format_bytes($self->file_size);
166 }
167  
168  
169 0 ▶sub media_type {
Subroutine media_type: not called CC=1 SCAR=6.9
Pod documented
170 0 my $self = shift;
171 0 return guess_media_type($self->full_path);
172 }
173  
174  
175 0 ▶sub store {
Subroutine store: not called CC=7 SCAR=40.3
Pod documented
176 0 my ($self, $params) = @_;
177 0 my $path = $self->full_path;
178  
179 0 my (@parts) = split m{/}, $path;
180 0 my $blob_name = $parts[-1];
181  
182 0 my $object_id;
183 0 ▶ if ($params->{content}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($params->{'content'}) { } 0 0 0
elsif ($params->{'filename'}) { } 0 0 0
184 0 $object_id = $self->make_blob($blob_name, $params->{content});
185 }
186 elsif ($params->{filename}) {
187 0 $object_id = $self->make_blob_from_file($blob_name, $params->{filename});
188 }
189 0 ▶ http_throw("unable to create blob for $path") unless $object_id;
Branch: 1 branch
BranchTrueFalseTotal
unless $object_id 0 0 0
190  
191 0 my $old_tree_id = $self->find_root;
192 0 ▶ http_throw("unable to locate original tree ID for ".$self->branch)
Branch: 1 branch
BranchTrueFalseTotal
unless $old_tree_id 0 0 0
193 unless $old_tree_id;
194  
195 0 my $new_tree_id = $self->make_tree($old_tree_id, \@parts, $object_id);
196 0 ▶ http_throw("unable to create the new tree containing $path\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $new_tree_id 0 0 0
197 unless $new_tree_id;
198  
199 0 my $commit_id = $self->commit_tree($old_tree_id, $new_tree_id, $params->{comment});
200 0 ▶ http_throw("unable to commit the new tree containing $path\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $commit_id 0 0 0
201 unless $commit_id;
202  
203 0 $self->update_root($old_tree_id, $commit_id);
204 }
205  
206  
207 0 ▶sub rename {
Subroutine rename: not called CC=4 SCAR=30.0
Pod documented
208 0 my ($self, $params) = @_;
209 0 my $old_path = $self->full_path;
210  
211 0 my (@new_parts) = split m{/}, $params->{full_path};
212 0 my (@old_parts) = split m{/}, $old_path;
213 0 my $blob_name = $old_parts[-1];
214  
215 0 my $object_id = $self->object_id;
216  
217 0 my $old_tree_id = $self->find_root;
218 0 ▶ http_throw("unable to locate original tree ID for ".$self->branch)
Branch: 1 branch
BranchTrueFalseTotal
unless $old_tree_id 0 0 0
219 unless $old_tree_id;
220  
221 0 my $new_tree_id = $self->make_tree(
222 $old_tree_id, \@old_parts, \@new_parts, $object_id);
223 0 ▶ http_throw("unable to create the new tree renaming $old_path to $params->{full_path}\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $new_tree_id 0 0 0
224 unless $new_tree_id;
225  
226 0 my $commit_id = $self->commit_tree($old_tree_id, $new_tree_id, $params->{comment});
227 0 ▶ http_throw("unable to commit the new tree renaming $old_path to $params->{full_path}\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $commit_id 0 0 0
228 unless $commit_id;
229  
230 0 $self->update_root($old_tree_id, $commit_id);
231  
232 return Yukki::Model::File->new(
233 app => $self->app,
234 repository => $self->repository,
235 full_path => $params->{full_path},
236 0 );
237 }
238  
239  
240 0 ▶sub remove {
Subroutine remove: not called CC=4 SCAR=30.0
Pod documented
241 0 my ($self, $params) = @_;
242 0 my $old_path = $self->full_path;
243  
244 0 my (@old_parts) = split m{/}, $old_path;
245  
246 0 my $old_tree_id = $self->find_root;
247 0 ▶ http_throw("unable to locate original tree ID for ".$self->branch)
Branch: 1 branch
BranchTrueFalseTotal
unless $old_tree_id 0 0 0
248 unless $old_tree_id;
249  
250 0 my $new_tree_id = $self->make_tree($old_tree_id, \@old_parts);
251 0 ▶ http_throw("unable to create the new tree removing $old_path\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $new_tree_id 0 0 0
252 unless $new_tree_id;
253  
254 0 my $commit_id = $self->commit_tree($old_tree_id, $new_tree_id, $params->{comment});
255 0 ▶ http_throw("unable to commit the new tree removing $old_path\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $commit_id 0 0 0
256 unless $commit_id;
257  
258 0 $self->update_root($old_tree_id, $commit_id);
259 }
260  
261  
262 0 ▶sub exists {
Subroutine exists: not called CC=1 SCAR=6.9
Pod documented
263 0 my $self = shift;
264  
265 0 my $path = $self->full_path;
266 0 return $self->find_path($path, $self->revision);
267 }
268  
269  
270 0 ▶sub fetch {
Subroutine fetch: not called CC=2 SCAR=17.9
Pod documented
271 0 my $self = shift;
272  
273 0 my $path = $self->full_path;
274 0 my $object_id = $self->find_path($path, $self->revision);
275  
276 0 ▶ return unless defined $object_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $object_id 0 0 0
277  
278 0 return $self->show($object_id);
279 }
280  
281  
282 0 ▶sub has_format {
Subroutine has_format: not called CC=4 SCAR=30.0
Pod documented
283 0 my ($self, $media_type) = @_;
284 0 ▶ $media_type //= $self->media_type;
Condition0%0/3
$media_type //= $self->media_type
l!l&&r!l&&!r
000
MC/DC0%0/2
$media_type //= $self->media_type
$media_type $self->media_type
Truth table0%0/3
A //= B
A= $media_typeB= $self->media_type
ABresult
1X1
011
000
285  
286 0 my @formatters = $self->app->formatter_plugins;
287 0 for my $formatter (@formatters) {
288 0 ▶ return 1 if $formatter->has_format($media_type);
Branch: 1 branch
BranchTrueFalseTotal
if $formatter->has_format($media_type) 0 0 0
289 }
290  
291 0 return '';
292 }
293  
294  
295 0 ▶sub fetch_formatted {
Subroutine fetch_formatted: not called CC=4 SCAR=30.0
Pod documented
296 0 my ($self, $ctx, $position) = @_;
297 0 ▶ $position //= 0;
Condition0%0/2
$position //= 0
l!l
00
MC/DC0%0/1
$position //= 0
$position
Truth table0%0/2
A
A= $position
Aresult
11
00
298  
299 0 my $media_type = $self->media_type;
300  
301 0 my $formatter;
302 0 for my $plugin ($self->app->formatter_plugins) {
303 0 ▶ return $plugin->format({
Branch: 1 branch
BranchTrueFalseTotal
if $plugin->has_format($media_type) 0 0 0
304 context => $ctx,
305 file => $self,
306 position => $position,
307 }) if $plugin->has_format($media_type);
308 }
309  
310 0 return $self->fetch;
311 }
312  
313  
314 0 ▶sub history {
Subroutine history: not called CC=1 SCAR=6.9
Pod documented
315 0 my $self = shift;
316 0 return $self->log($self->full_path);
317 }
318  
319  
320 0 ▶sub diff {
Subroutine diff: not called CC=1 SCAR=6.9
Pod documented
321 0 my ($self, $object_id_1, $object_id_2) = @_;
322 0 return $self->diff_blobs($self->full_path, $object_id_1, $object_id_2);
323 }
324  
325  
326 0 ▶sub file_preview {
Subroutine file_preview: not called CC=1 SCAR=6.9
Pod documented
327 0 my ($self, %params) = @_;
328  
329 0 Class::Load::load_class('Yukki::Model::FilePreview');
330 0 return Yukki::Model::FilePreview->new(
331 %params,
332 app => $self->app,
333 repository => $self->repository,
334 path => $self->path,
335 );
336 }
337  
338  
339 0 ▶sub list_files {
Subroutine list_files: not called CC=1 SCAR=6.9
Pod documented
340 0 my ($self) = @_;
341 0 return $self->repository->list_files($self->path, $self->revision);
342 }
343  
344  
345 0 ▶sub parent {
Subroutine parent: not called CC=2 SCAR=17.9
Pod documented
346 0 my $self = shift;
347  
348 0 my @parts = split m{/}, $self->path;
349 0 ▶ return if @parts == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @parts == 1 0 0 0
350  
351 0 pop @parts;
352 0 return Yukki::Model::File->new(
353 app => $self->app,
354 repository => $self->repository,
355 path => join('/', @parts),
356 );
357 }
358  
359  
360 6 1;
361  
362 __END__
« blib/lib/Yukki/Web/Plugin/YukkiText.pm ↑ Summary blib/lib/Yukki/Model/Root.pm »