Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to have a :required attribute? #5

Open
srchulo opened this issue Dec 9, 2018 · 14 comments
Open

Possible to have a :required attribute? #5

srchulo opened this issue Dec 9, 2018 · 14 comments

Comments

@srchulo
Copy link

srchulo commented Dec 9, 2018

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
Copy link
Owner

tobyink commented Jan 1, 2019

This seems like a reasonable request.

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

@srchulo
Copy link
Author

srchulo commented Jan 1, 2019

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

@tobyink
Copy link
Owner

tobyink commented Jan 1, 2019

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

@tobyink
Copy link
Owner

tobyink commented Jan 1, 2019

Released a new MooseX::MungeHas.

@srchulo
Copy link
Author

srchulo commented Jan 1, 2019

Wow! That was fast :)

@tobyink
Copy link
Owner

tobyink commented Jan 1, 2019

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
Copy link
Author

srchulo commented Jan 2, 2019

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

@srchulo
Copy link
Author

srchulo commented Jan 6, 2019

Created pull request here:

#7

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

@srchulo
Copy link
Author

srchulo commented Jan 6, 2019

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
Copy link
Owner

tobyink commented Jan 7, 2019

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
Copy link
Author

srchulo commented Jan 7, 2019

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
Copy link
Owner

tobyink commented Jan 7, 2019

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
Copy link
Author

srchulo commented Jan 8, 2019

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

@tobyink
Copy link
Owner

tobyink commented Jan 8, 2019

Looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants