simplify_static_dir/lib/Directory/Simplify/FileHash.pm

59 lines
1.3 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 = shift;
my (@files, $callback);
if (ref $_[0] eq 'HASH') {
# Called method like { files => [] }
my %opts = %{$_[0]};
@files = @{$opts{files}};
$callback = $opts{callback};
} else {
@files = @_;
}
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;