fixing an example which I broke
[gitmo/Class-MOP.git] / examples / ClassEncapsulatedAttributes.pod
1
2 package # hide the package from PAUSE
3     ClassEncapsulatedAttributes;
4
5 use strict;
6 use warnings;
7
8 our $VERSION = '0.03';
9
10 use base 'Class::MOP::Class';
11
12 sub initialize { 
13     (shift)->SUPER::initialize(@_, 
14         # use the custom attribute metaclass here 
15         ':attribute_metaclass' => 'ClassEncapsulatedAttributes::Attribute' 
16     );
17 }
18
19 sub construct_instance {
20     my ($class, %params) = @_;
21     my $instance = {};
22     foreach my $current_class ($class->class_precedence_list()) {
23         $instance->{$current_class} = {} 
24             unless exists $instance->{$current_class};
25         my $meta = $current_class->meta;
26         foreach my $attr_name ($meta->get_attribute_list()) {
27             my $attr = $meta->get_attribute($attr_name);
28             # if the attr has an init_arg, use that, otherwise,
29             # use the attributes name itself as the init_arg
30             my $init_arg = $attr->init_arg();
31             # try to fetch the init arg from the %params ...
32             my $val;        
33             $val = $params{$current_class}->{$init_arg} 
34                 if exists $params{$current_class} && 
35                    exists ${$params{$current_class}}{$init_arg};
36             # if nothing was in the %params, we can use the 
37             # attribute's default value (if it has one)
38             $val ||= $attr->default($instance) if $attr->has_default();
39             # now add this to the instance structure
40             $instance->{$current_class}->{$attr_name} = $val;
41         }
42     }  
43     return $instance;
44 }
45
46 package # hide the package from PAUSE
47     ClassEncapsulatedAttributes::Attribute;
48
49 use strict;
50 use warnings;
51
52 our $VERSION = '0.02';
53
54 use base 'Class::MOP::Attribute';
55
56 sub generate_accessor_method {
57     my ($self, $attr_name) = @_;
58     my $class_name = $self->associated_class->name;
59     eval qq{sub {
60         \$_[0]->{'$class_name'}->{'$attr_name'} = \$_[1] if scalar(\@_) == 2;
61         \$_[0]->{'$class_name'}->{'$attr_name'};
62     }};
63 }
64
65 sub generate_reader_method {
66     my ($self, $attr_name) = @_; 
67     my $class_name = $self->associated_class->name;
68     eval qq{sub {
69         \$_[0]->{'$class_name'}->{'$attr_name'};
70     }};   
71 }
72
73 sub generate_writer_method {
74     my ($self, $attr_name) = @_; 
75     my $class_name = $self->associated_class->name;    
76     eval qq{sub {
77         \$_[0]->{'$class_name'}->{'$attr_name'} = \$_[1];
78     }};
79 }
80
81 sub generate_predicate_method {
82     my ($self, $attr_name) = @_; 
83     my $class_name = $self->associated_class->name;    
84     eval qq{sub {
85         defined \$_[0]->{'$class_name'}->{'$attr_name'} ? 1 : 0;
86     }};
87 }
88
89 ## &remove_attribute is left as an exercise for the reader :)
90
91 1;
92
93 __END__
94
95 =pod
96
97 =head1 NAME
98
99 ClassEncapsulatedAttributes - A set of example metaclasses with class encapsulated attributes
100
101 =head1 SYNOPSIS
102
103   package Foo;
104   
105   use metaclass 'ClassEncapsulatedAttributes';
106   
107   Foo->meta->add_attribute('foo' => (
108       accessor  => 'Foo_foo',
109       default   => 'init in FOO'
110   ));
111   
112   sub new  {
113       my $class = shift;
114       $class->meta->new_object(@_);
115   }
116   
117   package Bar;
118   our @ISA = ('Foo');
119   
120   # duplicate the attribute name here
121   Bar->meta->add_attribute('foo' => (
122       accessor  => 'Bar_foo',
123       default   => 'init in BAR'            
124   ));      
125   
126   # ... later in other code ...
127   
128   my $bar = Bar->new();
129   prints $bar->Bar_foo(); # init in BAR
130   prints $bar->Foo_foo(); # init in FOO  
131   
132   # and ...
133   
134   my $bar = Bar->new(
135       'Foo' => { 'foo' => 'Foo::foo' },
136       'Bar' => { 'foo' => 'Bar::foo' }        
137   );  
138   
139   prints $bar->Bar_foo(); # Foo::foo
140   prints $bar->Foo_foo(); # Bar::foo  
141   
142 =head1 DESCRIPTION
143
144 This is an example metaclass which encapsulates a class's 
145 attributes on a per-class basis. This means that there is no
146 possibility of name clashes with inherited attributes. This 
147 is similar to how C++ handles its data members. 
148
149 =head1 ACKNOWLEDGEMENTS
150
151 Thanks to Yuval "nothingmuch" Kogman for the idea for this example.
152
153 =head1 AUTHOR
154
155 Stevan Little E<lt>stevan@iinteractive.comE<gt>
156
157 =head1 COPYRIGHT AND LICENSE
158
159 Copyright 2006 by Infinity Interactive, Inc.
160
161 L<http://www.iinteractive.com>
162
163 This library is free software; you can redistribute it and/or modify
164 it under the same terms as Perl itself. 
165
166 =cut