From: Karen Etheridge Date: Wed, 1 Jun 2011 00:40:09 +0000 (+0000) Subject: call all triggers in rebless_instance - tests now pass. X-Git-Tag: 2.0103~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8005c51c215a30792afaa91dda43b739ef0e4520;p=gitmo%2FMoose.git call all triggers in rebless_instance - tests now pass. refactored common code out into a sub. As it happens, this is sufficient even for the immutable path, because $meta->rebless_instance is not inlined, even though there is a sub in CMOP for generating inlineable code! --- diff --git a/lib/Class/MOP/Class.pm b/lib/Class/MOP/Class.pm index 45dfd46..856053a 100644 --- a/lib/Class/MOP/Class.pm +++ b/lib/Class/MOP/Class.pm @@ -727,6 +727,7 @@ sub _create_meta_instance { return $instance; } +# TODO: this is actually not being used! sub _inline_rebless_instance { my $self = shift; diff --git a/lib/Moose/Meta/Class.pm b/lib/Moose/Meta/Class.pm index 1e9173b..01b548f 100644 --- a/lib/Moose/Meta/Class.pm +++ b/lib/Moose/Meta/Class.pm @@ -267,14 +267,22 @@ sub new_object { my $params = @_ == 1 ? $_[0] : {@_}; my $object = $self->SUPER::new_object($params); - foreach my $attr ( $self->get_all_attributes() ) { + $self->call_all_triggers($object, $params); + + $object->BUILDALL($params) if $object->can('BUILDALL'); + + return $object; +} + +sub call_all_triggers { + my ($self, $object, $params) = @_; + + foreach my $attr ($self->get_all_attributes()) { next unless $attr->can('has_trigger') && $attr->has_trigger; my $init_arg = $attr->init_arg; - next unless defined $init_arg; - next unless exists $params->{$init_arg}; $attr->trigger->( @@ -286,10 +294,6 @@ sub new_object { ), ); } - - $object->BUILDALL($params) if $object->can('BUILDALL'); - - return $object; } sub _generate_fallback_constructor { @@ -742,6 +746,18 @@ sub _immutable_options { ); } + +sub _fixup_attributes_after_rebless { + my $self = shift; + my ($instance, $rebless_from, %params) = @_; + + $self->SUPER::_fixup_attributes_after_rebless($instance, $rebless_from, %params); + + $self->call_all_triggers($instance, \%params); +} + + + ## ------------------------------------------------- our $error_level; diff --git a/t/basics/rebless.t b/t/basics/rebless.t index 18a2812..c443ec5 100644 --- a/t/basics/rebless.t +++ b/t/basics/rebless.t @@ -64,6 +64,7 @@ subtype 'Positive' initializer => sub { my ($self, $value, $set, $attr) = @_; $initializer_calls{new_attr}++; + $set->($value); }, ); } @@ -99,10 +100,7 @@ with_immutable like( exception { $foo->type_constrained(10.5) }, qr/^Attribute \(type_constrained\) does not pass the type constraint because\: Validation failed for 'Int' with value 10\.5/, '... this failed because of type check' ); - TODO: { - local $TODO = 'Moose::Meta::Class does not yet call triggers for rebless_instance!'; is_deeply(\%Child::trigger_calls, { new_attr => 1 }, 'Trigger fired on rebless_instance'); - } is_deeply(\%Child::initializer_calls, { new_attr => 1 }, 'Initializer fired on rebless_instance'); undef %Child::trigger_calls;