package # hide the package from PAUSE AttributesWithHistory; use strict; use warnings; use Class::MOP 'meta'; our $VERSION = '0.01'; use base 'Class::MOP::Attribute'; # this is for an extra attribute constructor # option, which is to be able to create a # way for the class to access the history __PACKAGE__->meta->add_attribute( Class::MOP::Attribute->new('history_accessor' => ( reader => 'history_accessor', init_arg => 'history_accessor', predicate => 'has_history_accessor', )) ); # this is a place to store the actual # history of the attribute __PACKAGE__->meta->add_attribute( Class::MOP::Attribute->new('_history' => ( accessor => '_history', default => sub { [] }, )) ); # generate the methods sub generate_history_accessor_method { my ($self, $attr_name) = @_; eval qq{sub { \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\}; }}; } sub generate_accessor_method { my ($self, $attr_name) = @_; eval qq{sub { if (scalar(\@_) == 2) { push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\} => \$_[1]; \$_[0]->{'$attr_name'} = \$_[1]; } \$_[0]->{'$attr_name'}; }}; } sub generate_writer_method { my ($self, $attr_name) = @_; eval qq{sub { push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\} => \$_[1]; \$_[0]->{'$attr_name'} = \$_[1]; }}; } sub install_accessors { my $self = shift; # do as we normall do ... $self->SUPER::install_accessors(); # and now add the history accessor $self->associated_class->add_method( $self->process_accessors('history_accessor' => $self->history_accessor()) ) if $self->has_history_accessor(); return; } 1; =pod =head1 NAME AttributesWithHistory - An example attribute metaclass which keeps a history of changes =head1 SYSNOPSIS package Foo; use Class::MOP 'meta'; Foo->meta->add_attribute(AttributesWithHistory->new('foo' => ( accessor => 'foo', history_accessor => 'get_foo_history', ))); Foo->meta->add_attribute(AttributesWithHistory->new('bar' => ( reader => 'get_bar', writer => 'set_bar', history_accessor => 'get_bar_history', ))); sub new { my $class = shift; bless $class->meta->construct_instance(@_) => $class; } =head1 DESCRIPTION This is an example of an attribute metaclass which keeps a record of all the values it has been assigned. It stores the 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 Stevan Little Estevan@iinteractive.comE =head1 COPYRIGHT AND LICENSE Copyright 2006 by Infinity Interactive, Inc. L This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut