getting close to a 0.07 release
[gitmo/Class-MOP.git] / examples / ClassEncapsulatedAttributes.pod
CommitLineData
d6fbcd05 1
2package # hide the package from PAUSE
3 ClassEncapsulatedAttributes;
4
5use strict;
6use warnings;
7
5659d76e 8our $VERSION = '0.03';
d6fbcd05 9
10use base 'Class::MOP::Class';
11
351bd7d4 12sub initialize {
13 (shift)->SUPER::initialize(@_,
14 # use the custom attribute metaclass here
15 ':attribute_metaclass' => 'ClassEncapsulatedAttributes::Attribute'
16 );
17}
18
d6fbcd05 19sub construct_instance {
20 my ($class, %params) = @_;
d6fbcd05 21 my $instance = {};
22 foreach my $current_class ($class->class_precedence_list()) {
23 $instance->{$current_class} = {}
24 unless exists $instance->{$current_class};
aa448b16 25 my $meta = $current_class->meta;
d6fbcd05 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
651955fb 30 my $init_arg = $attr->init_arg();
d6fbcd05 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 }
d6fbcd05 43 return $instance;
44}
45
46package # hide the package from PAUSE
47 ClassEncapsulatedAttributes::Attribute;
48
49use strict;
50use warnings;
51
99e5b7e8 52our $VERSION = '0.02';
d6fbcd05 53
54use base 'Class::MOP::Attribute';
55
56sub 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
65sub 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
73sub 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
81sub 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
911;
92
93__END__
94
95=pod
96
97=head1 NAME
98
99ClassEncapsulatedAttributes - A set of example metaclasses with class encapsulated attributes
100
101=head1 SYNOPSIS
102
103 package Foo;
104
677eb158 105 use metaclass 'ClassEncapsulatedAttributes';
d6fbcd05 106
2e41896e 107 Foo->meta->add_attribute('foo' => (
108 accessor => 'Foo_foo',
109 default => 'init in FOO'
110 ));
d6fbcd05 111
112 sub new {
113 my $class = shift;
5659d76e 114 $class->meta->new_object(@_);
d6fbcd05 115 }
116
117 package Bar;
118 our @ISA = ('Foo');
119
120 # duplicate the attribute name here
2e41896e 121 Bar->meta->add_attribute('foo' => (
122 accessor => 'Bar_foo',
123 default => 'init in BAR'
124 ));
d6fbcd05 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
d7c2cbe3 144This is an example metaclass which encapsulates a class's
145attributes on a per-class basis. This means that there is no
146possibility of name clashes with inherited attributes. This
147is similar to how C++ handles its data members.
148
149=head1 ACKNOWLEDGEMENTS
150
151Thanks to Yuval "nothingmuch" Kogman for the idea for this example.
152
d6fbcd05 153=head1 AUTHOR
154
155Stevan Little E<lt>stevan@iinteractive.comE<gt>
156
157=head1 COPYRIGHT AND LICENSE
158
159Copyright 2006 by Infinity Interactive, Inc.
160
161L<http://www.iinteractive.com>
162
163This library is free software; you can redistribute it and/or modify
164it under the same terms as Perl itself.
165
166=cut