2 package Class::MOP::Method::Accessor;
8 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '0.77';
11 $VERSION = eval $VERSION;
12 our $AUTHORITY = 'cpan:STEVAN';
14 use base 'Class::MOP::Method::Generated';
20 (exists $options{attribute})
21 || confess "You must supply an attribute to construct with";
23 (exists $options{accessor_type})
24 || confess "You must supply an accessor_type to construct with";
26 (blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute'))
27 || confess "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
29 ($options{package_name} && $options{name})
30 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
32 my $self = $class->_new(\%options);
34 # we don't want this creating
35 # a cycle in the code, if not
37 weaken($self->{'attribute'});
39 $self->initialize_body;
46 my $options = @_ == 1 ? $_[0] : {@_};
48 $options->{is_inline} ||= 0;
50 return bless $options, $class;
55 sub associated_attribute { (shift)->{'attribute'} }
56 sub accessor_type { (shift)->{'accessor_type'} }
63 my $method_name = join "_" => (
67 ($self->is_inline ? 'inline' : ())
70 $self->{'body'} = $self->$method_name();
75 sub generate_accessor_method {
76 my $attr = (shift)->associated_attribute;
78 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
79 $attr->get_value($_[0]);
83 sub generate_reader_method {
84 my $attr = (shift)->associated_attribute;
86 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
87 $attr->get_value($_[0]);
91 sub generate_writer_method {
92 my $attr = (shift)->associated_attribute;
94 $attr->set_value($_[0], $_[1]);
98 sub generate_predicate_method {
99 my $attr = (shift)->associated_attribute;
101 $attr->has_value($_[0])
105 sub generate_clearer_method {
106 my $attr = (shift)->associated_attribute;
108 $attr->clear_value($_[0])
115 sub generate_accessor_method_inline {
117 my $attr = $self->associated_attribute;
118 my $attr_name = $attr->name;
119 my $meta_instance = $attr->associated_class->instance_metaclass;
121 return $self->_eval_closure(
124 . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]')
125 . ' if scalar(@_) == 2; '
126 . $meta_instance->inline_get_slot_value('$_[0]', $attr_name)
131 sub generate_reader_method_inline {
133 my $attr = $self->associated_attribute;
134 my $attr_name = $attr->name;
135 my $meta_instance = $attr->associated_class->instance_metaclass;
137 return $self->_eval_closure(
140 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
141 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
146 sub generate_writer_method_inline {
148 my $attr = $self->associated_attribute;
149 my $attr_name = $attr->name;
150 my $meta_instance = $attr->associated_class->instance_metaclass;
152 return $self->_eval_closure(
155 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
161 sub generate_predicate_method_inline {
163 my $attr = $self->associated_attribute;
164 my $attr_name = $attr->name;
165 my $meta_instance = $attr->associated_class->instance_metaclass;
167 return $self->_eval_closure(
170 . $meta_instance->inline_is_slot_initialized('$_[0]', "'$attr_name'")
175 sub generate_clearer_method_inline {
177 my $attr = $self->associated_attribute;
178 my $attr_name = $attr->name;
179 my $meta_instance = $attr->associated_class->instance_metaclass;
181 return $self->_eval_closure(
184 . $meta_instance->inline_deinitialize_slot('$_[0]', "'$attr_name'")
197 Class::MOP::Method::Accessor - Method Meta Object for accessors
201 use Class::MOP::Method::Accessor;
203 my $reader = Class::MOP::Method::Accessor->new(
204 attribute => $attribute,
206 accessor_type => 'reader',
209 $reader->body->execute($instance); # call the reader method
213 This is a C<Class::MOP::Method> subclass which is used interally
214 by C<Class::MOP::Attribute> to generate accessor code. It can
215 handle generation of readers, writers, predicate and clearer
216 methods, both as closures and as more optimized inline methods.
222 =item B<new (%options)>
224 This creates the method based on the criteria in C<%options>,
231 This must be an instance of C<Class::MOP::Attribute> which this
232 accessor is being generated for. This paramter is B<required>.
234 =item I<accessor_type>
236 This is a string from the following set; reader, writer, accessor,
237 predicate or clearer. This is used to determine which type of
238 method is to be generated.
242 This is a boolean to indicate if the method should be generated
243 as a closure, or as a more optimized inline version.
247 =item B<accessor_type>
249 This returns the accessor type which was passed into C<new>.
253 This returns the boolean which was passed into C<new>.
255 =item B<associated_attribute>
257 This returns the attribute instance which was passed into C<new>.
259 =item B<initialize_body>
261 This will actually generate the method based on the specified
262 criteria passed to the constructor.
266 =head2 Method Generators
268 These methods will generate appropriate code references for
269 the various types of accessors which are supported by
270 C<Class::MOP::Attribute>. The names pretty much explain it all.
274 =item B<generate_accessor_method>
276 =item B<generate_accessor_method_inline>
278 =item B<generate_clearer_method>
280 =item B<generate_clearer_method_inline>
282 =item B<generate_predicate_method>
284 =item B<generate_predicate_method_inline>
286 =item B<generate_reader_method>
288 =item B<generate_reader_method_inline>
290 =item B<generate_writer_method>
292 =item B<generate_writer_method_inline>
298 Stevan Little E<lt>stevan@iinteractive.comE<gt>
300 =head1 COPYRIGHT AND LICENSE
302 Copyright 2006-2008 by Infinity Interactive, Inc.
304 L<http://www.iinteractive.com>
306 This library is free software; you can redistribute it and/or modify
307 it under the same terms as Perl itself.