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