tobyink / p5-moops

16 stars 4 forks source link

Possible to have a :required attribute? #5

Open srchulo opened 5 years ago

srchulo commented 5 years ago

I find the :ro attribute super useful. I often want to use :ro and have all fields be required, too. Would it be possible to add this as an attribute?

tobyink commented 5 years ago

This seems like a reasonable request.

Attributes could be made non-required by explicitly specifying required => false or by providing a default.

srchulo commented 5 years ago

Would this require changes to MooseX::MungeHas to support this?

tobyink commented 5 years ago

That would be the simplest way to implement it, yes.

tobyink commented 5 years ago

Released a new MooseX::MungeHas.

srchulo commented 5 years ago

Wow! That was fast :)

tobyink commented 5 years ago

It was only a few lines of code. The test cases were more work than the implementation.

Moops just needs this now:

use v5.14;
use strict;
use warnings FATAL => 'all';
no warnings qw(void once uninitialized numeric);

package Moops::TraitFor::Keyword::req;

our $AUTHORITY = 'cpan:TOBYINK';
our $VERSION   = '0.036';

use Moo::Role;

around arguments_for_moosex_mungehas => sub {
    my $next = shift;
    my $self = shift;
    require MooseX::MungeHas;
    MooseX::MungeHas->VERSION('0.011');
    return ('always_required', $self->$next(@_));
};

1;

Once you've got that, you should be able to do:

use Moops;

class Foo :req {
    ...;
}

I'll try to get it included in a release of Moops some time this month. If you wanted to write some test cases for it, that might help me get it released faster. t/94trait-ro-rw-rwp.t is pretty close to the kinds of tests you'd want to do.

srchulo commented 5 years ago

Thank you! I'm on vacation without my laptop now, but I will try to get to this soon to help you out :)

srchulo commented 5 years ago

Created pull request here:

https://github.com/tobyink/p5-moops/pull/7

Sorry-- I'm not sure if there's a better way to link these.

srchulo commented 5 years ago

Also, if you don't mind, could you explain how

return ('always_required', $self->$next(@_));

Works? It's returning 'always_required' and the values return by $next, but to what? And with multiple traits do the return values just stack up, and $next can be another around method for another trait? It looks like they're passed to use MooseX::MungeHas qw().

tobyink commented 5 years ago

Moops::Keyword::Role line 51-52 is:

push @lines, "use MooseX::MungeHas qw(@{[ $self->arguments_for_moosex_mungehas ]});"
    if $using =~ /^Mo/;

And Moops::Keyword::Role->arguments_for_moosex_mungehas just provides a list of strings.

Traits like :req get applied to Moops::Keyword::Role, so can modify the list returned by arguments_for_moosex_mungehas, in this case adding another string to it.

srchulo commented 5 years ago

Thank you for taking the time to explain this to me-- I'm trying to learn how Moops works and I appreciate it.

One more follow up question, when you have multiple traits like :req and :ro, are both arounds for arguments_for_moosex_mungehas called?

tobyink commented 5 years ago

Yep. That's just generally how around works in Moose/Mouse/Moo. Multiple roles can be applied to a class and can all use around to modify the same method. (The order in which they are applied is not guaranteed, but in this case, the order doesn't matter anyway.)

srchulo commented 5 years ago

Awesome, thank you :) Please let me know if you'd like any changes on the pull request :)

tobyink commented 5 years ago

Looks good to me.