more
[gitmo/Class-MOP.git] / examples / ClassEncapsulatedAttributes.pod
CommitLineData
d6fbcd05 1
2package # hide the package from PAUSE
3 ClassEncapsulatedAttributes;
4
5use strict;
6use warnings;
7
a977cf65 8our $VERSION = '0.04';
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)
a977cf65 38 if (!defined $val && $attr->has_default) {
39 $val = $attr->default($instance);
40 }
d6fbcd05 41 # now add this to the instance structure
42 $instance->{$current_class}->{$attr_name} = $val;
43 }
44 }
d6fbcd05 45 return $instance;
46}
47
48package # hide the package from PAUSE
49 ClassEncapsulatedAttributes::Attribute;
50
51use strict;
52use warnings;
53
99e5b7e8 54our $VERSION = '0.02';
d6fbcd05 55
56use base 'Class::MOP::Attribute';
57
58sub generate_accessor_method {
59 my ($self, $attr_name) = @_;
60 my $class_name = $self->associated_class->name;
61 eval qq{sub {
62 \$_[0]->{'$class_name'}->{'$attr_name'} = \$_[1] if scalar(\@_) == 2;
63 \$_[0]->{'$class_name'}->{'$attr_name'};
64 }};
65}
66
67sub generate_reader_method {
68 my ($self, $attr_name) = @_;
69 my $class_name = $self->associated_class->name;
70 eval qq{sub {
b9dfbf78 71 Carp::confess "Cannot assign a value to a read-only accessor" if \@_ > 1;
d6fbcd05 72 \$_[0]->{'$class_name'}->{'$attr_name'};
73 }};
74}
75
76sub generate_writer_method {
77 my ($self, $attr_name) = @_;
78 my $class_name = $self->associated_class->name;
79 eval qq{sub {
80 \$_[0]->{'$class_name'}->{'$attr_name'} = \$_[1];
81 }};
82}
83
84sub generate_predicate_method {
85 my ($self, $attr_name) = @_;
86 my $class_name = $self->associated_class->name;
87 eval qq{sub {
88 defined \$_[0]->{'$class_name'}->{'$attr_name'} ? 1 : 0;
89 }};
90}
91
92## &remove_attribute is left as an exercise for the reader :)
93
941;
95
96__END__
97
98=pod
99
100=head1 NAME
101
102ClassEncapsulatedAttributes - A set of example metaclasses with class encapsulated attributes
103
104=head1 SYNOPSIS
105
106 package Foo;
107
677eb158 108 use metaclass 'ClassEncapsulatedAttributes';
d6fbcd05 109
2e41896e 110 Foo->meta->add_attribute('foo' => (
111 accessor => 'Foo_foo',
112 default => 'init in FOO'
113 ));
d6fbcd05 114
115 sub new {
116 my $class = shift;
5659d76e 117 $class->meta->new_object(@_);
d6fbcd05 118 }
119
120 package Bar;
121 our @ISA = ('Foo');
122
123 # duplicate the attribute name here
2e41896e 124 Bar->meta->add_attribute('foo' => (
125 accessor => 'Bar_foo',
126 default => 'init in BAR'
127 ));
d6fbcd05 128
129 # ... later in other code ...
130
131 my $bar = Bar->new();
132 prints $bar->Bar_foo(); # init in BAR
133 prints $bar->Foo_foo(); # init in FOO
134
135 # and ...
136
137 my $bar = Bar->new(
138 'Foo' => { 'foo' => 'Foo::foo' },
139 'Bar' => { 'foo' => 'Bar::foo' }
140 );
141
142 prints $bar->Bar_foo(); # Foo::foo
143 prints $bar->Foo_foo(); # Bar::foo
144
145=head1 DESCRIPTION
146
d7c2cbe3 147This is an example metaclass which encapsulates a class's
148attributes on a per-class basis. This means that there is no
149possibility of name clashes with inherited attributes. This
150is similar to how C++ handles its data members.
151
152=head1 ACKNOWLEDGEMENTS
153
154Thanks to Yuval "nothingmuch" Kogman for the idea for this example.
155
d6fbcd05 156=head1 AUTHOR
157
158Stevan Little E<lt>stevan@iinteractive.comE<gt>
159
160=head1 COPYRIGHT AND LICENSE
161
162Copyright 2006 by Infinity Interactive, Inc.
163
164L<http://www.iinteractive.com>
165
166This library is free software; you can redistribute it and/or modify
167it under the same terms as Perl itself.
168
169=cut