Lumi
Lumi

Reputation: 15274

Moose: override `required`ness of attribute in derived class

I have a base class and a dozen derived classes. All but one derived classes require an attribute named key. So I could add it to eleven derived classes and leave the twelfth one alone.

However, laziness being what it is, I'd like to add the attribute to the base class, thus avoiding repeating the declaration eleven times and adding what I consider is consistency and simplicity.

Now, that poses a problem for the one class that does not require the key attribute. Note that there is no harm if this class has this attribute, but it does not require it.

My idea has been to resolve this by using a flag method is_strict, which would be called from BUILDARGS to decide whether the key is required or not. Here's a simple script to illustrate this (okay, I inverted the notion, the key attribute is required in only one case (instead of in all but one cases), but the problem remains affected by this inversion):

#!perl
package Bla;
use Moose;
use Carp ();
has grop => is => 'ro', isa => 'Str'; # optional
has key  => is => 'ro', isa => 'Int'; # required in all but one cases
# required => should depend on $class->is_strict;
sub is_strict { 0 } # not strict by default as per this base class
# imagine a bunch of other stuff here shared by all derived classes
around BUILDARGS => sub {
    my $orig = shift;
    my $class = shift;
    my $args = @_ == 1 ? shift : { @_ };
    Carp::croak 'key missing'
        if not exists $args->{key}
        and $class->is_strict;
    return $class->$orig( @_ );
};
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Eins;
use Moose; extends 'Bla';
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Zwei;
use Moose; extends 'Bla';
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Drei;
use Moose; extends 'Bla';
override is_strict => sub { 1 }; # but here it is required
no Moose; __PACKAGE__->meta->make_immutable;

package main;
use Test::More;
use Test::Exception;
lives_ok  { Bla::Eins->new };
lives_ok  { Bla::Zwei->new };
throws_ok { Bla::Drei->new } qr/key missing/;
lives_ok  { Bla::Drei->new( key => 99 ) };
done_testing;

This works, but is there a better way to achieve what I want?

Upvotes: 3

Views: 999

Answers (2)

John Macdonald
John Macdonald

Reputation: 11

Rather than using has '+key' => (requires => 1), you could put the requires=> 1 in the original and use has '+key' => (requires => 0) to turn it off. In the original request, there were 11 required and only one not-required, so that would mean only one definition needs the over-ride and the other 11 can take it unchanged.

Upvotes: 1

Lumi
Lumi

Reputation: 15274

Okay, I've been a little dumb for not having tried the most obvious solution, just override the attribute definition in the derived class where the requiredness differs from the default. Here we go:

#!perl
package Bla;
use Moose;
use Carp ();
has grop => is => 'ro', isa => 'Str'; # optional
has key  => is => 'ro', isa => 'Int'; # required in all but one cases
# imagine a bunch of other stuff here shared by all derived classes
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Eins;
use Moose; extends 'Bla';
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Zwei;
use Moose; extends 'Bla';
no Moose; __PACKAGE__->meta->make_immutable;

package Bla::Drei;
use Moose; extends 'Bla';
# prefix an attribute you're overriding with a "+" sign
has '+key' => is => 'ro', isa => 'Int', required => 1;
no Moose; __PACKAGE__->meta->make_immutable;

package main;
use Test::More;
use Test::Exception;
lives_ok  { Bla::Eins->new };
lives_ok  { Bla::Zwei->new };
throws_ok { Bla::Drei->new } qr/\bkey\b.*\brequired\b/;
lives_ok  { Bla::Drei->new( key => 99 ) };
done_testing;

Still, I'm thankful for all feedback as this Mooseland is less well chartered than object systems in other languages.

Ah, and I've seen you're supposed to prefix an attribute definition that you're overriding with a +, so Moose will pitifully croak if the referenced attribute isn't found in a parent class, adding security and consistency to your code. I've updated my sample code accordingly.

Upvotes: 6

Related Questions