simplify_static_dir/lib/Directory/Simplify/FileHash.pm

52 lines
1.1 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 17:41:07 +00:00
$callback->($file) if ref $callback eq 'CODE';
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;