Don't make a method object for calls to has_method, just for get_method
[gitmo/Class-MOP.git] / examples / AttributesWithHistory.pod
CommitLineData
343203ee 1
2package # hide the package from PAUSE
3 AttributesWithHistory;
4
5use strict;
6use warnings;
7
ba38bf08 8our $VERSION = '0.05';
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
1fdb86fb 15AttributesWithHistory->meta->add_attribute('history_accessor' => (
a4258ffd 16 reader => 'history_accessor',
17 init_arg => 'history_accessor',
18 predicate => 'has_history_accessor',
19));
343203ee 20
21# this is a place to store the actual
22# history of the attribute
1fdb86fb 23AttributesWithHistory->meta->add_attribute('_history' => (
a4258ffd 24 accessor => '_history',
25 default => sub { {} },
26));
343203ee 27
ba38bf08 28sub accessor_metaclass { 'AttributesWithHistory::Method::Accessor' }
29
30AttributesWithHistory->meta->add_after_method_modifier('install_accessors' => sub {
31 my ($self) = @_;
32 # and now add the history accessor
33 $self->associated_class->add_method(
45a183fb 34 $self->_process_accessors('history_accessor' => $self->history_accessor())
ba38bf08 35 ) if $self->has_history_accessor();
36});
37
38package # hide the package from PAUSE
39 AttributesWithHistory::Method::Accessor;
40
41use strict;
42use warnings;
43
44our $VERSION = '0.01';
45
46use base 'Class::MOP::Method::Accessor';
47
343203ee 48# generate the methods
49
afc92ac6 50sub _generate_history_accessor_method {
ba38bf08 51 my $attr_name = (shift)->associated_attribute->name;
343203ee 52 eval qq{sub {
0b8eb325 53 unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{
54 \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = [];
55 \}
56 \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\};
343203ee 57 }};
58}
59
afc92ac6 60sub _generate_accessor_method {
ba38bf08 61 my $attr_name = (shift)->associated_attribute->name;
343203ee 62 eval qq{sub {
63 if (scalar(\@_) == 2) {
0b8eb325 64 unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{
65 \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = [];
66 \}
67 push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\} => \$_[1];
343203ee 68 \$_[0]->{'$attr_name'} = \$_[1];
69 }
70 \$_[0]->{'$attr_name'};
71 }};
72}
73
afc92ac6 74sub _generate_writer_method {
ba38bf08 75 my $attr_name = (shift)->associated_attribute->name;
343203ee 76 eval qq{sub {
0b8eb325 77 unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{
78 \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = [];
79 \}
80 push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\} => \$_[1];
343203ee 81 \$_[0]->{'$attr_name'} = \$_[1];
82 }};
ba38bf08 83}
343203ee 84
851;
86
87=pod
88
89=head1 NAME
90
91AttributesWithHistory - An example attribute metaclass which keeps a history of changes
92
93=head1 SYSNOPSIS
94
95 package Foo;
96
343203ee 97 Foo->meta->add_attribute(AttributesWithHistory->new('foo' => (
98 accessor => 'foo',
99 history_accessor => 'get_foo_history',
100 )));
101
102 Foo->meta->add_attribute(AttributesWithHistory->new('bar' => (
103 reader => 'get_bar',
104 writer => 'set_bar',
105 history_accessor => 'get_bar_history',
106 )));
107
108 sub new {
109 my $class = shift;
5659d76e 110 $class->meta->new_object(@_);
343203ee 111 }
112
113=head1 DESCRIPTION
114
115This is an example of an attribute metaclass which keeps a
116record of all the values it has been assigned. It stores the
117history as a field in the attribute meta-object, and will
118autogenerate a means of accessing that history for the class
119which these attributes are added too.
120
1a09d9cc 121=head1 AUTHORS
343203ee 122
123Stevan Little E<lt>stevan@iinteractive.comE<gt>
124
1a09d9cc 125Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
126
343203ee 127=head1 COPYRIGHT AND LICENSE
128
69e3ab0a 129Copyright 2006-2008 by Infinity Interactive, Inc.
343203ee 130
131L<http://www.iinteractive.com>
132
133This library is free software; you can redistribute it and/or modify
134it under the same terms as Perl itself.
135
136=cut