bump version to 0.94
[gitmo/Class-MOP.git] / lib / Class / MOP / Method.pm
index f2f3bb1..6602310 100644 (file)
@@ -5,9 +5,9 @@ use strict;
 use warnings;
 
 use Carp         'confess';
-use Scalar::Util 'weaken';
+use Scalar::Util 'weaken', 'reftype', 'blessed';
 
-our $VERSION   = '0.81';
+our $VERSION   = '0.94';
 $VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
@@ -18,15 +18,6 @@ use base 'Class::MOP::Object';
 # they should act like CODE refs.
 use overload '&{}' => sub { $_[0]->body }, fallback => 1;
 
-our $UPGRADE_ERROR_TEXT = q{
----------------------------------------------------------
-NOTE: this error is likely not an error, but a regression
-caused by the latest upgrade to Moose/Class::MOP. Consider
-upgrading any MooseX::* modules to their latest versions
-before spending too much time chasing this one down.
----------------------------------------------------------
-};
-
 # construction
 
 sub wrap {
@@ -37,11 +28,18 @@ sub wrap {
     my %params = @args;
     my $code = $params{body};
 
-    ('CODE' eq ref($code))
-        || confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
+    if (blessed($code) && $code->isa(__PACKAGE__)) {
+        my $method = $code->clone;
+        delete $params{body};
+        Class::MOP::class_of($class)->rebless_instance($method, %params);
+        return $method;
+    }
+    elsif (!ref $code || 'CODE' ne reftype($code)) {
+        confess "You must supply a CODE reference to bless, not (" . ($code || 'undef') . ")";
+    }
 
     ($params{package_name} && $params{name})
-        || confess "You must supply the package_name and name parameters $UPGRADE_ERROR_TEXT";
+        || confess "You must supply the package_name and name parameters";
 
     my $self = $class->_new(\%params);
 
@@ -52,13 +50,18 @@ sub wrap {
 
 sub _new {
     my $class = shift;
+
+    return Class::MOP::Class->initialize($class)->new_object(@_)
+        if $class ne __PACKAGE__;
+
     my $params = @_ == 1 ? $_[0] : {@_};
 
-    my $self = bless {
+    return bless {
         'body'                 => $params->{body},
         'associated_metaclass' => $params->{associated_metaclass},
         'package_name'         => $params->{package_name},
         'name'                 => $params->{name},
+        'original_method'      => $params->{original_method},
     } => $class;
 }
 
@@ -148,8 +151,9 @@ introspection interface.
 
 =item B<< Class::MOP::Method->wrap($code, %options) >>
 
-This is the constructor. It accepts a subroutine reference and a hash
-of options.
+This is the constructor. It accepts a method body in the form of
+either a code reference or a L<Class::MOP::Method> instance, followed
+by a hash of options.
 
 The options are:
 
@@ -157,11 +161,13 @@ The options are:
 
 =item * name
 
-The method name (without a package name). This is required.
+The method name (without a package name). This is required if C<$code>
+is a coderef.
 
 =item * package_name
 
-The package name for the method. This is required.
+The package name for the method. This is required if C<$code> is a
+coderef.
 
 =item * associated_metaclass