getting close to a 0.07 release
[gitmo/Class-MOP.git] / examples / AttributesWithHistory.pod
CommitLineData
343203ee 1
2package # hide the package from PAUSE
3 AttributesWithHistory;
4
5use strict;
6use warnings;
7
99e5b7e8 8our $VERSION = '0.02';
343203ee 9
10use base 'Class::MOP::Attribute';
11
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',
20 ))
21);
22
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 { [] },
29 ))
30);
31
32# generate the methods
33
34sub generate_history_accessor_method {
35 my ($self, $attr_name) = @_;
36 eval qq{sub {
37 \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\};
38 }};
39}
40
41sub generate_accessor_method {
42 my ($self, $attr_name) = @_;
43 eval qq{sub {
44 if (scalar(\@_) == 2) {
45 push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\} => \$_[1];
46 \$_[0]->{'$attr_name'} = \$_[1];
47 }
48 \$_[0]->{'$attr_name'};
49 }};
50}
51
52sub generate_writer_method {
53 my ($self, $attr_name) = @_;
54 eval qq{sub {
55 push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()\} => \$_[1];
56 \$_[0]->{'$attr_name'} = \$_[1];
57 }};
58}
59
60sub install_accessors {
61 my $self = shift;
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();
68 return;
69}
70
711;
72
73=pod
74
75=head1 NAME
76
77AttributesWithHistory - An example attribute metaclass which keeps a history of changes
78
79=head1 SYSNOPSIS
80
81 package Foo;
82
343203ee 83 Foo->meta->add_attribute(AttributesWithHistory->new('foo' => (
84 accessor => 'foo',
85 history_accessor => 'get_foo_history',
86 )));
87
88 Foo->meta->add_attribute(AttributesWithHistory->new('bar' => (
89 reader => 'get_bar',
90 writer => 'set_bar',
91 history_accessor => 'get_bar_history',
92 )));
93
94 sub new {
95 my $class = shift;
5659d76e 96 $class->meta->new_object(@_);
343203ee 97 }
98
99=head1 DESCRIPTION
100
101This is an example of an attribute metaclass which keeps a
102record of all the values it has been assigned. It stores the
103history as a field in the attribute meta-object, and will
104autogenerate a means of accessing that history for the class
105which these attributes are added too.
106
107=head1 AUTHOR
108
109Stevan Little E<lt>stevan@iinteractive.comE<gt>
110
111=head1 COPYRIGHT AND LICENSE
112
113Copyright 2006 by Infinity Interactive, Inc.
114
115L<http://www.iinteractive.com>
116
117This library is free software; you can redistribute it and/or modify
118it under the same terms as Perl itself.
119
120=cut