Subject: | Proper C::A::F support requires support for the make_accessor, make_ro_accessor and make_wo_accessor methods. |
Hiya.
A number of things which use Catalyst (e.g. C::C::FormBuilder) expect to
be able to find the make_accessor methods from C::A, but these are not
currently supported.
Patch attached which adds this support with tests, some additional
tests, and also fixes the issue in RT#40380.
Cheers
t0m
Subject: | MX-Emulate-CAF.diff |
Index: t/accessors.t
===================================================================
--- t/accessors.t (revision 6445)
+++ t/accessors.t (working copy)
@@ -1,6 +1,6 @@
#!perl
use strict;
-use Test::More tests => 33;
+use Test::More tests => 47;
use Test::Exception;
use Class::MOP;
@@ -28,6 +28,9 @@
$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->make_accessor('quux');
+ $class->make_ro_accessor('quux_ro');
+ $class->make_wo_accessor('quux_wo');
$class->follow_best_practice;
$class->mk_accessors(qw( best));
}
@@ -35,35 +38,44 @@
my %attrs = map{$_->name => $_} $class->meta->compute_all_applicable_attributes;
#2
-is(keys %attrs, 11, 'Correct number of attributes');
+is(keys %attrs, 14, 'Correct number of attributes');
-#3-12
+#3-15
ok(exists $attrs{$_}, "Attribute ${_} created")
- for qw( foo bar yar car mar static unchanged sekret double_sekret best );
+ for qw( foo bar yar car mar static unchanged sekret double_sekret best quux quux_ro quux_wo);
-#13-21
+#16-27
ok($class->can("_${_}_accessor"), "Alias method (_${_}_accessor) for ${_} created")
- for qw( foo bar yar car mar static unchanged sekret double_sekret );
+ for qw( foo bar yar car mar static unchanged sekret double_sekret quux quux_ro quux_wo);
-#22-24
+#27-29
is( $attrs{$_}->accessor, $_, "Accessor ${_} created" )
for qw( foo bar yar);
-#25,26
+#30,32
ok( !$attrs{$_}->has_accessor, "Accessor ${_} not created" )
for qw( car mar);
-#27,28
+#33,36
is( $attrs{$_}->reader, $_, "Reader ${_} created")
- for qw( static unchanged );
+ for qw( static unchanged quux_ro );
-#29,30
+#37,39
+ok( !$attrs{$_}->writer, "No writer ${_} created")
+ for qw( static unchanged quux_ro );
+
+#40,43
is( $attrs{$_}->writer, $_, "Writer ${_} created")
- for qw(sekret double_sekret);
+ for qw(sekret double_sekret quux_wo );
-#31,32
+#43,46
+ok( !$attrs{$_}->reader, "No reader ${_} created")
+ for qw( sekret double_sekret quux_wo );
+
+#47,49
is( $attrs{'best'}->reader, 'get_best', "Reader get_best created");
is( $attrs{'best'}->writer, 'set_best', "Writer set_best created");
-#33
+#50
lives_ok{ $class->new->test(1) } 'no auto-reference to accessors from aliases';
+
Index: lib/MooseX/Emulate/Class/Accessor/Fast.pm
===================================================================
--- lib/MooseX/Emulate/Class/Accessor/Fast.pm (revision 6445)
+++ lib/MooseX/Emulate/Class/Accessor/Fast.pm (working copy)
@@ -20,9 +20,9 @@
#fields with readers and writers
__PACKAGE__->mk_accessors(qw/field1 field2/);
#fields with readers only
- __PACKAGE__->mk_accessors(qw/field3 field4/);
+ __PACKAGE__->mk_ro_accessors(qw/field3 field4/);
#fields with writers only
- __PACKAGE__->mk_accessors(qw/field5 field6/);
+ __PACKAGE__->mk_wo_accessors(qw/field5 field6/);
=head1 DESCRIPTION
@@ -117,6 +117,17 @@
}
}
+=head2 make_accessor $field
+
+Creates a single read-write accessor using mk_accessors
+
+=cut
+
+sub make_accessor{
+ my ( $self, $field ) = @_;
+ $self->mk_accessors($field);
+}
+
=head2 mk_ro_accessors @field_names
Create read-only accessors.
@@ -137,8 +148,19 @@
}
}
-=head2 mk_ro_accessors @field_names
+=head2 mk_ro_accessor $field
+Create a single read-only accessor
+
+=cut
+
+sub make_ro_accessor{
+ my ($self, $field) = @_;
+ $self->mk_ro_accessors($field);
+}
+
+=head2 mk_wo_accessors @field_names
+
Create write-only accessors.
=cut
@@ -158,6 +180,17 @@
}
}
+=head2 mk_wo_accessor $field
+
+Create a single write-only accessor
+
+=cut
+
+sub make_wo_accessor{
+ my ($self, $field) = @_;
+ $self->mk_wo_accessors($field);
+}
+
=head2 follow_best_practices
Preface readers with 'get_' and writers with 'set_'.
Index: Changes
===================================================================
--- Changes (revision 6445)
+++ Changes (working copy)
@@ -1,3 +1,8 @@
+0.00500
+ - Add support for the make_accessor, make_ro_accessor and
+ make_wo_accessor methods.
+ - Tests for these
+ - Correct typos in the module POD (RT#40380)
0.00400 Oct 28, 2008
- Fix bug where a bad assumption was causing us to infinitely loop
on badly-written code like Data::Page. (Reported by marcus)