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 '$!package_name' => $options{package_name},
32 '$!name' => $options{name},
33 # specific to this subclass
34 '$!attribute' => $options{attribute},
35 '$!is_inline' => ($options{is_inline} || 0),
36 '$!accessor_type' => $options{accessor_type},
39 # we don't want this creating
40 # a cycle in the code, if not
42 weaken($self->{'$!attribute'});
44 $self->initialize_body;
51 sub associated_attribute { (shift)->{'$!attribute'} }
52 sub accessor_type { (shift)->{'$!accessor_type'} }
59 my $method_name = join "_" => (
63 ($self->is_inline ? 'inline' : ())
66 eval { $self->{'&!body'} = $self->$method_name() };
72 sub generate_accessor_method {
73 my $attr = (shift)->associated_attribute;
75 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
76 $attr->get_value($_[0]);
80 sub generate_reader_method {
81 my $attr = (shift)->associated_attribute;
83 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
84 $attr->get_value($_[0]);
88 sub generate_writer_method {
89 my $attr = (shift)->associated_attribute;
91 $attr->set_value($_[0], $_[1]);
95 sub generate_predicate_method {
96 my $attr = (shift)->associated_attribute;
98 $attr->has_value($_[0])
102 sub generate_clearer_method {
103 my $attr = (shift)->associated_attribute;
105 $attr->clear_value($_[0])
112 sub generate_accessor_method_inline {
113 my $attr = (shift)->associated_attribute;
114 my $attr_name = $attr->name;
115 my $meta_instance = $attr->associated_class->instance_metaclass;
117 my $code = eval 'sub {'
118 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]') . ' if scalar(@_) == 2; '
119 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
121 confess "Could not generate inline accessor because : $@" if $@;
126 sub generate_reader_method_inline {
127 my $attr = (shift)->associated_attribute;
128 my $attr_name = $attr->name;
129 my $meta_instance = $attr->associated_class->instance_metaclass;
131 my $code = eval 'sub {'
132 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
133 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
135 confess "Could not generate inline accessor because : $@" if $@;
140 sub generate_writer_method_inline {
141 my $attr = (shift)->associated_attribute;
142 my $attr_name = $attr->name;
143 my $meta_instance = $attr->associated_class->instance_metaclass;
145 my $code = eval 'sub {'
146 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
148 confess "Could not generate inline accessor because : $@" if $@;
154 sub generate_predicate_method_inline {
155 my $attr = (shift)->associated_attribute;
156 my $attr_name = $attr->name;
157 my $meta_instance = $attr->associated_class->instance_metaclass;
159 my $code = eval 'sub {' .
160 $meta_instance->inline_is_slot_initialized('$_[0]', "'$attr_name'")
162 confess "Could not generate inline predicate because : $@" if $@;
167 sub generate_clearer_method_inline {
168 my $attr = (shift)->associated_attribute;
169 my $attr_name = $attr->name;
170 my $meta_instance = $attr->associated_class->instance_metaclass;
172 my $code = eval 'sub {'
173 . $meta_instance->inline_deinitialize_slot('$_[0]', "'$attr_name'")
175 confess "Could not generate inline clearer because : $@" if $@;
188 Class::MOP::Method::Accessor - Method Meta Object for accessors
192 use Class::MOP::Method::Accessor;
194 my $reader = Class::MOP::Method::Accessor->new(
195 attribute => $attribute,
197 accessor_type => 'reader',
200 $reader->body->($instance); # call the reader method
204 This is a C<Class::MOP::Method> subclass which is used interally
205 by C<Class::MOP::Attribute> to generate accessor code. It can
206 handle generation of readers, writers, predicate and clearer
207 methods, both as closures and as more optimized inline methods.
213 =item B<new (%options)>
215 This creates the method based on the criteria in C<%options>,
222 This must be an instance of C<Class::MOP::Attribute> which this
223 accessor is being generated for. This paramter is B<required>.
225 =item I<accessor_type>
227 This is a string from the following set; reader, writer, accessor,
228 predicate or clearer. This is used to determine which type of
229 method is to be generated.
233 This is a boolean to indicate if the method should be generated
234 as a closure, or as a more optimized inline version.
238 =item B<accessor_type>
240 This returns the accessor type which was passed into C<new>.
244 This returns the boolean which was passed into C<new>.
246 =item B<associated_attribute>
248 This returns the attribute instance which was passed into C<new>.
250 =item B<initialize_body>
252 This will actually generate the method based on the specified
253 criteria passed to the constructor.
257 =head2 Method Generators
259 These methods will generate appropriate code references for
260 the various types of accessors which are supported by
261 C<Class::MOP::Attribute>. The names pretty much explain it all.
265 =item B<generate_accessor_method>
267 =item B<generate_accessor_method_inline>
269 =item B<generate_clearer_method>
271 =item B<generate_clearer_method_inline>
273 =item B<generate_predicate_method>
275 =item B<generate_predicate_method_inline>
277 =item B<generate_reader_method>
279 =item B<generate_reader_method_inline>
281 =item B<generate_writer_method>
283 =item B<generate_writer_method_inline>
289 Stevan Little E<lt>stevan@iinteractive.comE<gt>
291 =head1 COPYRIGHT AND LICENSE
293 Copyright 2006-2008 by Infinity Interactive, Inc.
295 L<http://www.iinteractive.com>
297 This library is free software; you can redistribute it and/or modify
298 it under the same terms as Perl itself.