also test adding strictness in a subclass -- and fix mutable case (thanks doy!) :)
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Trait / Class.pm
index bfda93a..7f75669 100644 (file)
@@ -1,13 +1,13 @@
 package MooseX::StrictConstructor::Trait::Class;
 
-use strict;
-use warnings;
-
-use B ();
-use Carp ();
+# applied as class_metaroles => { class => [ __PACKAGE__ ] }, for Moose 1.99x and later
 
 use Moose::Role;
 
+use namespace::autoclean;
+
+use B ();
+
 around '_inline_BUILDALL' => sub {
     my $orig = shift;
     my $self = shift;
@@ -23,15 +23,25 @@ around '_inline_BUILDALL' => sub {
 
     return (
         @source,
-        'my %attrs = (' . join(' ', @attrs) . ');',
+        'my %attrs = (' . ( join ' ', @attrs ) . ');',
         'my @bad = sort grep { !$attrs{$_} } keys %{ $params };',
         'if (@bad) {',
-            'Carp::confess "Found unknown attribute(s) passed to the constructor: @bad";',
+            'Moose->throw_error("Found unknown attribute(s) passed to the constructor: @bad");',
         '}',
     );
 };
 
-no Moose::Role;
+# if the Object role is applied first, and then a superclass added, we just
+# lost our BUILDALL modification.
+after superclasses => sub
+{
+    my $self = shift;
+    return if not @_;
+    Moose::Util::MetaRole::apply_base_class_roles(
+        for => $self->name,
+        roles => ['MooseX::StrictConstructor::Role::Object'],
+    )
+};
 
 1;