Jifty:...Plugin(3pm)

πŸ“¦
version 0.78 Β· 2 versions
  • 0.78 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.78 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Jifty::DBI::Record::Plugin πŸ’–

okay bestie, so this is Jifty:...Plugin(3pm) from the User Contributed Perl Documentation corner of the universe, and it's literally the plug for Record model mixins in Jifty::DBI. no cap.

✨ NAME ✨

Jifty::DBI::Record::Plugin β€” record model mixins for Jifty::DBI. that's it, that's the whole vibe. πŸ’…


🌸 SYNOPSIS 🌸

okay so here's the whole thing in action, don't panic, it's giving very much "define a mixin, then use the mixin":

# Define a mixin
package MyApp::FavoriteColor;
use base qw/ Jifty::DBI::Record::Plugin /;

# Define which methods you want to put in the host model
our @EXPORT = qw(
    favorite_complementary_color
);

use Jifty::DBI::Schema;
use Jifty::DBI::Record schema {
    column favorite_color =>
        type is 'text',
        label is 'Favorite Color',
        valid_values are qw/ red green blue yellow /;
};

sub favorite_complementary_color {
    my $self = shift; # whatever host object thing we've mixed with
    my $color = $self->favorite_color;
    return $color eq 'red'    ? 'green'
         : $color eq 'green'  ? 'red'
         : $color eq 'blue'   ? 'orange'
         : $color eq 'yellow' ? 'purple'
         :                      undef;
}

# Use the mixin
package MyApp::Model::User;

use Jifty::DBI::Schema;
use Jifty::DBI::Record schema {
    column name =>
        type is 'text',
        label is 'Name';
};

# Mixins
use MyApp::FavoriteColor;

sub name_and_color {
    my $self  = shift;
    my $name  = $self->name;
    my $color = $self->favorite_color;

    return "The favorite color of $name is $color.";
}

sub name_and_complementary_color {
    my $self  = shift;
    my $name  = $self->name;
    my $color = $self->favorite_complementary_color;

    return "The complement of $name's favorite color is $color.";
}

like. iconic. πŸŽ€


πŸ¦‹ DESCRIPTION πŸ¦‹

so by using this package you can provide models that are built out of one or more mixins. and honestly? your whole entire table could be defined inside the mixins, with not a single column declared in the model class itself. main character energy fr fr.

πŸ“ MODEL MIXINS πŸ“

to build a mixin, you literally just create a model that inherits from this package, Jifty::DBI::Record::Plugin. then you add the schema definitions you want to be inherited. easy:

package MyApp::FasterSwallow;
use base qw/ Jifty::DBI::Record::Plugin /;

use Jifty::DBI::Schema;
use Jifty::DBI::Record schema {
    column swallow_type =>
        type is 'text',
        valid are qw/ african european /,
        default is 'african';
};

πŸ’œ @EXPORT

a mixin can define an @EXPORT variable, and it works exactly as advertised in Exporter. translation: you hand it the name of any methods or variable names living in the mixin, and the host model just... gains those methods. slay.

our @EXPORT = qw( autocomplete_swallow_type );

sub autocomplete_swallow_type {
    my $self  = shift;
    my $value = quotemeta(shift);

    # You should probably find a better way than actually doing this...

    my @values;
    push @values, 'african'  if 'african'  =~ /$value/;
    push @values, 'european' if 'european' =~ /$value/;

    return @values;
}

so that way, if you've got any custom methods you wanna throw into the host model, just define them in the mixin and pop them into the @EXPORT variable. periodt. πŸ’«

🧚 register_triggers

your mixin might also wanna register triggers for the records it's gonna get added to. you do that by defining a method named register_triggers:

sub register_triggers {
    my $self = shift;
    $self->add_trigger(
        name      => 'before_create',
        callback  => \&before_create,
        abortable => 1,
    );
}

sub before_create {
    # do something...
}

see Class::Trigger. πŸ‘‘

🌈 register_triggers_for_column

and on top of the general register_triggers method we just talked about, the mixin can also implement a register_triggers_for_column method. this one gets called for each column in the table. it's mainly helpful for registering the after_set_* and before_set_* columns.

for example:

sub register_triggers_for_column {
    my $self   = shift;
    my $column = shift;

    return unless $column ne 'updated_on';

    $self->add_trigger(
        name      => 'after_set_'.$column,
        callback  => \&touch_update_time,
        abortable => 1,
    );
}

sub touch_update_time {
    my $self = shift;
    $self->set_updated_on(DateTime->now);
}

bonus perk: this is callable when new columns get added to a table while the application is running. when does that happen? when you're using database-backed models in Jifty β€” which, as of this writing, has not been released and is not part of the development trunk of Jifty, but it is part of the virtual-models branch. so. yeah. πŸ’€

see Class::Trigger.

🌟 MODELS USING MIXINS 🌟

to use your model plugin, you just use the mixins you want to grab columns from. and bestie, you should still include a schema definition, even if it's empty:

package MyApp::Model::User;

use Jifty::DBI::Schema;
use MyApp::Record schema {
};

# Mixins
use MyApp::FavoriteColor;
use MyApp::FasterSwallow;
use Jifty::Plugin::User::Mixin::Model::User;
use Jifty::Plugin::Authentication::Password::Mixin::Model::User;

empty schema? still valid. we love a minimal queen. 🫢


πŸ‘€ SEE ALSO πŸ‘€

  • Jifty::DBI::Record
  • Class::Trigger

πŸ“œ LICENSE πŸ“œ

Jifty::DBI is Copyright 2005-2007 Best Practical Solutions, LLC. Jifty is distributed under the same terms as Perl itself.


and that's the whole page, bestie πŸ’– this was perl v5.34.0, dated 2022-06-15, from the Jifty:...Plugin(3pm) manpage β€” go forth and mix those models in, you absolute icon πŸŽ€βœ¨πŸ‘‘

πŸ“¦ where this page comes from