Hello Monks,
I've finally been compelled to the monastery gates for the first time by the intricacies of the Moose meta-model. I by no means mean that pejoratively: on the contrary, it all seems wonderfully elegant. Which makes my inability to work this simple example all the more frustrating!
I'm making a module that will dynamically add attributes to a class at run-time based on key/value pairs in a config file.
The various modules on CPAN which sound like they might fit the bill only seem to fill-in values for 'existing' attributes, i.e. those which have been declared in the client class. My intent is really to turn /any/ key in the config file into an attribute in the client class. So it would look something like MooseX::ConfigFromFile, but creating new attributes instead of merely initializing them.*
Now, as my first foray into making a Moose extension, I have a basic test case:
lib/MooseX/Config/Auto.pmand in lib/MooseX/Config/Auto/Role/Meta/Class.pmpackage MooseX::Config::Auto; use Moose (); use Moose::Exporter; use MooseX::Config::Auto::Role::Meta::Class; Moose::Exporter->setup_import_methods( class_metaroles => { class => ['MooseX::Config::Auto::Role::Meta::Class'], } );
package MooseX::Config::Auto::Role::Meta::Class; use Moose::Role; after new_object => sub { print "BOOP!\n"; };
Now, here's my problem: I feel my module's functionality would be better suited to being a Role that's with'd than a regular module that's use'd. Maybe I'm completely off base with this, but as a theoretical exercise*: Is there in fact any way to get the simple behaviour outlined above via a Role?
The problem seems to be getting a hold of the consuming class's metaclass, in order to apply my own metaroles. I've looked at MooseX::Role::Parameterized, and read through the ins and outs of a number of other modules like MooseX::UndefTolerant and MooseX::GlobRef, but haven't managed to work out how to achieve what I'm after.
My only clue came from adding this code:
print "\t", $_->name, "\n" for __PACKAGE__->meta->meta->calculate_all_ +roles;
Using the 'normal' approach outlined above, sure enough this prints 'MooseX::Config::Auto::Role::Meta::Class'. But my various attempts at the Role method give me the error
"Can't locate object method "calculate_all_roles" via package "Class:: +MOP::Class::Immutable::Class::MOP::Class"
It may be that the answer's staring me in the face, but I'm too lost in the woods at this point to see it.
Put me right monks!
* This sounds like a Bad Idea, and it certainly is. The created attributes will lack most Moosey benefits. It's intended to be used strictly in an early, rapid development phase, where it's more convenient to add and delete lines from a config file than 'hard-code' into your class. Yes, yes, perhaps only very marginally more convenient - and there are perfectly good ways to add many attributes at once in Moose. I accept that this may well be Doing It Wrong from the get-go; but I've learnt a lot about Moose in the process, so I hope you can indulge me purely as an academic endeavour, if not otherwise :)
In reply to Can a Moose Role access & modify the metaclass of classes into which it's composed? by doubi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |