2 package Class::MOP::Method::Accessor;
8 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '0.01';
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Class::MOP::Method';
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->intialize_body;
49 sub associated_attribute { (shift)->{'$!attribute'} }
50 sub accessor_type { (shift)->{'$!accessor_type'} }
51 sub is_inline { (shift)->{'$!is_inline'} }
58 my $method_name = join "_" => (
62 ($self->is_inline ? 'inline' : ())
65 eval { $self->{'&!body'} = $self->$method_name() };
71 sub generate_accessor_method {
72 my $attr = (shift)->associated_attribute;
74 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
75 $attr->get_value($_[0]);
79 sub generate_reader_method {
80 my $attr = (shift)->associated_attribute;
82 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
83 $attr->get_value($_[0]);
87 sub generate_writer_method {
88 my $attr = (shift)->associated_attribute;
90 $attr->set_value($_[0], $_[1]);
94 sub generate_predicate_method {
95 my $attr = (shift)->associated_attribute;
97 $attr->has_value($_[0])
101 sub generate_clearer_method {
102 my $attr = (shift)->associated_attribute;
104 $attr->clear_value($_[0])
111 sub generate_accessor_method_inline {
112 my $attr = (shift)->associated_attribute;
113 my $attr_name = $attr->name;
114 my $meta_instance = $attr->associated_class->instance_metaclass;
116 my $code = eval 'sub {'
117 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]') . ' if scalar(@_) == 2; '
118 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
120 confess "Could not generate inline accessor because : $@" if $@;
125 sub generate_reader_method_inline {
126 my $attr = (shift)->associated_attribute;
127 my $attr_name = $attr->name;
128 my $meta_instance = $attr->associated_class->instance_metaclass;
130 my $code = eval 'sub {'
131 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
132 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
134 confess "Could not generate inline accessor because : $@" if $@;
139 sub generate_writer_method_inline {
140 my $attr = (shift)->associated_attribute;
141 my $attr_name = $attr->name;
142 my $meta_instance = $attr->associated_class->instance_metaclass;
144 my $code = eval 'sub {'
145 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
147 confess "Could not generate inline accessor because : $@" if $@;
153 sub generate_predicate_method_inline {
154 my $attr = (shift)->associated_attribute;
155 my $attr_name = $attr->name;
156 my $meta_instance = $attr->associated_class->instance_metaclass;
158 my $code = eval 'sub {'
159 . 'defined ' . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'") . ' ? 1 : 0'
161 confess "Could not generate inline predicate because : $@" if $@;
166 sub generate_clearer_method_inline {
167 my $attr = (shift)->associated_attribute;
168 my $attr_name = $attr->name;
169 my $meta_instance = $attr->associated_class->instance_metaclass;
171 my $code = eval 'sub {'
172 . $meta_instance->inline_deinitialize_slot('$_[0]', "'$attr_name'")
174 confess "Could not generate inline clearer because : $@" if $@;
187 Class::MOP::Method::Accessor - Method Meta Object for accessors
191 use Class::MOP::Method::Accessor;
193 my $reader = Class::MOP::Method::Accessor->new(
194 attribute => $attribute,
196 accessor_type => 'reader',
199 $reader->body->($instance); # call the reader method
203 This is a C<Class::MOP::Method> subclass which is used interally
204 by C<Class::MOP::Attribute> to generate accessor code. It can
205 handle generation of readers, writers, predicate and clearer
206 methods, both as closures and as more optimized inline methods.
212 =item B<new (%options)>
214 This creates the method based on the criteria in C<%options>,
221 This must be an instance of C<Class::MOP::Attribute> which this
222 accessor is being generated for. This paramter is B<required>.
224 =item I<accessor_type>
226 This is a string from the following set; reader, writer, accessor,
227 predicate or clearer. This is used to determine which type of
228 method is to be generated.
232 This is a boolean to indicate if the method should be generated
233 as a closure, or as a more optimized inline version.
237 =item B<accessor_type>
239 This returns the accessor type which was passed into C<new>.
243 This returns the boolean which was passed into C<new>.
245 =item B<associated_attribute>
247 This returns the attribute instance which was passed into C<new>.
249 =item B<intialize_body>
251 This will actually generate the method based on the specified
252 criteria passed to the constructor.
256 =head2 Method Generators
258 These methods will generate appropriate code references for
259 the various types of accessors which are supported by
260 C<Class::MOP::Attribute>. The names pretty much explain it all.
264 =item B<generate_accessor_method>
266 =item B<generate_accessor_method_inline>
268 =item B<generate_clearer_method>
270 =item B<generate_clearer_method_inline>
272 =item B<generate_predicate_method>
274 =item B<generate_predicate_method_inline>
276 =item B<generate_reader_method>
278 =item B<generate_reader_method_inline>
280 =item B<generate_writer_method>
282 =item B<generate_writer_method_inline>
288 Stevan Little E<lt>stevan@iinteractive.comE<gt>
290 =head1 COPYRIGHT AND LICENSE
292 Copyright 2006, 2007 by Infinity Interactive, Inc.
294 L<http://www.iinteractive.com>
296 This library is free software; you can redistribute it and/or modify
297 it under the same terms as Perl itself.