From: Guillermo Roditi Date: Sat, 22 Nov 2008 18:25:26 +0000 (+0000) Subject: make_accessor, make_ro_accessor, make_rw_accessor X-Git-Tag: 0.00500~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8eb9108bc536b72c729ed2f454085f429472a2ff;p=gitmo%2FMooseX-Emulate-Class-Accessor-Fast.git make_accessor, make_ro_accessor, make_rw_accessor --- diff --git a/Changes b/Changes index a3b2425..42201c7 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,5 @@ +0.00500 + - make_accessor, make_ro_accessor, make_rw_accessor 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) diff --git a/lib/MooseX/Emulate/Class/Accessor/Fast.pm b/lib/MooseX/Emulate/Class/Accessor/Fast.pm index 27e19ca..f87ca44 100644 --- a/lib/MooseX/Emulate/Class/Accessor/Fast.pm +++ b/lib/MooseX/Emulate/Class/Accessor/Fast.pm @@ -226,6 +226,36 @@ sub get{ return @values; } +sub make_accessor { + my($class, $field) = @_; + my $meta = $class->meta; + my $attr = $meta->find_attribute_by_name($field) || $meta->add_attribute($field); + my $reader = $attr->get_read_method_ref; + my $writer = $attr->get_write_method_ref; + return sub { + my $self = shift; + return $self->$reader unless @_; + return $self->$writer((@_ > 1 ? [@_] : @_)); + } +} + + +sub make_ro_accessor { + my($class, $field) = @_; + my $meta = $class->meta; + my $attr = $meta->find_attribute_by_name($field) || $meta->add_attribute($field); + return $attr->get_read_method_ref; +} + + +sub make_wo_accessor { + my($class, $field) = @_; + my $meta = $class->meta; + my $attr = $meta->find_attribute_by_name($field) || $meta->add_attribute($field); + return $attr->get_write_method_ref; +} + + 1; =head2 meta