X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=examples%2FAttributesWithHistory.pod;h=e7ae1c26ac797298af47390118ead85bf72b8cfc;hb=f1af2436a0bc78cca31deb547233ef30983a2663;hp=fe712c849cc51a987ca7ca26b3a54585a58ec44d;hpb=99e5b7e8e18cc7593175b4586e56d53b41932556;p=gitmo%2FClass-MOP.git diff --git a/examples/AttributesWithHistory.pod b/examples/AttributesWithHistory.pod index fe712c8..e7ae1c2 100644 --- a/examples/AttributesWithHistory.pod +++ b/examples/AttributesWithHistory.pod @@ -5,68 +5,82 @@ package # hide the package from PAUSE use strict; use warnings; -our $VERSION = '0.02'; +our $VERSION = '0.05'; 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', - )) -); +AttributesWithHistory->meta->add_attribute('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 { [] }, - )) -); +AttributesWithHistory->meta->add_attribute('_history' => ( + accessor => '_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) = @_; +sub _generate_history_accessor_method { + my $attr_name = (shift)->associated_attribute->name; eval qq{sub { - \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\}; + unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{ + \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = []; + \} + \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\}; }}; } -sub generate_accessor_method { - my ($self, $attr_name) = @_; +sub _generate_accessor_method { + my $attr_name = (shift)->associated_attribute->name; eval qq{sub { if (scalar(\@_) == 2) { - push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\} => \$_[1]; + unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{ + \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = []; + \} + push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\} => \$_[1]; \$_[0]->{'$attr_name'} = \$_[1]; } \$_[0]->{'$attr_name'}; }}; } -sub generate_writer_method { - my ($self, $attr_name) = @_; +sub _generate_writer_method { + my $attr_name = (shift)->associated_attribute->name; eval qq{sub { - push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\} => \$_[1]; + unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{ + \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = []; + \} + push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\} => \$_[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; @@ -80,8 +94,6 @@ AttributesWithHistory - An example attribute metaclass which keeps a history of package Foo; - use Class::MOP 'meta'; - Foo->meta->add_attribute(AttributesWithHistory->new('foo' => ( accessor => 'foo', history_accessor => 'get_foo_history', @@ -95,7 +107,7 @@ AttributesWithHistory - An example attribute metaclass which keeps a history of sub new { my $class = shift; - bless $class->meta->construct_instance(@_) => $class; + $class->meta->new_object(@_); } =head1 DESCRIPTION @@ -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