simplify_static_dir/lib/Directory/Simplify/FileHash.pm

54 lines
1.2 KiB
Perl
Raw Normal View History

2022-11-16 17:33:08 +00:00
package Directory::Simplify::FileHash;
use strict;
use warnings;
=head1 DESCRIPTION
Object for abstracting management of a hashed filesystem
=cut
# :squash-remove-start:
2022-11-16 17:33:08 +00:00
require Directory::Simplify::File;
# :squash-remove-end:
2022-11-16 17:33:08 +00:00
sub new {
2023-07-20 17:37:07 +00:00
my ($class, %args) = @_;
2022-11-16 17:33:08 +00:00
return bless {
_entries => {},
_files_in_hash => {},
2023-07-20 17:37:07 +00:00
%args,
2022-11-16 17:33:08 +00:00
}, $class;
}
sub add {
my ($self, %args) = @_;
my @files = @{$args{files}};
my $callback = $args{callback};
2022-11-16 17:33:08 +00:00
foreach my $file (@files) {
unless (ref $file eq 'Directory::Simplify::File') {
$file = Directory::Simplify::File->new($file);
}
unless ($self->{_files_in_hash}->{$file->{name}}) {
my $hash = $file->hash;
unless (defined $self->{_entries}->{$hash}) {
$self->{_entries}->{$hash} = [];
}
push @{$self->{_entries}->{$hash}}, $file;
2023-07-20 18:05:12 +00:00
if (ref $callback eq 'CODE') {
$callback->($file);
}
2022-11-16 17:33:08 +00:00
}
$self->{_files_in_hash}->{$file->{name}} = 1;
}
2023-07-20 17:37:27 +00:00
return;
2022-11-16 17:33:08 +00:00
}
sub entries {
my $self = shift;
2023-07-20 17:37:27 +00:00
return values %{$self->{_entries}};
2022-11-16 17:33:08 +00:00
}
1;