From: Dave Rolsky Date: Sun, 4 Sep 2011 16:26:32 +0000 (-0500) Subject: Merge Moose::Meta::Method::Constructor into CMOP::Method::Constructor X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=39e367b6de8717d3310cac1d8d0d3f4ad5710bc5;p=gitmo%2FMoose.git Merge Moose::Meta::Method::Constructor into CMOP::Method::Constructor --- diff --git a/lib/Class/MOP/Method/Constructor.pm b/lib/Class/MOP/Method/Constructor.pm index 0b4ef7d..c3ada2d 100644 --- a/lib/Class/MOP/Method/Constructor.pm +++ b/lib/Class/MOP/Method/Constructor.pm @@ -12,16 +12,30 @@ use base 'Class::MOP::Method::Inlined'; sub new { my $class = shift; - my %options = @_; + my $params = @_ == 1 ? $_[0] : {@_}; + + my $meta = $params->{metaclass}; + + (blessed $meta && $meta->isa('Class::MOP::Class')) + || $class->throw_error("You must pass a metaclass instance if you want to inline") + if $params->{is_inline}; + + (ref $params->{options} eq 'HASH') + || $class->throw_error("You must pass a hash of options", data => $params->{options}); - (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class')) - || confess "You must pass a metaclass instance if you want to inline" - if $options{is_inline}; + ($params->{package_name} && $params->{name}) + || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT"); - ($options{package_name} && $options{name}) - || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT"; + # XXX - This is a hack that came about from the CMOP/Moose + # merge. Previously, Moose::Meta::Method::Constructor defaulted to + # Moose::Object in this parameter, while the CMOP version expected + # nothing. This should be made less of a hack somehow once the merge is + # done. + $params->{_expected_method_class} = 'Moose::Object' + unless exists $params->{_expected_method_class} + || !$class->isa('Moose::Meta::Method::Constructor'); - my $self = $class->_new(\%options); + my $self = $class->_new($params); # we don't want this creating # a cycle in the code, if not @@ -34,30 +48,32 @@ sub new { } sub _new { - my $class = shift; + my $class = shift; + my $params = shift; - return Class::MOP::Class->initialize($class)->new_object(@_) - if $class ne __PACKAGE__; - - my $params = @_ == 1 ? $_[0] : {@_}; + return Class::MOP::Class->initialize($class)->new_object($params) + if $class ne __PACKAGE__ + # XXX - temporary hack until cmop & moose are merged + && $class ne 'Moose::Meta::Method::Constructor'; return bless { # inherited from Class::MOP::Method - body => $params->{body}, + body => $params->{body}, + # associated_metaclass => $params->{associated_metaclass}, # overriden - package_name => $params->{package_name}, - name => $params->{name}, - original_method => $params->{original_method}, + package_name => $params->{package_name}, + name => $params->{name}, + original_method => $params->{original_method}, # inherited from Class::MOP::Generated - is_inline => $params->{is_inline} || 0, - definition_context => $params->{definition_context}, + is_inline => $params->{is_inline} || 1, + definition_context => $params->{definition_context}, # inherited from Class::MOP::Inlined _expected_method_class => $params->{_expected_method_class}, # defined in this subclass - options => $params->{options} || {}, + options => $params->{options} || {}, associated_metaclass => $params->{metaclass}, }, $class; } @@ -73,7 +89,9 @@ sub _initialize_body { my $self = shift; my $method_name = '_generate_constructor_method'; - $method_name .= '_inline' if $self->is_inline; + $method_name .= '_inline' + if $self->is_inline + && $self->associated_metaclass->can('_inline_new_object'); $self->{'body'} = $self->$method_name; } diff --git a/lib/Moose/Meta/Method/Constructor.pm b/lib/Moose/Meta/Method/Constructor.pm index e20c424..96e31ee 100644 --- a/lib/Moose/Meta/Method/Constructor.pm +++ b/lib/Moose/Meta/Method/Constructor.pm @@ -1,55 +1,9 @@ - package Moose::Meta::Method::Constructor; use strict; use warnings; -use Carp (); -use List::MoreUtils 'any'; -use Scalar::Util 'blessed', 'weaken', 'looks_like_number', 'refaddr'; -use Try::Tiny; - -use base 'Moose::Meta::Method', - 'Class::MOP::Method::Constructor'; - -sub new { - my $class = shift; - my %options = @_; - - my $meta = $options{metaclass}; - - (ref $options{options} eq 'HASH') - || $class->throw_error("You must pass a hash of options", data => $options{options}); - - ($options{package_name} && $options{name}) - || $class->throw_error("You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT"); - - my $self = bless { - 'body' => undef, - 'package_name' => $options{package_name}, - 'name' => $options{name}, - 'options' => $options{options}, - 'associated_metaclass' => $meta, - 'definition_context' => $options{definition_context}, - '_expected_method_class' => $options{_expected_method_class} || 'Moose::Object', - } => $class; - - # we don't want this creating - # a cycle in the code, if not - # needed - weaken($self->{'associated_metaclass'}); - - $self->_initialize_body; - - return $self; -} - -## method - -sub _initialize_body { - my $self = shift; - $self->{'body'} = $self->_generate_constructor_method_inline; -} +use base 'Class::MOP::Method::Constructor', 'Moose::Meta::Method'; 1;