From: Yuval Kogman Date: Sat, 27 Jun 2009 01:58:58 +0000 (-0400) Subject: Refactor attribute methods to eliminate redundancy X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d4fbde0bbbde3133a43396d82c6d6d24847a18c9;p=gitmo%2FClass-MOP.git Refactor attribute methods to eliminate redundancy Moved _inialize_body to Method::Inlined, and all the deprecated methods to ::Generated, so that the various subclasses contain a minimal definition. When traits are introduced to Class::MOP Generated, Inlined and Attribute should become traits. --- diff --git a/lib/Class/MOP/Method/Accessor.pm b/lib/Class/MOP/Method/Accessor.pm index c36026c..56d6296 100644 --- a/lib/Class/MOP/Method/Accessor.pm +++ b/lib/Class/MOP/Method/Accessor.pm @@ -13,26 +13,8 @@ our $AUTHORITY = 'cpan:STEVAN'; use base 'Class::MOP::Method::Attribute'; -sub _initialize_body { - my $self = shift; - - my $method_name = join "_" => ( - '_generate', - 'method', - ($self->is_inline ? 'inline' : ()) - ); - - $self->{'body'} = $self->$method_name(); -} - ## generators -sub generate_method { - Carp::cluck('The generate_accessor_method method has been made private.' - . " The public version is deprecated and will be removed in a future release.\n"); - shift->_generate_method; -} - sub _generate_method { my $attr = (shift)->associated_attribute; return sub { @@ -43,12 +25,6 @@ sub _generate_method { ## Inline methods -sub generate_method_inline { - Carp::cluck('The generate_accessor_method_inline method has been made private.' - . " The public version is deprecated and will be removed in a future release.\n"); - shift->_generate_method_inline; -} - sub _generate_method_inline { my $self = shift; my $attr = $self->associated_attribute; diff --git a/lib/Class/MOP/Method/Attribute.pm b/lib/Class/MOP/Method/Attribute.pm index 468d526..927023c 100644 --- a/lib/Class/MOP/Method/Attribute.pm +++ b/lib/Class/MOP/Method/Attribute.pm @@ -11,7 +11,7 @@ our $VERSION = '0.88'; $VERSION = eval $VERSION; our $AUTHORITY = 'cpan:STEVAN'; -use base 'Class::MOP::Method::Generated'; +use base 'Class::MOP::Method::Inlined'; sub new { my $class = shift; @@ -51,12 +51,6 @@ sub associated_attribute { (shift)->{'attribute'} } ## factory -sub initialize_body { - Carp::cluck('The initialize_body method has been made private.' - . " The public version is deprecated and will be removed in a future release.\n"); - shift->_initialize_body; -} - 1; # XXX - UPDATE DOCS diff --git a/lib/Class/MOP/Method/Clearer.pm b/lib/Class/MOP/Method/Clearer.pm index 226a911..131d46f 100644 --- a/lib/Class/MOP/Method/Clearer.pm +++ b/lib/Class/MOP/Method/Clearer.pm @@ -13,26 +13,8 @@ our $AUTHORITY = 'cpan:STEVAN'; use base 'Class::MOP::Method::Attribute'; -sub _initialize_body { - my $self = shift; - - my $method_name = join "_" => ( - '_generate', - 'method', - ($self->is_inline ? 'inline' : ()) - ); - - $self->{'body'} = $self->$method_name(); -} - ## generators -sub generate_method { - Carp::cluck('The generate_clearer_method method has been made private.' - . " The public version is deprecated and will be removed in a future release.\n"); - shift->_generate_method; -} - sub _generate_method { my $attr = (shift)->associated_attribute; return sub { @@ -42,12 +24,6 @@ sub _generate_method { ## Inline methods -sub generate_method_inline { - Carp::cluck('The generate_clearer_method_inline method has been made private.' - . " The public version is deprecated and will be removed in a future release.\n"); - shift->_generate_method_inline; -} - sub _generate_method_inline { my $self = shift; my $attr = $self->associated_attribute; diff --git a/lib/Class/MOP/Method/Generated.pm b/lib/Class/MOP/Method/Generated.pm index d717f83..5589cda 100644 --- a/lib/Class/MOP/Method/Generated.pm +++ b/lib/Class/MOP/Method/Generated.pm @@ -16,14 +16,12 @@ use constant _PRINT_SOURCE => $ENV{MOP_PRINT_SOURCE} ? 1 : 0; ## accessors +# FIXME refactor into a trait when those are introduced to Class::MOP + sub new { confess __PACKAGE__ . " is an abstract base class, you must provide a constructor."; } -sub is_inline { $_[0]{is_inline} } - -sub definition_context { $_[0]{definition_context} } - sub _initialize_body { confess "No body to initialize, " . __PACKAGE__ . " is an abstract base class"; } @@ -31,11 +29,7 @@ sub _initialize_body { sub body { my $self = shift; - $self->{'body'} ||= do { - $self->_initialize_body; - }; - - return $self->{'body'}; + $self->{'body'} ||= $self->_initialize_body; } sub _eval_closure { @@ -101,6 +95,25 @@ sub _compile_code { return $self->_eval_closure($args{environment}, $code); } +sub generate_method { + Carp::cluck('The generate_reader_method method has been made private.' + . " The public version is deprecated and will be removed in a future release.\n"); + shift->_generate_method; +} + +sub generate_method_inline { + Carp::cluck('The generate_reader_method_inline method has been made private.' + . " The public version is deprecated and will be removed in a future release.\n"); + shift->_generate_method_inline; +} + +sub initialize_body { + Carp::cluck('The initialize_body method has been made private.' + . " The public version is deprecated and will be removed in a future release.\n"); + shift->_initialize_body; +} + + 1; __END__ diff --git a/lib/Class/MOP/Method/Inlined.pm b/lib/Class/MOP/Method/Inlined.pm index b374da1..974aabd 100644 --- a/lib/Class/MOP/Method/Inlined.pm +++ b/lib/Class/MOP/Method/Inlined.pm @@ -12,6 +12,30 @@ our $AUTHORITY = 'cpan:STEVAN'; use base 'Class::MOP::Method::Generated'; +# FIXME refactor into a trait when those are introduced to Class::MOP + +sub generate_method_inline { + confess "No inline body to generate, " . __PACKAGE__ . " is an abstract base class"; +} + +sub _generate_method { + confess "No body to generate, " . __PACKAGE__ . " is an abstract base class"; +} + +sub is_inline { $_[0]{is_inline} } + +sub definition_context { $_[0]{definition_context} } + +sub _initialize_body { + my $self = shift; + + if ( $self->is_inline ) { + return $self->_generate_method_inline; + } else { + return $self->_generate_method; + } +} + sub _expected_method_class { $_[0]{_expected_method_class} } sub _uninlined_body { diff --git a/lib/Class/MOP/Method/Predicate.pm b/lib/Class/MOP/Method/Predicate.pm index 74b1626..49557c9 100644 --- a/lib/Class/MOP/Method/Predicate.pm +++ b/lib/Class/MOP/Method/Predicate.pm @@ -13,26 +13,8 @@ our $AUTHORITY = 'cpan:STEVAN'; use base 'Class::MOP::Method::Attribute'; -sub _initialize_body { - my $self = shift; - - my $method_name = join "_" => ( - '_generate', - 'method', - ($self->is_inline ? 'inline' : ()) - ); - - $self->{'body'} = $self->$method_name(); -} - ## generators -sub generate_method { - Carp::cluck('The generate_predicate_method method has been made private.' - . " The public version is deprecated and will be removed in a future release.\n"); - shift->_generate_method; -} - sub _generate_method { my $attr = (shift)->associated_attribute; return sub { @@ -42,12 +24,6 @@ sub _generate_method { ## Inline methods -sub generate_method_inline { - Carp::cluck('The generate_predicate_method_inline method has been made private.' - . " The public version is deprecated and will be removed in a future release.\n"); - shift->_generate_predicate_method_inline; -} - sub _generate_method_inline { my $self = shift; my $attr = $self->associated_attribute; diff --git a/lib/Class/MOP/Method/Reader.pm b/lib/Class/MOP/Method/Reader.pm index 16e636b..13ae5da 100644 --- a/lib/Class/MOP/Method/Reader.pm +++ b/lib/Class/MOP/Method/Reader.pm @@ -17,26 +17,8 @@ sub associated_attribute { (shift)->{'attribute'} } ## factory -sub _initialize_body { - my $self = shift; - - my $method_name = join "_" => ( - '_generate', - 'method', - ($self->is_inline ? 'inline' : ()) - ); - - $self->{'body'} = $self->$method_name(); -} - ## generators -sub generate_method { - Carp::cluck('The generate_reader_method method has been made private.' - . " The public version is deprecated and will be removed in a future release.\n"); - shift->_generate_method; -} - sub _generate_method { my $attr = (shift)->associated_attribute; return sub { @@ -47,12 +29,6 @@ sub _generate_method { ## Inline methods -sub generate_method_inline { - Carp::cluck('The generate_reader_method_inline method has been made private.' - . " The public version is deprecated and will be removed in a future release.\n"); - shift->_generate_method_inline; -} - sub _generate_method_inline { my $self = shift; my $attr = $self->associated_attribute; diff --git a/lib/Class/MOP/Method/Writer.pm b/lib/Class/MOP/Method/Writer.pm index 8dc1a16..affe20a 100644 --- a/lib/Class/MOP/Method/Writer.pm +++ b/lib/Class/MOP/Method/Writer.pm @@ -13,26 +13,8 @@ our $AUTHORITY = 'cpan:STEVAN'; use base 'Class::MOP::Method::Attribute'; -sub _initialize_body { - my $self = shift; - - my $method_name = join "_" => ( - '_generate', - 'method', - ($self->is_inline ? 'inline' : ()) - ); - - $self->{'body'} = $self->$method_name(); -} - ## generators -sub generate_method { - Carp::cluck('The generate_writer_method method has been made private.' - . " The public version is deprecated and will be removed in a future release.\n"); - shift->_generate_method; -} - sub _generate_method { my $attr = (shift)->associated_attribute; return sub { @@ -42,12 +24,6 @@ sub _generate_method { ## Inline methods -sub generate_method_inline { - Carp::cluck('The generate_writer_method_inline method has been made private.' - . " The public version is deprecated and will be removed in a future release.\n"); - shift->_generate_method_inline; -} - sub _generate_method_inline { my $self = shift; my $attr = $self->associated_attribute;