2 package # hide the package from PAUSE
10 use base 'Class::MOP::Attribute';
12 # this is for an extra attribute constructor
13 # option, which is to be able to create a
14 # way for the class to access the history
15 __PACKAGE__->meta->add_attribute(
16 Class::MOP::Attribute->new('history_accessor' => (
17 reader => 'history_accessor',
18 init_arg => 'history_accessor',
19 predicate => 'has_history_accessor',
23 # this is a place to store the actual
24 # history of the attribute
25 __PACKAGE__->meta->add_attribute(
26 Class::MOP::Attribute->new('_history' => (
27 accessor => '_history',
28 default => sub { [] },
32 # generate the methods
34 sub generate_history_accessor_method {
35 my ($self, $attr_name) = @_;
37 \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\};
41 sub generate_accessor_method {
42 my ($self, $attr_name) = @_;
44 if (scalar(\@_) == 2) {
45 push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\} => \$_[1];
46 \$_[0]->{'$attr_name'} = \$_[1];
48 \$_[0]->{'$attr_name'};
52 sub generate_writer_method {
53 my ($self, $attr_name) = @_;
55 push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\} => \$_[1];
56 \$_[0]->{'$attr_name'} = \$_[1];
60 sub install_accessors {
62 # do as we normall do ...
63 $self->SUPER::install_accessors();
64 # and now add the history accessor
65 $self->associated_class->add_method(
66 $self->process_accessors('history_accessor' => $self->history_accessor())
67 ) if $self->has_history_accessor();
77 AttributesWithHistory - An example attribute metaclass which keeps a history of changes
83 use Class::MOP 'meta';
85 Foo->meta->add_attribute(AttributesWithHistory->new('foo' => (
87 history_accessor => 'get_foo_history',
90 Foo->meta->add_attribute(AttributesWithHistory->new('bar' => (
93 history_accessor => 'get_bar_history',
98 bless $class->meta->construct_instance(@_) => $class;
103 This is an example of an attribute metaclass which keeps a
104 record of all the values it has been assigned. It stores the
105 history as a field in the attribute meta-object, and will
106 autogenerate a means of accessing that history for the class
107 which these attributes are added too.
111 Stevan Little E<lt>stevan@iinteractive.comE<gt>
113 =head1 COPYRIGHT AND LICENSE
115 Copyright 2006 by Infinity Interactive, Inc.
117 L<http://www.iinteractive.com>
119 This library is free software; you can redistribute it and/or modify
120 it under the same terms as Perl itself.