2 package Class::MOP::Method::Accessor;
8 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '0.78';
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 eval { $self->{'body'} = $self->$method_name() };
76 sub generate_accessor_method {
77 my $attr = (shift)->associated_attribute;
79 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
80 $attr->get_value($_[0]);
84 sub generate_reader_method {
85 my $attr = (shift)->associated_attribute;
87 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
88 $attr->get_value($_[0]);
92 sub generate_writer_method {
93 my $attr = (shift)->associated_attribute;
95 $attr->set_value($_[0], $_[1]);
99 sub generate_predicate_method {
100 my $attr = (shift)->associated_attribute;
102 $attr->has_value($_[0])
106 sub generate_clearer_method {
107 my $attr = (shift)->associated_attribute;
109 $attr->clear_value($_[0])
116 sub generate_accessor_method_inline {
118 my $attr = $self->associated_attribute;
119 my $attr_name = $attr->name;
120 my $meta_instance = $attr->associated_class->instance_metaclass;
122 my $code = $self->_eval_closure(
125 . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]')
126 . ' if scalar(@_) == 2; '
127 . $meta_instance->inline_get_slot_value('$_[0]', $attr_name)
130 confess "Could not generate inline accessor because : $@" if $@;
135 sub generate_reader_method_inline {
137 my $attr = $self->associated_attribute;
138 my $attr_name = $attr->name;
139 my $meta_instance = $attr->associated_class->instance_metaclass;
141 my $code = $self->_eval_closure(
144 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
145 . $meta_instance->inline_get_slot_value('$_[0]', $attr_name)
148 confess "Could not generate inline reader because : $@" if $@;
153 sub generate_writer_method_inline {
155 my $attr = $self->associated_attribute;
156 my $attr_name = $attr->name;
157 my $meta_instance = $attr->associated_class->instance_metaclass;
159 my $code = $self->_eval_closure(
162 . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]')
165 confess "Could not generate inline writer because : $@" if $@;
171 sub generate_predicate_method_inline {
173 my $attr = $self->associated_attribute;
174 my $attr_name = $attr->name;
175 my $meta_instance = $attr->associated_class->instance_metaclass;
177 my $code = $self->_eval_closure(
180 . $meta_instance->inline_is_slot_initialized('$_[0]', $attr_name)
183 confess "Could not generate inline predicate because : $@" if $@;
188 sub generate_clearer_method_inline {
190 my $attr = $self->associated_attribute;
191 my $attr_name = $attr->name;
192 my $meta_instance = $attr->associated_class->instance_metaclass;
194 my $code = $self->_eval_closure(
197 . $meta_instance->inline_deinitialize_slot('$_[0]', $attr_name)
200 confess "Could not generate inline clearer because : $@" if $@;
213 Class::MOP::Method::Accessor - Method Meta Object for accessors
217 use Class::MOP::Method::Accessor;
219 my $reader = Class::MOP::Method::Accessor->new(
220 attribute => $attribute,
222 accessor_type => 'reader',
225 $reader->body->execute($instance); # call the reader method
229 This is a C<Class::MOP::Method> subclass which is used interally
230 by C<Class::MOP::Attribute> to generate accessor code. It can
231 handle generation of readers, writers, predicate and clearer
232 methods, both as closures and as more optimized inline methods.
238 =item B<new (%options)>
240 This creates the method based on the criteria in C<%options>,
247 This must be an instance of C<Class::MOP::Attribute> which this
248 accessor is being generated for. This paramter is B<required>.
250 =item I<accessor_type>
252 This is a string from the following set; reader, writer, accessor,
253 predicate or clearer. This is used to determine which type of
254 method is to be generated.
258 This is a boolean to indicate if the method should be generated
259 as a closure, or as a more optimized inline version.
263 =item B<accessor_type>
265 This returns the accessor type which was passed into C<new>.
269 This returns the boolean which was passed into C<new>.
271 =item B<associated_attribute>
273 This returns the attribute instance which was passed into C<new>.
275 =item B<initialize_body>
277 This will actually generate the method based on the specified
278 criteria passed to the constructor.
282 =head2 Method Generators
284 These methods will generate appropriate code references for
285 the various types of accessors which are supported by
286 C<Class::MOP::Attribute>. The names pretty much explain it all.
290 =item B<generate_accessor_method>
292 =item B<generate_accessor_method_inline>
294 =item B<generate_clearer_method>
296 =item B<generate_clearer_method_inline>
298 =item B<generate_predicate_method>
300 =item B<generate_predicate_method_inline>
302 =item B<generate_reader_method>
304 =item B<generate_reader_method_inline>
306 =item B<generate_writer_method>
308 =item B<generate_writer_method_inline>
314 Stevan Little E<lt>stevan@iinteractive.comE<gt>
316 =head1 COPYRIGHT AND LICENSE
318 Copyright 2006-2009 by Infinity Interactive, Inc.
320 L<http://www.iinteractive.com>
322 This library is free software; you can redistribute it and/or modify
323 it under the same terms as Perl itself.