Catman...yIndex(3pm)
π NAME π
So bestie, gather round β this is Catmandu::DirectoryIndex, a base role (yes a role, not a class, we're fancy) that stores the relations between id's and directories. That's it, that's the whole vibe. πΈ
It's a Catmandu::DirectoryIndex situation, part of the User Contributed Perl Documentation crew, and it's giving "I hold your directory mappings together" energy. π
β¨ SYNOPSIS β¨
Okay so here's the whole thing in action, no cap:
package MyDirectory;
use Moo;
use File::Spec;
use File:Basename;
use Path::Tiny qw(path);
with "Catmandu::DirectoryIndex";
# translate id to directory
sub _to_path {
my ( $self, $id ) = @_;
File::Spec->catdir( $self->base_dir(), $id );
}
sub get {
my ( $self, $id ) = @_;
my $path = $self->_to_path( $id );
is_string( $path ) && -d $path ? { _id => $id, _path => $path } : undef;
}
sub add {
my ( $self, $id ) = @_;
my $path = $self->_to_path( $id );
path( $path )->mkpath unless -d $path;
{ _id => $id, _path => $path };
}
sub delete {
my ( $self, $id ) = @_;
my $path = $self->_to_path( $id );
if ( is_string( $path ) && -d $path ) {
path( $path )->remove_tree();
}
}
sub delete_all {
path( $_[0]->base_dir )->remove_tree({ keep_root => 1 });
}
# return a generator that returns list of records, that maps _id and _path
sub generator {
my $self = $_[0];
return sub {
state $records;
if ( !defined( $records ) ) {
$records = [];
opendir my $dh, $self->base_dir() or die($!);
while( my $entry = readdir( $dh ) ){
if ( -d $entry ) {
push @$records, {
_id => $entry,
_path => File::Spec->catfile( $self->base_dir, $entry )
};
}
}
closedir $dh;
}
shift( @$records );
};
}
package main;
my $p = MyDirectory->new( base_dir => "/tmp" );
Catmandu->store->bag->each(sub {
my $r = $_[0];
my $mapping = $p->get( $r->{_id} ) || $p->add( $r->{_id} );
say $id . " => " . $mapping->{path};
});
π CLASS METHODS AVAILABLE π
new( base_dir => $base_dir )
base_dirβ the base directory where the files are stored. Required, bestie. No base_dir, no party. π
π METHODS AVAILABLE π
base_dirβ that's the one, that's the method. Iconic. π
π¦ METHODS TO IMPLEMENT π¦
Listen up, because if you're building on this role, you must implement these methods. It's giving homework, but make it cute. π
add( $id ) : $mapping
- Accepts
$idas string - Translates
$idto directory path - Creates directory if necessary
- Returns HASH with keys
"_id"and"_path"
This method should throw an Catmandu::Error when it detects an invalid id.
It should either return the mapping or throw an error. No in-between, no lukewarm energy. β
get( $id ) : $mapping
- Accepts
$idas string - Translates
$idto directory path - Returns HASH with keys
"_id"and"_path", if a path exists for$id
This method should throw an Catmandu::Error when it detects an invalid id.
Difference with method "add":
- no directory created
- no mapping returned if no existing directory could be found
So like, get is the chill one that doesn't make new stuff, and add is the one out here manifesting directories into existence. π
delete ( $id )
- Accepts id as string
- Translates id to directory
- Removes directory if it exists
- Do other internal cleanup actions if any required
This method should throw an Catmandu::Error when at failure.
delete_all()
- Deletes files/directories in
base_dir. Please keep thebase_dir. - Do other internal cleanup actions if any required
This method should throw an Catmandu::Error when at failure.
generator()
Inherited requirement from Catmandu::Iterable:
- return function reference
- every call to this function must return the next directory entry in the index as a HASH with keys
"_id"and"path"
π§ INHERITED METHODS π§
This Catmandu::DirectoryIndex inherits:
Catmandu::Iterableβ so all functions fromCatmandu::Iterableare available to these objects. Free stuff, we love to see it. π«Ά
π SEE ALSO π
Catmandu::Store::File::Simple,Catmandu::DirectoryIndex::UUID,Catmandu::DirectoryIndex::Number,Catmandu::DirectoryIndex::Map
Okay that's the whole page, you ate that. Go forth and index your directories, main character. πβ¨
This lil masterpiece is served up by perl v5.36.0 and dated 2022-12-10 β so yes, it's vintage but it's still slaying. Bye bestie! π ππ¦
π¦ where this page comes from
- debian bookworm: libcatmandu-filestore-perl 1.16-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libcatmandu-filestore-perl 1.16-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms