X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=examples%2FAttributesWithHistory.pod;h=5e33d0d6848d7ad45e5dc5502bc90642f99300d6;hb=ba38bf08d30369c19a2c25997a0243c0d30be3d5;hp=3d2128187dfed5735ca30836e31e85d7534c814f;hpb=3b61bf22f75f994881c038d8f28180bca4e4d99a;p=gitmo%2FClass-MOP.git diff --git a/examples/AttributesWithHistory.pod b/examples/AttributesWithHistory.pod index 3d21281..5e33d0d 100644 --- a/examples/AttributesWithHistory.pod +++ b/examples/AttributesWithHistory.pod @@ -5,7 +5,7 @@ package # hide the package from PAUSE use strict; use warnings; -our $VERSION = '0.04'; +our $VERSION = '0.05'; use base 'Class::MOP::Attribute'; @@ -25,10 +25,30 @@ AttributesWithHistory->meta->add_attribute('_history' => ( default => sub { {} }, )); +sub accessor_metaclass { 'AttributesWithHistory::Method::Accessor' } + +AttributesWithHistory->meta->add_after_method_modifier('install_accessors' => sub { + my ($self) = @_; + # and now add the history accessor + $self->associated_class->add_method( + $self->process_accessors('history_accessor' => $self->history_accessor()) + ) if $self->has_history_accessor(); +}); + +package # hide the package from PAUSE + AttributesWithHistory::Method::Accessor; + +use strict; +use warnings; + +our $VERSION = '0.01'; + +use base 'Class::MOP::Method::Accessor'; + # generate the methods sub generate_history_accessor_method { - my ($self, $attr_name) = @_; + my $attr_name = (shift)->associated_attribute->name; eval qq{sub { unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{ \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = []; @@ -38,7 +58,7 @@ sub generate_history_accessor_method { } sub generate_accessor_method { - my ($self, $attr_name) = @_; + my $attr_name = (shift)->associated_attribute->name; eval qq{sub { if (scalar(\@_) == 2) { unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{ @@ -52,7 +72,7 @@ sub generate_accessor_method { } sub generate_writer_method { - my ($self, $attr_name) = @_; + my $attr_name = (shift)->associated_attribute->name; eval qq{sub { unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{ \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = []; @@ -60,15 +80,7 @@ sub generate_writer_method { push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\} => \$_[1]; \$_[0]->{'$attr_name'} = \$_[1]; }}; -} - -AttributesWithHistory->meta->add_after_method_modifier('install_accessors' => sub { - my ($self) = @_; - # and now add the history accessor - $self->associated_class->add_method( - $self->process_accessors('history_accessor' => $self->history_accessor()) - ) if $self->has_history_accessor(); -}); +} 1;