simplify_static_dir/lib/Directory/Simplify/FileHash.pm
Dan Church 4d4edd5e9d
Move library code to lib/
Fixes accidental inclusion of test instrumentation into the all-in-one
script.
2023-06-29 13:01:29 -05:00

57 lines
1.3 KiB
Perl

package Directory::Simplify::FileHash;
use strict;
use warnings;
=head1 DESCRIPTION
Object for abstracting management of a hashed filesystem
=cut
# :squash-remove-start:
require Directory::Simplify::File;
# :squash-remove-end:
sub new {
my $class = shift;
return bless {
_entries => {},
_files_in_hash => {},
@_,
}, $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;
&{$callback}($file) if ref $callback eq 'CODE';
}
$self->{_files_in_hash}->{$file->{name}} = 1;
}
}
sub entries {
my $self = shift;
values %{$self->{_entries}}
}
1;