2 package Class::MOP::Method::Accessor;
8 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '0.02';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Class::MOP::Method::Generated';
19 (exists $options{attribute})
20 || confess "You must supply an attribute to construct with";
22 (exists $options{accessor_type})
23 || confess "You must supply an accessor_type to construct with";
25 (blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute'))
26 || confess "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
31 # specific to this subclass
32 '$!attribute' => $options{attribute},
33 '$!is_inline' => ($options{is_inline} || 0),
34 '$!accessor_type' => $options{accessor_type},
37 # we don't want this creating
38 # a cycle in the code, if not
40 weaken($self->{'$!attribute'});
42 $self->initialize_body;
49 sub associated_attribute { (shift)->{'$!attribute'} }
50 sub accessor_type { (shift)->{'$!accessor_type'} }
57 my $method_name = join "_" => (
61 ($self->is_inline ? 'inline' : ())
64 eval { $self->{'&!body'} = $self->$method_name() };
70 sub generate_accessor_method {
71 my $attr = (shift)->associated_attribute;
73 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
74 $attr->get_value($_[0]);
78 sub generate_reader_method {
79 my $attr = (shift)->associated_attribute;
81 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
82 $attr->get_value($_[0]);
86 sub generate_writer_method {
87 my $attr = (shift)->associated_attribute;
89 $attr->set_value($_[0], $_[1]);
93 sub generate_predicate_method {
94 my $attr = (shift)->associated_attribute;
96 $attr->has_value($_[0])
100 sub generate_clearer_method {
101 my $attr = (shift)->associated_attribute;
103 $attr->clear_value($_[0])
110 sub generate_accessor_method_inline {
111 my $attr = (shift)->associated_attribute;
112 my $attr_name = $attr->name;
113 my $meta_instance = $attr->associated_class->instance_metaclass;
115 my $code = eval 'sub {'
116 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]') . ' if scalar(@_) == 2; '
117 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
119 confess "Could not generate inline accessor because : $@" if $@;
124 sub generate_reader_method_inline {
125 my $attr = (shift)->associated_attribute;
126 my $attr_name = $attr->name;
127 my $meta_instance = $attr->associated_class->instance_metaclass;
129 my $code = eval 'sub {'
130 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
131 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
133 confess "Could not generate inline accessor because : $@" if $@;
138 sub generate_writer_method_inline {
139 my $attr = (shift)->associated_attribute;
140 my $attr_name = $attr->name;
141 my $meta_instance = $attr->associated_class->instance_metaclass;
143 my $code = eval 'sub {'
144 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
146 confess "Could not generate inline accessor because : $@" if $@;
152 sub generate_predicate_method_inline {
153 my $attr = (shift)->associated_attribute;
154 my $attr_name = $attr->name;
155 my $meta_instance = $attr->associated_class->instance_metaclass;
157 my $code = eval 'sub {' .
158 $meta_instance->inline_is_slot_initialized('$_[0]', "'$attr_name'")
160 confess "Could not generate inline predicate because : $@" if $@;
165 sub generate_clearer_method_inline {
166 my $attr = (shift)->associated_attribute;
167 my $attr_name = $attr->name;
168 my $meta_instance = $attr->associated_class->instance_metaclass;
170 my $code = eval 'sub {'
171 . $meta_instance->inline_deinitialize_slot('$_[0]', "'$attr_name'")
173 confess "Could not generate inline clearer because : $@" if $@;
186 Class::MOP::Method::Accessor - Method Meta Object for accessors
190 use Class::MOP::Method::Accessor;
192 my $reader = Class::MOP::Method::Accessor->new(
193 attribute => $attribute,
195 accessor_type => 'reader',
198 $reader->body->($instance); # call the reader method
202 This is a C<Class::MOP::Method> subclass which is used interally
203 by C<Class::MOP::Attribute> to generate accessor code. It can
204 handle generation of readers, writers, predicate and clearer
205 methods, both as closures and as more optimized inline methods.
211 =item B<new (%options)>
213 This creates the method based on the criteria in C<%options>,
220 This must be an instance of C<Class::MOP::Attribute> which this
221 accessor is being generated for. This paramter is B<required>.
223 =item I<accessor_type>
225 This is a string from the following set; reader, writer, accessor,
226 predicate or clearer. This is used to determine which type of
227 method is to be generated.
231 This is a boolean to indicate if the method should be generated
232 as a closure, or as a more optimized inline version.
236 =item B<accessor_type>
238 This returns the accessor type which was passed into C<new>.
242 This returns the boolean which was passed into C<new>.
244 =item B<associated_attribute>
246 This returns the attribute instance which was passed into C<new>.
248 =item B<initialize_body>
250 This will actually generate the method based on the specified
251 criteria passed to the constructor.
255 =head2 Method Generators
257 These methods will generate appropriate code references for
258 the various types of accessors which are supported by
259 C<Class::MOP::Attribute>. The names pretty much explain it all.
263 =item B<generate_accessor_method>
265 =item B<generate_accessor_method_inline>
267 =item B<generate_clearer_method>
269 =item B<generate_clearer_method_inline>
271 =item B<generate_predicate_method>
273 =item B<generate_predicate_method_inline>
275 =item B<generate_reader_method>
277 =item B<generate_reader_method_inline>
279 =item B<generate_writer_method>
281 =item B<generate_writer_method_inline>
287 Stevan Little E<lt>stevan@iinteractive.comE<gt>
289 =head1 COPYRIGHT AND LICENSE
291 Copyright 2006-2008 by Infinity Interactive, Inc.
293 L<http://www.iinteractive.com>
295 This library is free software; you can redistribute it and/or modify
296 it under the same terms as Perl itself.