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 unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{
38 \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = [];
40 \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\};
44 sub generate_accessor_method {
45 my ($self, $attr_name) = @_;
47 if (scalar(\@_) == 2) {
48 unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{
49 \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = [];
51 push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\} => \$_[1];
52 \$_[0]->{'$attr_name'} = \$_[1];
54 \$_[0]->{'$attr_name'};
58 sub generate_writer_method {
59 my ($self, $attr_name) = @_;
61 unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{
62 \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = [];
64 push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\} => \$_[1];
65 \$_[0]->{'$attr_name'} = \$_[1];
69 sub install_accessors {
71 # do as we normall do ...
72 $self->SUPER::install_accessors();
73 # and now add the history accessor
74 $self->associated_class->add_method(
75 $self->process_accessors('history_accessor' => $self->history_accessor())
76 ) if $self->has_history_accessor();
86 AttributesWithHistory - An example attribute metaclass which keeps a history of changes
92 Foo->meta->add_attribute(AttributesWithHistory->new('foo' => (
94 history_accessor => 'get_foo_history',
97 Foo->meta->add_attribute(AttributesWithHistory->new('bar' => (
100 history_accessor => 'get_bar_history',
105 $class->meta->new_object(@_);
110 This is an example of an attribute metaclass which keeps a
111 record of all the values it has been assigned. It stores the
112 history as a field in the attribute meta-object, and will
113 autogenerate a means of accessing that history for the class
114 which these attributes are added too.
118 Stevan Little E<lt>stevan@iinteractive.comE<gt>
120 =head1 COPYRIGHT AND LICENSE
122 Copyright 2006 by Infinity Interactive, Inc.
124 L<http://www.iinteractive.com>
126 This library is free software; you can redistribute it and/or modify
127 it under the same terms as Perl itself.