X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=examples%2FAttributesWithHistory.pod;h=54fcdc4f425881fae9c5f59aeada250900801cbd;hb=1be561756ae40c4237d55ed010b5121762d473b0;hp=e355f912273eda9fc3fef89fe12c9fe91722bfd1;hpb=a4258ffd7a0a2bb8db5f01936068185d4f879b1a;p=gitmo%2FClass-MOP.git diff --git a/examples/AttributesWithHistory.pod b/examples/AttributesWithHistory.pod index e355f91..54fcdc4 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; @@ -106,13 +118,15 @@ history as a field in the attribute meta-object, and will autogenerate a means of accessing that history for the class which these attributes are added too. -=head1 AUTHOR +=head1 AUTHORS Stevan Little Estevan@iinteractive.comE +Yuval Kogman Enothingmuch@woobling.comE + =head1 COPYRIGHT AND LICENSE -Copyright 2006 by Infinity Interactive, Inc. +Copyright 2006-2008 by Infinity Interactive, Inc. L