Fail at the first hurdle :)
[gitmo/Moo.git] / lib / Moo / Role.pm
index 4055fbc..d84a5fd 100644 (file)
@@ -14,21 +14,85 @@ sub import {
   return if $INFO{$target}; # already exported into this package
   # get symbol table reference
   my $stash = do { no strict 'refs'; \%{"${target}::"} };
-  *{_getglob "${target}::has"} = sub {
+  _install_coderef "${target}::has" => sub {
     my ($name, %spec) = @_;
     ($INFO{$target}{accessor_maker} ||= do {
       require Method::Generate::Accessor;
       Method::Generate::Accessor->new
     })->generate_method($target, $name, \%spec);
-    $INFO{$target}{attributes}{$name} = \%spec;
+    push @{$INFO{$target}{attributes}||=[]}, $name, \%spec;
   };
+  if ($INC{'Moo/HandleMoose.pm'}) {
+    Moo::HandleMoose::inject_fake_metaclass_for($target);
+  }
   goto &Role::Tiny::import;
 }
 
-sub apply_role_to_package {
+sub _inhale_if_moose {
+  my ($self, $role) = @_;
+  _load_module($role);
+  if (!$INFO{$role} and $INC{"Moose.pm"}) {
+    if (my $meta = Class::MOP::class_of($role)) {
+      $INFO{$role}{methods} = {
+        map +($_ => $role->can($_)), $meta->get_method_list
+      };
+      $Role::Tiny::APPLIED_TO{$role} = {
+        map +($_->name => 1), $meta->calculate_all_roles
+      };
+      $INFO{$role}{requires} = [ $meta->get_required_method_list ];
+      $INFO{$role}{attributes} = [
+        map +($_ => $meta->get_attribute($_)), $meta->get_attribute_list
+      ];
+      my $mods = $INFO{$role}{modifiers} = [];
+      foreach my $type (qw(before after around)) {
+        my $map = $meta->${\"get_${type}_method_modifiers_map"};
+        foreach my $method (keys %$map) {
+          foreach my $mod (@{$map->{$method}}) {
+            push @$mods, [ $type => $method => $mod ];
+          }
+        }
+      }
+      require Class::Method::Modifiers if @$mods;
+      $INFO{$role}{inhaled_from_moose} = 1;
+    }
+  }
+}
+
+sub _maybe_make_accessors {
+  my ($self, $role, $target) = @_;
+  my $m;
+  if ($INFO{$role}{inhaled_from_moose}
+      or $m = Moo->_accessor_maker_for($target)
+      and ref($m) ne 'Method::Generate::Accessor') {
+    $self->_make_accessors($role, $target);
+  }
+}
+
+sub _make_accessors_if_moose {
+  my ($self, $role, $target) = @_;
+  if ($INFO{$role}{inhaled_from_moose}) {
+    $self->_make_accessors($role, $target);
+  }
+}
+
+sub _make_accessors {
+  my ($self, $role, $target) = @_;
+  my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do {
+    require Method::Generate::Accessor;
+    Method::Generate::Accessor->new
+  });
+  my @attrs = @{$INFO{$role}{attributes}||[]};
+  while (my ($name, $spec) = splice @attrs, 0, 2) {
+    $acc_gen->generate_method($target, $name, $spec);
+  }
+}
+
+sub apply_single_role_to_package {
   my ($me, $to, $role) = @_;
-  $me->SUPER::apply_role_to_package($to, $role);
+  $me->_inhale_if_moose($role);
   $me->_handle_constructor($to, $INFO{$role}{attributes});
+  $me->_maybe_make_accessors($role, $to);
+  $me->SUPER::apply_single_role_to_package($to, $role);
 }
 
 sub create_class_with_roles {
@@ -40,6 +104,17 @@ sub create_class_with_roles {
 
   return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
 
+  $me->_inhale_if_moose($_) for @roles;
+
+  my $m;
+  if ($m = Moo->_accessor_maker_for($superclass)
+      and ref($m) ne 'Method::Generate::Accessor') {
+    # old fashioned way time.
+    *{_getglob("${new_name}::ISA")} = [ $superclass ];
+    $me->apply_roles_to_package($new_name, @roles);
+    return $new_name;
+  }
+
   require Sub::Quote;
 
   $me->SUPER::create_class_with_roles($superclass, @roles);
@@ -51,12 +126,20 @@ 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 ], $superclass
   );
 
   return $new_name;
 }
 
+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->SUPER::_composable_package_for($role);
+}
+
 sub _install_single_modifier {
   my ($me, @args) = @_;
   _install_modifier(@args);
@@ -64,14 +147,15 @@ sub _install_single_modifier {
 
 sub _handle_constructor {
   my ($me, $to, $attr_info, $superclass) = @_;
-  return unless $attr_info && keys %$attr_info;
+  return unless $attr_info && @$attr_info;
   if ($INFO{$to}) {
-    @{$INFO{$to}{attributes}||={}}{keys %$attr_info} = values %$attr_info;
+    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)) {
-      $con->register_attribute_specs(%$attr_info);
+      # shallow copy of the specs since the constructor will assign an index
+      $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info);
     }
   }
 }
@@ -130,3 +214,13 @@ imported by this module.
 
 Declares an attribute for the class to be composed into.  See
 L<Moo/has> for all options.
+
+=head1 AUTHORS
+
+See L<Moo> for authors.
+
+=head1 COPYRIGHT AND LICENSE
+
+See L<Moo> for the copyright and license.
+
+=cut