more consistent method arguments and extra protection against autovivification
[gitmo/Moo.git] / lib / Moo / Role.pm
index 764ffb2..c890dfe 100644 (file)
@@ -10,6 +10,7 @@ require Moo::sification;
 BEGIN { *INFO = \%Role::Tiny::INFO }
 
 our %INFO;
+our %APPLY_DEFAULTS;
 
 sub _install_tracked {
   my ($target, $name, $code) = @_;
@@ -171,25 +172,25 @@ sub _inhale_if_moose {
 }
 
 sub _maybe_make_accessors {
-  my ($self, $role, $target) = @_;
+  my ($self, $target, $role) = @_;
   my $m;
-  if ($INFO{$role}{inhaled_from_moose}
+  if ($INFO{$role} && $INFO{$role}{inhaled_from_moose}
       or $INC{"Moo.pm"}
       and $m = Moo->_accessor_maker_for($target)
       and ref($m) ne 'Method::Generate::Accessor') {
-    $self->_make_accessors($role, $target);
+    $self->_make_accessors($target, $role);
   }
 }
 
 sub _make_accessors_if_moose {
-  my ($self, $role, $target) = @_;
-  if ($INFO{$role}{inhaled_from_moose}) {
-    $self->_make_accessors($role, $target);
+  my ($self, $target, $role) = @_;
+  if ($INFO{$role} && $INFO{$role}{inhaled_from_moose}) {
+    $self->_make_accessors($target, $role);
   }
 }
 
 sub _make_accessors {
-  my ($self, $role, $target) = @_;
+  my ($self, $target, $role) = @_;
   my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do {
     require Method::Generate::Accessor;
     Method::Generate::Accessor->new
@@ -215,10 +216,10 @@ sub apply_roles_to_package {
 
 sub apply_single_role_to_package {
   my ($me, $to, $role) = @_;
-  die "${role} is not a Moo::Role" unless my $info = $INFO{$role};
   $me->_inhale_if_moose($role);
-  $me->_handle_constructor($to, $INFO{$role}{attributes});
-  $me->_maybe_make_accessors($role, $to);
+  die "${role} is not a Moo::Role" unless my $info = $INFO{$role};
+  $me->_handle_constructor($to, $role);
+  $me->_maybe_make_accessors($to, $role);
   $me->SUPER::apply_single_role_to_package($to, $role);
 }
 
@@ -250,14 +251,12 @@ sub create_class_with_roles {
   $me->SUPER::create_class_with_roles($superclass, @roles);
 
   foreach my $role (@roles) {
-    die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
+    die "${role} is not a Role::Tiny" unless $INFO{$role};
   }
 
   $Moo::MAKERS{$new_name} = {};
 
-  $me->_handle_constructor(
-    $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ]
-  );
+  $me->_handle_constructor($new_name, $_) for @roles;
 
   return $new_name;
 }
@@ -265,31 +264,40 @@ sub create_class_with_roles {
 sub apply_roles_to_object {
   my ($me, $object, @roles) = @_;
   my $new = $me->SUPER::apply_roles_to_object($object, @roles);
-  if ($INC{'Moo.pm'}
-      and my $m = Moo->_accessor_maker_for(ref $new)
-      and my $con_gen = Moo->_constructor_maker_for(ref $new)) {
-    require Sub::Quote;
 
-    my $specs = $con_gen->all_attribute_specs;
+  my $apply_defaults = $APPLY_DEFAULTS{ref $new} ||= do {
     my %attrs = map { @{$INFO{$_}{attributes}||[]} } @roles;
 
-    my $assign = '';
-    my %captures;
-    foreach my $name ( keys %attrs ) {
-      my $spec = $specs->{$name};
-      if ($m->has_eager_default($name, $spec)) {
-        my ($has, $has_cap)
-          = $m->generate_simple_has('$_[0]', $name, $spec);
-        my ($code, $pop_cap)
-          = $m->generate_use_default('$_[0]', $name, $spec, $has);
-
-        $assign .= $code;
-        @captures{keys %$has_cap, keys %$pop_cap}
-          = (values %$has_cap, values %$pop_cap);
+    if ($INC{'Moo.pm'}
+        and keys %attrs
+        and my $con_gen = Moo->_constructor_maker_for(ref $new)
+        and my $m = Moo->_accessor_maker_for(ref $new)) {
+      require Sub::Quote;
+
+      my $specs = $con_gen->all_attribute_specs;
+
+      my $assign = '';
+      my %captures;
+      foreach my $name ( keys %attrs ) {
+        my $spec = $specs->{$name};
+        if ($m->has_eager_default($name, $spec)) {
+          my ($has, $has_cap)
+            = $m->generate_simple_has('$_[0]', $name, $spec);
+          my ($code, $pop_cap)
+            = $m->generate_use_default('$_[0]', $name, $spec, $has);
+
+          $assign .= $code;
+          @captures{keys %$has_cap, keys %$pop_cap}
+            = (values %$has_cap, values %$pop_cap);
+        }
       }
+      Sub::Quote::quote_sub($assign, \%captures);
     }
-    Sub::Quote::quote_sub($assign, \%captures)->($new);
-  }
+    else {
+      sub {};
+    }
+  };
+  $new->$apply_defaults;
   return $new;
 }
 
@@ -297,7 +305,7 @@ sub _composable_package_for {
   my ($self, $role) = @_;
   my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
   return $composed_name if $Role::Tiny::COMPOSED{role}{$composed_name};
-  $self->_make_accessors_if_moose($role, $composed_name);
+  $self->_make_accessors_if_moose($composed_name, $role);
   $self->SUPER::_composable_package_for($role);
 }
 
@@ -307,7 +315,8 @@ sub _install_single_modifier {
 }
 
 sub _handle_constructor {
-  my ($me, $to, $attr_info) = @_;
+  my ($me, $to, $role) = @_;
+  my $attr_info = $INFO{$role} && $INFO{$role}{attributes};
   return unless $attr_info && @$attr_info;
   if ($INFO{$to}) {
     push @{$INFO{$to}{attributes}||=[]}, @$attr_info;