use Moose::Role;
-our $VERSION = '0.00300';
+our $VERSION = '0.00400';
=head1 NAME
for my $attr_name (@_){
my $reader = $self->accessor_name_for($attr_name);
my $writer = $self->mutator_name_for( $attr_name);
+
#dont overwrite existing methods
- my @opts = $reader eq $writer ?
- ( $self->can($reader) ? () : (accessor => $reader) ) :
- (
- ( $self->can($reader) ? () : (reader => $reader) ),
- ( $self->can($writer) ? () : (writer => $writer) ),
- );
- $meta->add_attribute($attr_name, @opts);
-
- $meta->add_method("_${attr_name}_accessor", $self->can($reader) )
- if($reader eq $attr_name && !$self->can("_${attr_name}_accessor") );
+ if($reader eq $writer){
+ my %opts = ( $self->can($reader) ? () : (accessor => $reader) );
+ my $attr = $meta->add_attribute($attr_name, %opts);
+ if($attr_name eq $reader){
+ my $alias = "_${attr_name}_accessor";
+ next if $self->can($alias);
+ my @alias_method = $opts{accessor} ? ( $alias => $self->can($reader) )
+ : ( $attr->process_accessors(accessor => $alias, 0 ) );
+ $meta->add_method(@alias_method);
+ }
+ } else {
+ my @opts = ( $self->can($writer) ? () : (writer => $writer) );
+ push(@opts, (reader => $reader)) unless $self->can($reader);
+ $meta->add_attribute($attr_name, @opts);
+ }
}
}
my $meta = $self->meta;
for my $attr_name (@_){
my $reader = $self->accessor_name_for($attr_name);
- $meta->add_attribute($attr_name,
- $self->can($reader) ? () : (reader => $reader) );
- $meta->add_method("_${attr_name}_accessor", $meta->find_method_by_name($reader))
- if($reader eq $attr_name && !$self->can("_${attr_name}_accessor") );
+ my @opts = ($self->can($reader) ? () : (reader => $reader) );
+ my $attr = $meta->add_attribute($attr_name, @opts);
+ if($reader eq $attr_name && $reader eq $self->mutator_name_for($attr_name)){
+ $meta->add_method("_${attr_name}_accessor" => $attr->get_read_method_ref)
+ unless $self->can("_${attr_name}_accessor");
+ }
}
}
my $meta = $self->meta;
for my $attr_name (@_){
my $writer = $self->mutator_name_for($attr_name);
- $meta->add_attribute($attr_name, $self->can($writer) ? () : (writer => $writer) );
- $meta->add_method("_${attr_name}_accessor", $meta->find_method_by_name($writer))
- if($writer eq $attr_name && !$self->can("_${attr_name}_accessor") );
+ my @opts = ($self->can($writer) ? () : (writer => $writer) );
+ my $attr = $meta->add_attribute($attr_name, @opts);
+ if($writer eq $attr_name && $writer eq $self->accessor_name_for($attr_name)){
+ $meta->add_method("_${attr_name}_accessor" => $attr->get_write_method_ref)
+ unless $self->can("_${attr_name}_accessor");
+ }
}
}
#!perl
use strict;
-use Test::More tests => 32;
+use Test::More tests => 33;
+use Test::Exception;
+
+use Class::MOP;
#1
require_ok("MooseX::Adopt::Class::Accessor::Fast");
my $class = "Testing::Class::Accessor::Fast";
{
- no strict 'refs';
- @{"${class}::ISA"} = ('Class::Accessor::Fast');
- *{"${class}::car"} = sub { shift->_car_accessor(@_); };
- *{"${class}::mar"} = sub { return "Overloaded"; };
+ my $infinite_loop_indicator = 0;
+ my $meta = Class::MOP::Class->create(
+ $class,
+ superclasses => ['Class::Accessor::Fast'],
+ methods => {
+ car => sub { shift->_car_accessor(@_); },
+ mar => sub { return "Overloaded"; },
+ test => sub {
+ die('Infinite loop detected') if $infinite_loop_indicator++;
+ $_[0]->_test_accessor((@_ > 1 ? @_ : ()));
+ }
+ }
+ );
- $class->mk_accessors(qw( foo bar yar car mar ));
+ $class->mk_accessors(qw( foo bar yar car mar test));
$class->mk_ro_accessors(qw(static unchanged));
$class->mk_wo_accessors(qw(sekret double_sekret));
$class->follow_best_practice;
my %attrs = map{$_->name => $_} $class->meta->compute_all_applicable_attributes;
#2
-is(keys %attrs, 10, 'Correct number of attributes');
+is(keys %attrs, 11, 'Correct number of attributes');
#3-12
ok(exists $attrs{$_}, "Attribute ${_} created")
for qw( foo bar yar car mar static unchanged sekret double_sekret best );
#13-21
-ok($class->can("_${_}_accessor"), "Attribute ${_} created")
+ok($class->can("_${_}_accessor"), "Alias method (_${_}_accessor) for ${_} created")
for qw( foo bar yar car mar static unchanged sekret double_sekret );
#22-24
#31,32
is( $attrs{'best'}->reader, 'get_best', "Reader get_best created");
is( $attrs{'best'}->writer, 'set_best', "Writer set_best created");
+
+#33
+lives_ok{ $class->new->test(1) } 'no auto-reference to accessors from aliases';