summaryrefslogtreecommitdiff
path: root/lib/AniDB/Manager.pm
blob: eeb724f3c9417f91a28e6dd3b5df776cc26d55bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package AniDB::Manager; 
use 5.024;
use experimental 'signatures';
use Moo;
use Log::Any '$log';
use Path::Tiny;
use namespace::clean;
 
has datastore => (
    is => 'ro',
    required => 1,
    handles => [qw(update)],
);
 
around BUILDARGS => sub($orig,$class,@args) {
    my $args = $class->$orig(@args);
    if (my $db_name = $args->{database}) {
        require AniDB::Datastore;
        $args->{datastore} = AniDB::Datastore->new({
            database => $db_name,
        });
    }
    return $args;
};
 
has renamer => (
    is => 'ro',
    lazy => 1,
);
 
sub _build_renamer {
    require AniDB::Renamer;
    return AniDB::Renamer->new();
}
 
sub new_name_for($self,$path) {
    $path = path($path)->realpath;
    my $full_info = $self->datastore->full_info_for($path);
    return $self->renamer->new_name_for($path,$full_info);
}
 
sub maybe_new_name_for($self,$path) {
    my $new_name = $self->new_name_for($path);
    return if $new_name eq $path;
    return $new_name;
}
 
sub rename($self,$path,$new_path) {
    $path = path($path)->realpath;
    $new_path ||= $self->new_name_for($path);
    if ($path->move($new_path)) {
        $self->datastore->rename($path,$new_path);
        return 1;
    }
    else {
        $log->errorf(
            'failed to rename %s to %s',
            $path$new_path,
        );
        return;
    }
}
 
1;