From: Guillermo Roditi Date: Mon, 29 Dec 2008 17:17:08 +0000 (+0000) Subject: fix recreated accessor bug X-Git-Tag: 0.00700~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=54a5b50a9b52ee4a7c28babe6381dc3143bb1b73;p=gitmo%2FMooseX-Emulate-Class-Accessor-Fast.git fix recreated accessor bug --- diff --git a/Changes b/Changes index 6672b31..99133a6 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,7 @@ +0.00700 + - Creating a new accessor with the same name as an old one would result in + a new attribute with no reader/writer/accessor. Reported by t0m + - tests (t0m) 0.00600 Dec 17, 2008 - Add test for a 'meta' accessor, which we need to treat as a special case (t0m) diff --git a/lib/MooseX/Emulate/Class/Accessor/Fast.pm b/lib/MooseX/Emulate/Class/Accessor/Fast.pm index 0f1fbdd..c37ef35 100644 --- a/lib/MooseX/Emulate/Class/Accessor/Fast.pm +++ b/lib/MooseX/Emulate/Class/Accessor/Fast.pm @@ -103,6 +103,8 @@ sub mk_accessors{ my $self = shift; my $meta = $locate_metaclass->($self); for my $attr_name (@_){ + $meta->remove_attribute($attr_name) + if $meta->find_attribute_by_name($attr_name); my $reader = $self->accessor_name_for($attr_name); my $writer = $self->mutator_name_for( $attr_name); @@ -134,6 +136,8 @@ sub mk_ro_accessors{ my $self = shift; my $meta = $locate_metaclass->($self); for my $attr_name (@_){ + $meta->remove_attribute($attr_name) + if $meta->find_attribute_by_name($attr_name); my $reader = $self->accessor_name_for($attr_name); my @opts = ($meta->has_method($reader) ? () : (reader => $reader) ); my $attr = $meta->add_attribute($attr_name, @opts); @@ -155,6 +159,8 @@ sub mk_wo_accessors{ my $self = shift; my $meta = $locate_metaclass->($self); for my $attr_name (@_){ + $meta->remove_attribute($attr_name) + if $meta->find_attribute_by_name($attr_name); my $writer = $self->mutator_name_for($attr_name); my @opts = ($meta->has_method($writer) ? () : (writer => $writer) ); my $attr = $meta->add_attribute($attr_name, @opts); diff --git a/t/recreated_accessors.t b/t/recreated_accessors.t new file mode 100644 index 0000000..8b63b59 --- /dev/null +++ b/t/recreated_accessors.t @@ -0,0 +1,32 @@ +#!/usr/bin/perl -w + +use strict; +use warnings; +use Test::More tests => 4; +use Test::Exception; + +# 1 +use_ok('MooseX::Emulate::Class::Accessor::Fast'); +{ + package My::Test::Package; + use Moose; + with 'MooseX::Emulate::Class::Accessor::Fast'; + for (0..1) { + __PACKAGE__->mk_accessors(qw( foo )); + __PACKAGE__->mk_ro_accessors(qw( bar )); + __PACKAGE__->mk_wo_accessors(qw( baz )); + } +} + +my $i = My::Test::Package->new(bar => 'bar'); + +# 2 +lives_ok { + $i->foo('foo'); + $i->baz('baz'); + + # 3-4 + is($i->foo, 'foo'); + is($i->bar, 'bar'); +} 'No exception'; +