yuval-wins
[gitmo/Class-MOP.git] / examples / AttributesWithHistory.pod
1
2 package # hide the package from PAUSE
3     AttributesWithHistory;
4
5 use strict;
6 use warnings;
7
8 our $VERSION = '0.04';
9
10 use 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 AttributesWithHistory->meta->add_attribute('history_accessor' => (
16     reader    => 'history_accessor',
17     init_arg  => 'history_accessor',
18     predicate => 'has_history_accessor',
19 ));
20
21 # this is a place to store the actual 
22 # history of the attribute
23 AttributesWithHistory->meta->add_attribute('_history' => (
24     accessor => '_history',
25     default  => sub { {} },
26 ));
27
28 # generate the methods
29
30 sub generate_history_accessor_method {
31     my ($self, $attr_name) = @_; 
32     eval qq{sub {
33         unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{
34             \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = [];    
35         \}
36         \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\};        
37     }};    
38 }
39
40 sub generate_accessor_method {
41     my ($self, $attr_name) = @_;
42     eval qq{sub {
43         if (scalar(\@_) == 2) {
44             unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{
45                 \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = [];    
46             \}            
47             push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\} => \$_[1];
48             \$_[0]->{'$attr_name'} = \$_[1];
49         }
50         \$_[0]->{'$attr_name'};
51     }};
52 }
53
54 sub generate_writer_method {
55     my ($self, $attr_name) = @_; 
56     eval qq{sub {
57         unless (ref \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}) \{
58             \$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\} = [];    
59         \}        
60         push \@\{\$_[0]->meta->get_attribute('$attr_name')->_history()->\{\$_[0]\}\} => \$_[1];        
61         \$_[0]->{'$attr_name'} = \$_[1];
62     }};
63 }
64
65 AttributesWithHistory->meta->add_after_method_modifier('install_accessors' => sub {
66     my ($self) = @_;
67     # and now add the history accessor
68     $self->associated_class->add_method(
69         $self->process_accessors('history_accessor' => $self->history_accessor())
70     ) if $self->has_history_accessor();
71 });
72
73 1;
74
75 =pod
76
77 =head1 NAME
78
79 AttributesWithHistory - An example attribute metaclass which keeps a history of changes
80
81 =head1 SYSNOPSIS
82   
83   package Foo;
84   
85   Foo->meta->add_attribute(AttributesWithHistory->new('foo' => (
86       accessor         => 'foo',
87       history_accessor => 'get_foo_history',
88   )));    
89   
90   Foo->meta->add_attribute(AttributesWithHistory->new('bar' => (
91       reader           => 'get_bar',
92       writer           => 'set_bar',
93       history_accessor => 'get_bar_history',
94   )));    
95   
96   sub new  {
97       my $class = shift;
98       $class->meta->new_object(@_);
99   }
100   
101 =head1 DESCRIPTION
102
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.
108
109 =head1 AUTHOR
110
111 Stevan Little E<lt>stevan@iinteractive.comE<gt>
112
113 =head1 COPYRIGHT AND LICENSE
114
115 Copyright 2006 by Infinity Interactive, Inc.
116
117 L<http://www.iinteractive.com>
118
119 This library is free software; you can redistribute it and/or modify
120 it under the same terms as Perl itself.
121
122 =cut