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;