Subject: | Add a strict option for p-roles |
It's nice to have an option to die on unknown parameters. We implemented this in an internal wrapper at $WORK using this code:
package MM::MooseX::Role::Parameterized::Meta::Trait::Parameterizable::Strict;
+
+use MM::Moose::Role;
+
+with 'MooseX::Role::Parameterized::Meta::Trait::Parameterizable';
+
+around construct_parameters => sub ($orig, $self, %params) {
+ my %attrs = (
+ -alias => 1,
+ -excludes => 1,
+ (
+ map { $_ => 1 }
+ grep { defined }
+ map { $_->init_arg }
+ $self->parameters_metaclass->get_all_attributes
+ ),
+ );
+
+ if ( my @bad = sort grep { !exists $attrs{$_} } keys %params ) {
+ die 'Found unknown parameter(s) passed to role: ' . join ', ',
+ @bad;
+ }
+
+ return $self->$orig(%params);
+};
+
+1;