From: Guillermo Roditi Date: Mon, 21 May 2007 17:20:33 +0000 (+0000) Subject: get_read_method and get_write_method + tests + POD X-Git-Tag: 0_38~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=b25109b1bb81739adfbb5135176792bf5d0e3feb;p=gitmo%2FClass-MOP.git get_read_method and get_write_method + tests + POD --- diff --git a/Changes b/Changes index 2ecf56f..0d135cc 100644 --- a/Changes +++ b/Changes @@ -5,6 +5,9 @@ Revision history for Perl extension Class-MOP. * Class::MOP::Package - we now deal with stub methods properly - added tests for this + * Class::MOP::Attribute + - added get_read_method and get_write_method + - added tests and POD for this 0.37 Sat. March 10, 2007 ~~ Many, many documentation updates ~~ diff --git a/lib/Class/MOP/Attribute.pm b/lib/Class/MOP/Attribute.pm index 941f90a..67e07d5 100644 --- a/lib/Class/MOP/Attribute.pm +++ b/lib/Class/MOP/Attribute.pm @@ -116,6 +116,9 @@ sub init_arg { $_[0]->{'$!init_arg'} } # end bootstrapped away method section. # (all methods below here are kept intact) +sub get_read_method { $_[0]->reader || $_[0]->accessor } +sub get_write_method { $_[0]->writer || $_[0]->accessor } + sub is_default_a_coderef { ('CODE' eq (reftype($_[0]->{'$!default'} || $_[0]->{default}) || '')) } @@ -515,6 +518,14 @@ argument C<$instance> into it and return the value. Returns a list of slots required by the attribute. This is usually just one, which is the name of the attribute. +=item B + +=item B + +Return the name of a method suitable for reading / writing the value of the +attribute in the associated class. Suitable for use whether C and +C or C was used. + =back =head2 Informational predicates diff --git a/t/005_attributes.t b/t/005_attributes.t index 73bda5b..239d2a2 100644 --- a/t/005_attributes.t +++ b/t/005_attributes.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 43; +use Test::More tests => 47; use Test::Exception; BEGIN { @@ -53,6 +53,10 @@ my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar'); ::ok($meta->has_attribute('$bar'), '... Bar has $bar attribute'); ::is($meta->get_attribute('$bar'), $BAR_ATTR, '... got the right attribute back for Bar'); + my $attr = $meta->get_attribute('$bar'); + ::is($attr->get_read_method, 'bar', '... got the right read method for Bar'); + ::is($attr->get_write_method, 'bar', '... got the right write method for Bar'); + ::ok($meta->has_method('bar'), '... an accessor has been created'); ::isa_ok($meta->get_method('bar'), 'Class::MOP::Method::Accessor'); } @@ -67,6 +71,10 @@ my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar'); ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute'); ::is($meta->get_attribute('$baz'), $BAZ_ATTR, '... got the right attribute back for Baz'); + my $attr = $meta->get_attribute('$baz'); + ::is($attr->get_read_method, 'get_baz', '... got the right read method for Baz'); + ::is($attr->get_write_method, 'set_baz', '... got the right write method for Baz'); + ::ok($meta->has_method('get_baz'), '... a reader has been created'); ::ok($meta->has_method('set_baz'), '... a writer has been created'); diff --git a/t/014_attribute_introspection.t b/t/014_attribute_introspection.t index 7fe3b0e..d2d9103 100644 --- a/t/014_attribute_introspection.t +++ b/t/014_attribute_introspection.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 49; +use Test::More tests => 51; use Test::Exception; BEGIN { @@ -27,8 +27,8 @@ BEGIN { name has_accessor accessor - has_writer writer - has_reader reader + has_writer writer get_write_method + has_reader reader get_read_method has_predicate predicate has_clearer clearer has_init_arg init_arg