Cataly...Traits(3pm)

πŸ“¦
version 0.19 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

CatalystX::Component::Traits β€” automatic trait loading and resolution for Catalyst components, bestie. like it literally does the trait thing for you so you don't have to πŸŽ€


✨ SYNOPSIS ✨

package Catalyst::Model::SomeModel;
with 'CatalystX::Component::Traits';

package MyApp::Model::MyModel;
use parent 'Catalyst::Model::SomeModel';

package MyApp;

__PACKAGE__->config('Model::MyModel' => {
    traits => ['SearchedForTrait', '+Fully::Qualified::Trait']
});

okay that's the whole vibe right there πŸ‘‘ set it up once, get traits forever.


πŸ¦‹ DESCRIPTION πŸ¦‹

So basically this adds a COMPONENT method (as in Catalyst::Component) to your Catalyst component base class. That method reads the optional traits parameter from your app config and your component config, and then instantiates the component subclass with those traits using new_with_traits from MooseX::Traits via MooseX::Traits::Pluggable. no cap, that's it, that's the whole job πŸ’…


Trait names with a + in front? taken as full package names, periodt. no searching, no guessing, you said the whole thing, you meant the whole thing.

Unqualified names get searched for, using the algorithm below. it's giving detective energy πŸ•΅οΈβ™€οΈ

🌸 EXAMPLE 🌸

say your inheritance hierarchy is:

MyApp::Model::MyModel
Catalyst::Model::CatModel
Catalyst::Model
Catalyst::Component
Moose::Object

and your config is:

traits => ['Foo']

then the package search order for Foo will be:

MyApp::TraitFor::Model::CatModel::Foo
Catalyst::TraitFor::Model::CatModel::Foo

see how it walked up the chain? yeah. iconic behavior ✨

πŸ’€ A MORE PATHOLOGICAL EXAMPLE πŸ’€

okay now for the messy one. for:

My::App::Controller::AController
CatalystX::Something::ControllerBase::SomeController
Catalyst::Controller
Catalyst::Model
Catalyst::Component
Moose::Object

with:

traits => ['Foo']

the search order for Foo will be:

My::App::TraitFor::Controller::SomeController::Foo
CatalystX::Something::TraitFor::Controller::SomeController::Foo

and yeah β€” the Base after (M|V|C) gets automatically removed. the code said "not me reading 'ControllerBase' as 'Controller'" and just... fixed it. slay πŸ™ˆ


🫢 TRAIT MERGING 🫢

Traits from the component class config and the app config get automatically merged if you set the _trait_merge attribute default, like:

has '+_trait_merge' => (default => 1);

And you can also remove component class config traits by prefixing their names with a - in the app config traits. so rude, so effective πŸ’…

for example:

package Catalyst::Model::Foo;
has '+_trait_merge' => (default => 1);
__PACKAGE__->config->{traits} = [qw/Foo Bar/];

package MyApp;
__PACKAGE__->config->{'Model::Foo'}{traits} = [qw/-Foo Baz/];

will load the traits:

Bar Baz

Foo said ✌️ and Baz said heyyy. the math is mathing 🌈


πŸ’œ AUTHOR πŸ’œ

Rafael Kitover, <rkitover@cpan.org> β€” the main character who made this πŸŽ€


⭐ CONTRIBUTORS ⭐

Tomas Doran, <bobtfish@bobtfish.net> β€” bestie came through 🫢


πŸ› BUGS πŸ›

Please report any bugs or feature requests to bug-catalystx-component-traits at rt.cpan.org, or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CatalystX-Component-Traits>.

I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. so like... you WILL hear back. fr fr 😀

🌟 ACKNOWLEDGEMENTS 🌟

Matt S. Trout and Tomas Doran helped me with the current design. the group project actually went well for once πŸ˜­πŸ’–


Copyright (c) 2014, Rafael Kitover

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


and that's the whole page, bestie β€” this is Cataly...Traits(3pm) from the User Contributed Perl Documentation, part of perl v5.34.0, dated 2022-06-09. go load some traits, you absolute icon πŸ’…βœ¨

πŸ“¦ where this page comes from