2 package Class::MOP::Method::Accessor;
8 use Scalar::Util 'blessed', 'weaken';
10 our $VERSION = '0.65';
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";
28 ($options{package_name} && $options{name})
29 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
31 my $self = $class->_new(%options);
33 # we don't want this creating
34 # a cycle in the code, if not
36 weaken($self->{'attribute'});
38 $self->initialize_body;
44 my ( $class, %options ) = @_;
46 $options{is_inline} ||= 0;
48 return bless \%options, $class;
53 sub associated_attribute { (shift)->{'attribute'} }
54 sub accessor_type { (shift)->{'accessor_type'} }
61 my $method_name = join "_" => (
65 ($self->is_inline ? 'inline' : ())
68 eval { $self->{'body'} = $self->$method_name() };
74 sub generate_accessor_method {
75 my $attr = (shift)->associated_attribute;
77 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
78 $attr->get_value($_[0]);
82 sub generate_reader_method {
83 my $attr = (shift)->associated_attribute;
85 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
86 $attr->get_value($_[0]);
90 sub generate_writer_method {
91 my $attr = (shift)->associated_attribute;
93 $attr->set_value($_[0], $_[1]);
97 sub generate_predicate_method {
98 my $attr = (shift)->associated_attribute;
100 $attr->has_value($_[0])
104 sub generate_clearer_method {
105 my $attr = (shift)->associated_attribute;
107 $attr->clear_value($_[0])
114 sub generate_accessor_method_inline {
115 my $attr = (shift)->associated_attribute;
116 my $attr_name = $attr->name;
117 my $meta_instance = $attr->associated_class->instance_metaclass;
119 my $code = eval 'sub {'
120 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]') . ' if scalar(@_) == 2; '
121 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
123 confess "Could not generate inline accessor because : $@" if $@;
128 sub generate_reader_method_inline {
129 my $attr = (shift)->associated_attribute;
130 my $attr_name = $attr->name;
131 my $meta_instance = $attr->associated_class->instance_metaclass;
133 my $code = eval 'sub {'
134 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
135 . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'")
137 confess "Could not generate inline accessor because : $@" if $@;
142 sub generate_writer_method_inline {
143 my $attr = (shift)->associated_attribute;
144 my $attr_name = $attr->name;
145 my $meta_instance = $attr->associated_class->instance_metaclass;
147 my $code = eval 'sub {'
148 . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
150 confess "Could not generate inline accessor because : $@" if $@;
156 sub generate_predicate_method_inline {
157 my $attr = (shift)->associated_attribute;
158 my $attr_name = $attr->name;
159 my $meta_instance = $attr->associated_class->instance_metaclass;
161 my $code = eval 'sub {' .
162 $meta_instance->inline_is_slot_initialized('$_[0]', "'$attr_name'")
164 confess "Could not generate inline predicate because : $@" if $@;
169 sub generate_clearer_method_inline {
170 my $attr = (shift)->associated_attribute;
171 my $attr_name = $attr->name;
172 my $meta_instance = $attr->associated_class->instance_metaclass;
174 my $code = eval 'sub {'
175 . $meta_instance->inline_deinitialize_slot('$_[0]', "'$attr_name'")
177 confess "Could not generate inline clearer because : $@" if $@;
190 Class::MOP::Method::Accessor - Method Meta Object for accessors
194 use Class::MOP::Method::Accessor;
196 my $reader = Class::MOP::Method::Accessor->new(
197 attribute => $attribute,
199 accessor_type => 'reader',
202 $reader->body->($instance); # call the reader method
206 This is a C<Class::MOP::Method> subclass which is used interally
207 by C<Class::MOP::Attribute> to generate accessor code. It can
208 handle generation of readers, writers, predicate and clearer
209 methods, both as closures and as more optimized inline methods.
215 =item B<new (%options)>
217 This creates the method based on the criteria in C<%options>,
224 This must be an instance of C<Class::MOP::Attribute> which this
225 accessor is being generated for. This paramter is B<required>.
227 =item I<accessor_type>
229 This is a string from the following set; reader, writer, accessor,
230 predicate or clearer. This is used to determine which type of
231 method is to be generated.
235 This is a boolean to indicate if the method should be generated
236 as a closure, or as a more optimized inline version.
240 =item B<accessor_type>
242 This returns the accessor type which was passed into C<new>.
246 This returns the boolean which was passed into C<new>.
248 =item B<associated_attribute>
250 This returns the attribute instance which was passed into C<new>.
252 =item B<initialize_body>
254 This will actually generate the method based on the specified
255 criteria passed to the constructor.
259 =head2 Method Generators
261 These methods will generate appropriate code references for
262 the various types of accessors which are supported by
263 C<Class::MOP::Attribute>. The names pretty much explain it all.
267 =item B<generate_accessor_method>
269 =item B<generate_accessor_method_inline>
271 =item B<generate_clearer_method>
273 =item B<generate_clearer_method_inline>
275 =item B<generate_predicate_method>
277 =item B<generate_predicate_method_inline>
279 =item B<generate_reader_method>
281 =item B<generate_reader_method_inline>
283 =item B<generate_writer_method>
285 =item B<generate_writer_method_inline>
291 Stevan Little E<lt>stevan@iinteractive.comE<gt>
293 =head1 COPYRIGHT AND LICENSE
295 Copyright 2006-2008 by Infinity Interactive, Inc.
297 L<http://www.iinteractive.com>
299 This library is free software; you can redistribute it and/or modify
300 it under the same terms as Perl itself.