Open srchulo opened 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.
Would this require changes to MooseX::MungeHas to support this?
That would be the simplest way to implement it, yes.
Released a new MooseX::MungeHas.
Wow! That was fast :)
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.
Thank you! I'm on vacation without my laptop now, but I will try to get to this soon to help you out :)
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.
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()
.
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.
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 around
s for arguments_for_moosex_mungehas
called?
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.)
Awesome, thank you :) Please let me know if you'd like any changes on the pull request :)
Looks good to me.
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?