check if it is a role after inhaling from Moose
[gitmo/Moo.git] / lib / Moo / Role.pm
index 628258d..96315b7 100644 (file)
@@ -2,6 +2,7 @@ package Moo::Role;
 
 use strictures 1;
 use Moo::_Utils;
+use Role::Tiny ();
 use base qw(Role::Tiny);
 
 require Moo::sification;
@@ -9,6 +10,7 @@ require Moo::sification;
 BEGIN { *INFO = \%Role::Tiny::INFO }
 
 our %INFO;
+our %APPLY_DEFAULTS;
 
 sub _install_tracked {
   my ($target, $name, $code) = @_;
@@ -20,18 +22,24 @@ sub import {
   my $target = caller;
   my ($me) = @_;
   strictures->import;
-  return if $INFO{$target}; # already exported into this package
-  $INFO{$target} = {};
+  if ($Moo::MAKERS{$target} and $Moo::MAKERS{$target}{is_class}) {
+    die "Cannot import Moo::Role into a Moo class";
+  }
+  $INFO{$target} ||= {};
   # get symbol table reference
   my $stash = do { no strict 'refs'; \%{"${target}::"} };
   _install_tracked $target => has => sub {
-    my ($name, %spec) = @_;
-    ($INFO{$target}{accessor_maker} ||= do {
-      require Method::Generate::Accessor;
-      Method::Generate::Accessor->new
-    })->generate_method($target, $name, \%spec);
-    push @{$INFO{$target}{attributes}||=[]}, $name, \%spec;
-    $me->_maybe_reset_handlemoose($target);
+    my ($name_proto, %spec) = @_;
+    my $name_isref = ref $name_proto eq 'ARRAY';
+    foreach my $name ($name_isref ? @$name_proto : $name_proto) {
+      my $spec_ref = $name_isref ? +{%spec} : \%spec;
+      ($INFO{$target}{accessor_maker} ||= do {
+        require Method::Generate::Accessor;
+        Method::Generate::Accessor->new
+      })->generate_method($target, $name, $spec_ref);
+      push @{$INFO{$target}{attributes}||=[]}, $name, $spec_ref;
+      $me->_maybe_reset_handlemoose($target);
+    }
   };
   # install before/after/around subs
   foreach my $type (qw(before after around)) {
@@ -49,6 +57,9 @@ sub import {
     $me->apply_roles_to_package($target, @_);
     $me->_maybe_reset_handlemoose($target);
   };
+  return if $INFO{$target}{is_role}; # already exported into this package
+  $INFO{$target}{is_role} = 1;
+  *{_getglob("${target}::meta")} = $me->can('meta');
   # grab all *non-constant* (stash slot is not a scalarref) subs present
   # in the symbol table and store their refaddrs (no need to forcibly
   # inflate constant subs into real subs) - also add '' to here (this
@@ -63,6 +74,13 @@ sub import {
   }
 }
 
+# duplicate from Moo::Object
+sub meta {
+  require Moo::HandleMoose::FakeMetaClass;
+  my $class = ref($_[0])||$_[0];
+  bless({ name => $class }, 'Moo::HandleMoose::FakeMetaClass');
+}
+
 sub unimport {
   my $target = caller;
   _unimport_coderefs($target, $INFO{$target});
@@ -83,10 +101,12 @@ sub _inhale_if_moose {
       and (
         $INC{"Moose.pm"}
         and $meta = Class::MOP::class_of($role)
+        and $meta->isa('Moose::Meta::Role')
       )
       or (
-        $INC{"Mouse.pm"}
+        Mouse::Util->can('find_meta')
         and $meta = Mouse::Util::find_meta($role)
+        and $meta->isa('Mouse::Meta::Role')
      )
   ) {
     $INFO{$role}{methods} = {
@@ -100,12 +120,14 @@ sub _inhale_if_moose {
     $INFO{$role}{requires} = [ $meta->get_required_method_list ];
     $INFO{$role}{attributes} = [
       map +($_ => do {
-        my $spec = { %{$meta->get_attribute($_)} };
+        my $attr = $meta->get_attribute($_);
+        my $is_mouse = $meta->isa('Mouse::Meta::Role');
+        my $spec = { %{ $is_mouse ? $attr : $attr->original_options } };
 
         if ($spec->{isa}) {
 
           my $get_constraint = do {
-            my $pkg = $meta->isa('Mouse::Meta::Role')
+            my $pkg = $is_mouse
                         ? 'Mouse::Util::TypeConstraints'
                         : 'Moose::Util::TypeConstraints';
             _load_module($pkg);
@@ -186,13 +208,16 @@ sub _make_accessors {
 
 sub apply_roles_to_package {
   my ($me, $to, @roles) = @_;
-  $me->_inhale_if_moose($_) for @roles;
+  foreach my $role (@roles) {
+      $me->_inhale_if_moose($role);
+  }
   $me->SUPER::apply_roles_to_package($to, @roles);
 }
 
 sub apply_single_role_to_package {
   my ($me, $to, $role) = @_;
   $me->_inhale_if_moose($role);
+  die "${role} is not a Moo::Role" unless my $info = $INFO{$role};
   $me->_handle_constructor($to, $INFO{$role}{attributes});
   $me->_maybe_make_accessors($role, $to);
   $me->SUPER::apply_single_role_to_package($to, $role);
@@ -207,7 +232,9 @@ sub create_class_with_roles {
 
   return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
 
-  $me->_inhale_if_moose($_) for @roles;
+  foreach my $role (@roles) {
+      $me->_inhale_if_moose($role);
+  }
 
   my $m;
   if ($INC{"Moo.pm"}
@@ -230,12 +257,52 @@ sub create_class_with_roles {
   $Moo::MAKERS{$new_name} = {};
 
   $me->_handle_constructor(
-    $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ], $superclass
+    $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ]
   );
 
   return $new_name;
 }
 
+sub apply_roles_to_object {
+  my ($me, $object, @roles) = @_;
+  my $new = $me->SUPER::apply_roles_to_object($object, @roles);
+
+  my $apply_defaults = $APPLY_DEFAULTS{ref $new} ||= do {
+    my %attrs = map { @{$INFO{$_}{attributes}||[]} } @roles;
+
+    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);
+    }
+    else {
+      sub {};
+    }
+  };
+  $new->$apply_defaults;
+  return $new;
+}
+
 sub _composable_package_for {
   my ($self, $role) = @_;
   my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
@@ -250,14 +317,14 @@ sub _install_single_modifier {
 }
 
 sub _handle_constructor {
-  my ($me, $to, $attr_info, $superclass) = @_;
+  my ($me, $to, $attr_info) = @_;
   return unless $attr_info && @$attr_info;
   if ($INFO{$to}) {
     push @{$INFO{$to}{attributes}||=[]}, @$attr_info;
   } else {
     # only fiddle with the constructor if the target is a Moo class
     if ($INC{"Moo.pm"}
-        and my $con = Moo->_constructor_maker_for($to, $superclass)) {
+        and my $con = Moo->_constructor_maker_for($to)) {
       # shallow copy of the specs since the constructor will assign an index
       $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info);
     }
@@ -319,6 +386,10 @@ imported by this module.
 Declares an attribute for the class to be composed into.  See
 L<Moo/has> for all options.
 
+=head1 SUPPORT
+
+See L<Moo> for support and contact information.
+
 =head1 AUTHORS
 
 See L<Moo> for authors.
@@ -326,5 +397,3 @@ See L<Moo> for authors.
 =head1 COPYRIGHT AND LICENSE
 
 See L<Moo> for the copyright and license.
-
-=cut