Alzabo...odMaker(3pm)
π Alzabo::MethodMaker π
okay bestie, this is the manual page for Alzabo::MethodMaker, hanging out in the 3pm section of the manual like the icon it is π
and listen β the page literally opens with some raw code just vibing at the top before anything else, so here it is, verbatim, exactly as it appears, no notes π:
$self->{name}
$desc
EOF
$pod .= $params if $params;
return $pod;
}
package Alzabo::ClassDocs;
use Params::Validate qw( validate SCALAR );
use base qw(Alzabo::Docs);
sub new {
my $class = shift;
my %p = validate( @_, { group => { type => SCALAR },
description => { type => SCALAR },
} );
return bless \%p, $class;
}
sub as_pod {
my $self = shift;
return ucfirst "$self->{description}\n\n";
}
1;
__END__
β¨ NAME β¨
Alzabo::MethodMaker β auto-generate useful methods based on an existing schema. that's it, that's the whole job, and it eats π
πΈ SYNOPSIS πΈ
use Alzabo::MethodMaker ( schema => 'schema_name', all => 1 );
π¦ DESCRIPTION π¦
okay so this module takes an existing schema and generates a whole bunch of useful methods for that schema, plus its tables, plus its rows. obsessed π«Ά
the method-making situation is controlled by the parameters you hand over along with the use statement β exactly like the SYNOPSIS shows up there. β¨
π PARAMETERS π
all of these get passed to the module when it's imported via use. lowkey the whole control panel right here:
schema => $schema_nameβ this one is required, no skipping it bestie.class_root => $class_nameβ if you give it, this becomes the root of the class names this module generates. it should not end in'::'. if you don't give one, then the calling module's name gets used as the root instead. head over to New Class Names for the whole tea.all => $boolβ tells the module to make all the methods it possibly can. see METHOD CREATION OPTIONS for the details.
and real talk: if individual method creation options are set to false, that setting gets respected. so you could do
use Alzabo::MethodMaker( schema => 'foo', all => 1, tables => 0 );
and that turns on all the regular options except for tables. slay.
name_maker => \&naming_subβ if you provide this, the callback gets called every single time a method name needs to be generated, which gives you full control over the resulting names. otherwise names get generated the way the documentation describes.
the callback is expected to return a name for the method to be used. that name should not be fully qualified and should not contain any class designation β MethodMaker handles that part itself, bestie.
it is important that none of the names returned conflict with existing methods for the object the method is being added to. π
for example, when adding methods that return column objects to a table, if you have a column called 'name' and you try to use that as the method name, it won't work. Alzabo::Table objects already have such a method, and it returns the name of the table. see the relevant documentation of the schema, table, and row objects for a list of the methods they contain.
the NAMING SUB PARAMETERS section has all the details on what parameters get passed to this callback.
please note that if you have a large complex schema you will almost certainly need to provide a custom naming subroutine to avoid name conflicts. no cap, it's giving inevitable π
π₯ EFFECTS π₯
using this module has several effects on your schema's objects. buckle up π¦
π¦ New Class Names π¦
your schema, table, and row objects get blessed into subclasses of Alzabo::Runtime::Schema, Alzabo::Runtime::Table, and Alzabo::Runtime::Row, respectively. these subclasses are where all the various methods created by this module live. the new class names get formed by taking the class_root parameter and adding onto it. β¨
and to make it convenient to add new methods to the table and row classes, the created table classes are all subclasses of a new class based on your class root β and the same thing happens for all created row classes. cute, right?
- Schema
<class root>::Schema
- Tables
<class root>::Table::<table name>
all tables will be subclasses of:
<class root>::Table
- Rows
<class root>::Row::<table name>
all rows will be subclasses of:
<class root>::Row
with a root of My::MovieDB, and a schema with only two tables, Movie and Image, this would result in the following class names:
My::MovieDB::Schema
My::MovieDB::Table::Movie - subclass of My::MovieDB::Table
My::MovieDB::Row::Movie - subclass of My::MovieDB::Row
My::MovieDB::Table::Image - subclass of My::MovieDB::Table
My::MovieDB::Row::Image - subclass of My::MovieDB::Row
β¨ Loading Classes β¨
for each class into which an object is blessed, this module will attempt to load that class via a use statement. if there's no module found, that won't cause an error, don't worry π«Ά
but if that class defines any methods that have the same name as the ones this module generates, then this module will not attempt to generate them. respect the existing methods, periodt.
π METHOD CREATION OPTIONS π
when you're using Alzabo::MethodMaker, you may specify any of the following parameters. and specifying all causes all of them to be used. every last one. π
π Schema object methods π
tables => $boolβ creates methods for the schema that return the table object matching the name of the method.
for example, given a schema containing tables named Movie and Image, this would create methods that could be called as $schema->Movie and $schema->Image. iconic.
π Table object methods. π
table_columns => $boolβ creates methods for the tables that return the column object matching the name of the method. this is quite similar to thetablesoption for schemas. so if ourMovietable had a column calledtitle, we could write$schema->Movie->title. πinsert_hooks => $boolβ look for hooks to wrap around theinsert()method inAlzabo::Runtime::Table. see Loading Classes for more details. you have to define either apre_insert()and/orpost_insert()method for the generated table class, or this parameter will not do anything. see the HOOKS section for more details. π
π Row object methods π
row_columns => $boolβ this tells MethodMaker to create get/set methods for each column a row has. these methods take a single optional argument, which β if given β will cause that column to be updated for the row. two jobs, one method, we love efficiency πupdate_hooks => $boolβ look for hooks to wrap around theupdatemethod inAlzabo::Runtime::Row. see Loading Classes for more details. you have to define apre_update()and/orpost_update()method for the generated row class, or this parameter will not do anything. see the HOOKS section for more details.select_hooks => $boolβ look for hooks to wrap around theselectmethod inAlzabo::Runtime::Row. see Loading Classes for more details. you have to define either apre_select()and/orpost_select()method for the generated row class, or this parameter will not do anything. see the HOOKS section for more details.delete_hooks => $boolβ look for hooks to wrap around thedeletemethod inAlzabo::Runtime::Row. see Loading Classes for more details. you have to define either apre_delete()and/orpost_delete()method for the generated row class, or this parameter will not do anything. see the HOOKS section for more details.foreign_keys => $boolβ creates methods in row objects named for the table to which the relationship exists. these methods return either a singleAlzabo::Runtime::Rowobject or a singleAlzabo::Runtime::RowCursorobject, depending on the cardinality of the relationship.
for exa
Movie Credit
--------- --------
movie_id movie_id
title person_id
role_name
this would create a method for Movie row objects called Credit() which would return a cursor for the associated Credit table rows. similarly, Credit row objects would have a method called Movie() which would return the associated Movie row object. π₯
linking_tables => $boolβ a linking table, as defined here, is a table with a two column primary key, with each column being a foreign key to another table's primary key. these tables exist to facilitate n..n logical relationships. if bothforeign_keysandlinking_tablesare true, then methods will be created that skip the intermediate linking tables. chef's kiss π§
for example, with the following tables:
User UserGroup Group
------- --------- --------
user_id user_id group_id
user_name group_id group_name
the UserGroup table exists solely to facilitate the n..n relationship between User and Group. User row objects will have a Group() method, which returns a row cursor of Group row objects. and Group row objects will have a User() method which returns a row cursor of User row objects. π¦
lookup_columns => $boolβ lookup columns are columns in foreign tables to which a table has a many-to-one or one-to-one relationship to the foreign table's primary key. for example, given the tables below:
Restaurant Cuisine
--------- --------
restaurant_id cuisine_id
restaurant_name (n..1) description
phone spiciness
cuisine_id
in this example, Restaurant row objects would have Cuisine_description() and Cuisine_spiciness methods which returned the corresponding values from the Cuisine table. π
self_relations => $boolβ a self relation is when a table has a parent/child relationship with itself. here's an example:
Location
--------
location_id
location_name
parent_location_id
NOTE: if the relationship has a cardinality of 1..1 then no methods will be created, as this option is really intended for parent/child relationships. this may change in the future. bestie, that's the documented situation as of right now π
in this case, Location row objects will have both parent() and children() methods. the parent method returns a single row, while the children() method returns a row cursor of Location rows. πΈ
π HOOKS π
ok so like, we mentioned this before but let's get into it fr β you can create pre- and post-execution hooks that wrap around a whole bunch of methods. this is giving data validation on your inserts and updates, and it also lets you filter incoming or outgoing data however you need. for example? converting dates to and from a specific RDBMS format. iconic behavior tbh. β¨
every single hook lives inside a transaction, and that transaction gets rolled back if any part of the process fails. no half-finished nonsense here bestie. π«Ά
ngl there's a catch you need to know about: Alzabo uses both theAlzabo::Runtime::Row->selectandAlzabo::Runtime::Row->deletemethods internally. if you radically alter their behavior through hooks, then some of Alzabo's functionality may end up broken. π
given that, it may be safer to just create new methods to fetch and massage data, rather than making post-select hooks that alter data. just saying. πΈ
each of these hooks receives different parameters, and we're documenting them all below:
β¨ Insert Hooks
pre_insertβ this method receives a hash reference of all the parameters that are passed to theAlzabo::Runtime::Table->insert()method.
these are the actual parameters that will be passed to the insert method, so alterations to this reference will be seen by that method. that means you can alter the values that actually end up going into the database, or change any other parameters, however you see fit. π
post_insertβ this method also receives a hash reference containing all of the parameters passed to theinsert()method. plus, the hash reference contains an additional key,row, which contains the newly created row. π
π Update Hooks
pre_updateβ this method receives a hash reference of the parameters that will be passed to theAlzabo::Runtime::Row->update()method. again, alterations to these parameters will be seen by theupdatemethod.post_updateβ this method receives the same parameters aspre_update().
π¦ Select Hooks
pre_selectβ this method receives an array reference containing the names of the requested columns. it gets called when either theAlzabo::Runtime::Row->select()orAlzabo::Runtime::Row->select_hash()methods are called.post_selectβ this method is called after theAlzabo::Runtime::Row->select()orAlzabo::Runtime::Row->select_hash()methods. it receives a hash containing the name and values returned from the relevant method, which it may modify. and if the values of this hash reference are modified, then this will be seen by the original caller. π
π Delete hooks
pre_deleteβ this method receives no parameters. that's it. that's the whole vibe. π
π NAMING SUB PARAMETERS π
the naming sub will receive a hash containing the following parameters:
type => $method_typeβ this will always be the same as one of the parameters you give to the import method. it'll be one of the following:foreign_key,linking_table,lookup_columns,row_column,self_relation,table,table_column.
the following parameters vary from case to case, depending on the value of type. π
when the type is table:
table => Alzabo::Table objectβ this parameter gets passed when the type istable. it's the table object that the schema object's method will return.
when the type is table_column or row_column:
column => Alzabo::Column objectβ when the type istable_column, this is the column object the method will return. when the type isrow_column, then it's the column whose value the method will return.
when the type is foreign_key, linking_table, or self_relation:
foreign_key => Alzabo::ForeignKey objectβ this is the foreign key that the method is based on.
it is possible to create an n..n relationship between a table and itself, and MethodMaker will attempt to generate linking table methods for such relationships, so your naming sub may need to take this into account. heads up bestie π«Ά
when the type is foreign_key:
plural => $boolβ this indicates whether or not the method being created will return a cursor object (true) or a row object (false).
when the type is linking_table:
foreign_key_2 => Alzabo::ForeignKey objectβ when making a linking table method, two foreign keys are used. theforeign_keyis from the table being linked from to the linking table. this parameter is the foreign key from the linking table to the table being linked to.
when the type is lookup_columns:
column => Alzabo::Column objectβ when making lookup column methods, this column is the column in the foreign table for which a method is being made.
when the type is self_relation:
parent => $booleanβ this indicates whether or not the method being created will return parent objects (true) or child objects (false).
π NAMING SUB EXAMPLE π
here is an example that covers all of the possible options, no cap:
use Lingua::EN::Inflect;
sub namer
{
my %p = @_;
# Table object can be returned from the schema via methods such as $schema->User_t;
return $p{table}->name . '_t' if $p{type} eq 'table';
# Column objects are returned similarly, via $schema->User_t->username_c;
return $p{column}->name . '_c' if $p{type} eq 'table_column';
# If I have a row object, I can get at the columns via their
# names, for example $user->username;
return $p{column}->name if $p{type} eq 'row_column';
# This manipulates the table names a bit to generate names. For
# example, if I have a table called UserRating and a 1..n
# relationship from User to UserRating, I'll end up with a method
# on rows in the User table called ->Ratings which returns a row
# cursor of rows from the UserRating table.
if ( $p{type} eq 'foreign_key' )
{
my $name = $p{foreign_key}->table_to->name;
my $from = $p{foreign_key}->table_from->name;
$name =~ s/$from//;
if ($p{plural})
{
return my_PL( $name );
}
else
{
return $name;
}
}
# This is very similar to how foreign keys are handled. Assume
# we have the tables Restaurant, Cuisine, and RestaurantCuisine.
# If we are generating a method for the link from Restaurant
# through to Cuisine, we'll have a method on Restaurant table
# rows called ->Cuisines, which will return a cursor of rows from
# the Cuisine table.
#
# Note: this will generate a bad name if given a linking table
# that links a table to itself.
if ( $p{type} eq 'linking_table' )
{
my $method = $p{foreign_key}->table_to->name;
my $tname = $p{foreign_key}->table_from->name;
$method =~ s/$tname//;
return my_PL($method);
}
# Lookup columns are columns if foreign tables for which there
# exists a one-to-one or many-to-one relationship. In cases such
# as these, it is often the case that the foreign table is rarely
# used on its own, but rather it primarily used as a lookup table
# for values that should appear to be part of other tables.
#
# For example, an Address table might have a many-to-one
# relationship with a State table. The State table would contain
# the columns 'name' and 'abbreviation'. If we have
# an Address table row, it is convenient to simply be able to say
# $address->state_name and $address->state_abbreviation.
if ( $p{type} eq 'lookup_columns' )
{
return join '_', map { lc $_->name } $p{foreign_key}->table_to, $p{column};
}
# This should be fairly self-explanatory.
return $p{parent} ? 'parent' : 'children'
if $p{type} eq 'self_relation';
# And just to make sure that nothing slips by us we do this.
die "unknown type in call to naming sub: $p{type}\n";
}
# Lingua::EN::Inflect did not handle the word 'hours' properly when this was written
sub my_PL
{
my $name = shift;
return $name if $name =~ /hours$/i;
return Lingua::EN::Inflect::PL($name);
}
π GENERATED DOCUMENTATION π
this module keeps track of the methods that are generated, and can in turn generate basic POD for those methods. it's giving self-documenting, we love to see it. β¨
any schema that has had methods generated for it by Alzabo::MethodMaker will have an additional method, docs_as_pod. this returns documentation for the schema object's methods, plus any documentation available for objects that the schema contains β in this case, tables. and the tables in turn return their own documentation plus that of their contained row classes. it's a whole cascade bestie, so satisfying. π¦
it's also possible to call the docs_as_pod method on any generated table or row class individually. main character energy for every class fr fr. π
a simple script like the following can be used to send all of the generated documentation to STDOUT:
use Alzabo::MethodMaker ( schema => 'foo', all => 1 );
my $s = Alzabo::Runtime::Schema->load_from_file( name => 'foo' );
print $s->docs_as_pod;
πΈ AUTHOR πΈ
Dave Rolsky, <autarch@urth.org> β the icon who wrote this whole thing, we stan π
and that's a wrap on part 2 bestie!! π this beauty shipped as part of perl v5.34.0, dated 2022-06-14, and it's filed away in the User Contributed Perl Documentation under Alzabo...odMaker(3pm). go forth and make some methods, you've absolutely got this. slay π
β¨
π¦ where this page comes from
- debian bookworm: libalzabo-perl 0.92-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libalzabo-perl 0.92-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libalzabo-perl 0.92-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libalzabo-perl 0.92-6 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libalzabo-perl 0.92-6 Β· 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