Plinking Away Part the Third
In my last post I had a look at how MooseX::Abstract::Factory worked and what it could do for me. Today I will continue down my little list and have a look at
'MooseX::ClassCompositor' and see if this one fits into what I am trying to do.
Well at first glance it seems this could really work for me. Start with an empty class and add in all the roles. So lets have a closer look. Like the last module the documentation is slim and the test suit contains little more than the synopsis so no pointers there.
Well this might be good for game time but I am not sure if it will work with my present class structure and trick. Seem I have to have everything a role. Well lets give it the old collage try
Looking at how I will have to write code for it is unlike a Factory that one calls the create function indirectly it appears that I have to instantiate an instantiate of this class to use it. With the instance you can then use the 'class_for' method to apply the needed roles and that will then return a class that you can use 'new' to create a new instance.
So here is what I start out with
use MooseX::ClassCompositor;
my $comp = MooseX::ClassCompositor->new({
class_basename => 'RPG::ADD::Player',
class_metaroles => {
class => [ qw( RPG::ADD::Character::Ability::Henchmen
RPG::ADD::Character::Ability::OpenDoorsOnA
RPG::ADD::Languages )],
},
});
my $class = $comp->class_for( qw( RPG::ADD::Character::Class::Fighter::ExceptionalStrength) );
my $fighter = $class->new (
{name=>'Sir Cumferace',
strength=>18,
exceptional=>91},
);
print $fighter->name()."\n";
print "strength=".$fighter->strength();
print " (".$fighter->exceptional().")\n"
if($fighter->can('exceptional'));
print "opens a door on a ". $fighter->open_door_on_a()."\n";
print "opens a barred door on a ".$fighter->open_barred_doors_on_a
if($fighter->can('open_barred_doors_on_a'));
And when I give his a go I get
RPG::ADD::Character::Ability::Henchmen|RPG::ADD::Character::Ability::OpenDoorsOnA|RPG::ADD::Languages' requires the methods 'charisma', 'intelligence', and 'strength' to be implemented by 'Moose::Meta::Class::__ANON__::SERIAL::7' at C:\Dwimperl\perl\site\lib\Moose\Exception.pm line 37
...
opps not what I want. So obviously it is just trying to add in my roles into a blank space and my roles are complaining. Well I am not dead yet. How about if I change my 'RPG::ADD::Character' class into role and give that the old college try?
So now I have
my $comp = MooseX::ClassCompositor->new({
class_basename => 'RPG::ADD::Player',
class_metaroles => {
class => [ qw( RPG::ADD::Character
RPG::ADD::Character::Ability::Henchmen
RPG::ADD::Character::Ability::OpenDoorsOnA
RPG::ADD::Languages )],
},
});
and unfortunately the same again
RPG::ADD::Character|RPG::ADD::Character::Ability::Henchmen|RPG::ADD::Character::Ability::OpenDoorsOnA|RPG::ADD::Languages' requires the methods 'charisma', 'intelligence', and 'strength' to be implemented by 'Moose::Meta::Class::__ANON__::SERIAL::7' at C:\Dwimperl\perl\site\lib\Moose\Exception.pm line 37
So I guess you cannot use any role that has a 'require's in it. As well as I look back in my 'Fighter' class I had this in the BUILD sub
if ($self->exceptional() >90 and $self->exceptional()<=99){
RPG::ADD::Character::Class::Fighter::OpenBarredDoorsOnA1->meta->apply($self);
}
elsif
...
So obviously with no 'Class' to start this will not work to give my 'OpenBarredDoorsOnA1' role if I have high exceptional strength. Given that I use this little trick all over in my classes I would have to do a major rewrite of everything and bunch 'character' abilities and 'skill' together, This I was trying to avoid from the very start and I am not even sure it will work.
So it onto the bottom of the stack for MooseX::ClassCompositor.
Though I think I will keep it about for a while could be useful when creating objects on the fly like walls doors and the other implementia of RPG gaming. Though I would have to set things up so I have all role objects?
I don't know if you review lesser used modules but MooseX::ShortCut::BuildInstance allows the argument "add_roles_in_sequence" for both class building and instance building which supports the 'requires' parts of roles. It might work for you in this case.
That is #3 on my list from this post
https://blogs.perl.org/users/byterock/2014/04/still-plinking-away-part-the-first.html
Sorry for the oversight. I thought I had been keeping up with your posts but I missed that one. I'm flattered to be reviewed. (for better or worse.)