X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FClass%2FMOP%2FMethod%2FConstructor.pm;h=29017edb231b9081de289c746166aac50bf3e068;hb=d004c8d565f9b314da7652e9368aeb4587ffaa3d;hp=687aed6353bbd8c8c5dddf52dc9dc7cb3ae09d03;hpb=4b921e6bd2e94a59c73f3c51ace65f4ed677778d;p=gitmo%2FClass-MOP.git diff --git a/lib/Class/MOP/Method/Constructor.pm b/lib/Class/MOP/Method/Constructor.pm index 687aed6..29017ed 100644 --- a/lib/Class/MOP/Method/Constructor.pm +++ b/lib/Class/MOP/Method/Constructor.pm @@ -8,7 +8,7 @@ use Carp 'confess'; use Scalar::Util 'blessed', 'weaken'; use Try::Tiny; -our $VERSION = '1.11'; +our $VERSION = '1.12'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; @@ -75,7 +75,10 @@ sub associated_metaclass { (shift)->{'associated_metaclass'} } sub _attributes { my $self = shift; - $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ] + $self->{'attributes'} ||= [ + sort { $a->name cmp $b->name } + $self->associated_metaclass->get_all_attributes + ] } ## method @@ -89,10 +92,6 @@ sub _initialize_body { $self->{'body'} = $self->$method_name; } -sub _generate_constructor_method { - return sub { Class::MOP::Class->initialize(shift)->new_object(@_) } -} - sub _eval_environment { my $self = shift; my $defaults = [map { $_->default } @{ $self->_attributes }]; @@ -101,23 +100,18 @@ sub _eval_environment { }; } +sub _generate_constructor_method { + return sub { Class::MOP::Class->initialize(shift)->new_object(@_) } +} + sub _generate_constructor_method_inline { my $self = shift; my $meta = $self->associated_metaclass; - my $idx = 0; my @source = ( 'sub {', - 'my $class = shift;', - 'return Class::MOP::Class->initialize($class)->new_object(@_)', - 'if $class ne \'' . $meta->name . '\';', - 'my $params = @_ == 1 ? $_[0] : {@_};', - 'my $instance = ' . $meta->inline_create_instance('$class') . ';', - (map { $self->_generate_slot_initializer($_, $idx++) } - @{ $self->_attributes }), - $self->_preserve_weak_metaclasses, - 'return $instance', + $meta->_inline_new_object, '}', ); @@ -134,78 +128,6 @@ sub _generate_constructor_method_inline { return $code; } -sub _generate_slot_initializer { - my $self = shift; - my ($attr, $idx) = @_; - - my $default = $self->_generate_default_value($attr, $idx); - - if (defined(my $init_arg = $attr->init_arg)) { - my @source = ( - 'if (exists $params->{\'' . $init_arg . '\'}) {', - $attr->inline_set( - '$instance', '$params->{\'' . $init_arg . '\'}' - ) . ';', - '}', - ); - if (defined $default) { - push @source, ( - 'else {', - $attr->inline_set('$instance', $default) . ';', - '}', - ); - } - return @source; - } - elsif (defined $default) { - return ($attr->inline_set('$instance', $default) . ';'); - } - else { - return (); - } -} - -sub _preserve_weak_metaclasses { - my $self = shift; - my $meta = $self->associated_metaclass; - if (Class::MOP::metaclass_is_weak($meta->name)) { - return ( - $meta->_inline_set_mop_slot( - '$instance', 'Class::MOP::class_of($class)' - ) . ';' - ); - } - else { - return (); - } -} - -sub _generate_default_value { - my $self = shift; - my ($attr, $index) = @_; - - if ($attr->has_default) { - # NOTE: - # default values can either be CODE refs - # in which case we need to call them. Or - # they can be scalars (strings/numbers) - # in which case we can just deal with them - # in the code we eval. - if ($attr->is_default_a_coderef) { - return '$defaults->[' . $index . ']->($instance)'; - } - else { - return '$defaults->[' . $index . ']'; - } - } - elsif ($attr->has_builder) { - return '$instance->' . $attr->builder; - } - else { - return; - } -} - 1; __END__