various tweaks, and refactor _instantiate_module not to use eval STRING
[gitmo/Class-MOP.git] / lib / Class / MOP / Method / Accessor.pm
CommitLineData
ba38bf08 1
2package Class::MOP::Method::Accessor;
3
4use strict;
5use warnings;
6
7use Carp 'confess';
8use Scalar::Util 'blessed', 'weaken';
9
074ec38f 10our $VERSION = '0.89';
d519662a 11$VERSION = eval $VERSION;
ba38bf08 12our $AUTHORITY = 'cpan:STEVAN';
13
565f0cbb 14use base 'Class::MOP::Method::Generated';
ba38bf08 15
ba38bf08 16sub new {
17 my $class = shift;
18 my %options = @_;
8d2d4c67 19
ba38bf08 20 (exists $options{attribute})
21 || confess "You must supply an attribute to construct with";
8d2d4c67 22
ba38bf08 23 (exists $options{accessor_type})
8d2d4c67 24 || confess "You must supply an accessor_type to construct with";
25
ba38bf08 26 (blessed($options{attribute}) && $options{attribute}->isa('Class::MOP::Attribute'))
8d2d4c67 27 || confess "You must supply an attribute which is a 'Class::MOP::Attribute' instance";
28
b38f3848 29 ($options{package_name} && $options{name})
32202ce2 30 || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT";
b38f3848 31
0bfc85b8 32 my $self = $class->_new(\%options);
8d2d4c67 33
34 # we don't want this creating
35 # a cycle in the code, if not
ba38bf08 36 # needed
8683db0e 37 weaken($self->{'attribute'});
8d2d4c67 38
e9497117 39 $self->_initialize_body;
8d2d4c67 40
ba38bf08 41 return $self;
42}
43
a9e38dc7 44sub _new {
0bfc85b8 45 my $class = shift;
a9e38dc7 46
ec9e38e5 47 return Class::MOP::Class->initialize($class)->new_object(@_)
48 if $class ne __PACKAGE__;
a9e38dc7 49
ec9e38e5 50 my $params = @_ == 1 ? $_[0] : {@_};
51
52 return bless {
53 # inherited from Class::MOP::Method
54 body => $params->{body},
55 associated_metaclass => $params->{associated_metaclass},
56 package_name => $params->{package_name},
57 name => $params->{name},
58 original_method => $params->{original_method},
59
60 # inherit from Class::MOP::Generated
61 is_inline => $params->{is_inline} || 0,
62 definition_context => $params->{definition_context},
63
64 # defined in this class
65 attribute => $params->{attribute},
66 accessor_type => $params->{accessor_type},
67 } => $class;
a9e38dc7 68}
69
ba38bf08 70## accessors
71
8683db0e 72sub associated_attribute { (shift)->{'attribute'} }
73sub accessor_type { (shift)->{'accessor_type'} }
ba38bf08 74
8d2d4c67 75## factory
ba38bf08 76
565f0cbb 77sub initialize_body {
c7e28c19 78 Carp::cluck('The initialize_body method has been made private.'
79 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 80 shift->_initialize_body;
e9497117 81}
82
83sub _initialize_body {
ba38bf08 84 my $self = shift;
8d2d4c67 85
ba38bf08 86 my $method_name = join "_" => (
afc92ac6 87 '_generate',
8d2d4c67 88 $self->accessor_type,
ba38bf08 89 'method',
d90b42a6 90 ($self->is_inline ? 'inline' : ())
ba38bf08 91 );
8d2d4c67 92
1be5f78f 93 $self->{'body'} = $self->$method_name();
ba38bf08 94}
95
96## generators
97
98sub generate_accessor_method {
c7e28c19 99 Carp::cluck('The generate_accessor_method method has been made private.'
100 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 101 shift->_generate_accessor_method;
afc92ac6 102}
103
104sub _generate_accessor_method {
8d2d4c67 105 my $attr = (shift)->associated_attribute;
ba38bf08 106 return sub {
107 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
108 $attr->get_value($_[0]);
109 };
110}
111
112sub generate_reader_method {
c7e28c19 113 Carp::cluck('The generate_reader_method method has been made private.'
114 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 115 shift->_generate_reader_method;
afc92ac6 116}
117
118sub _generate_reader_method {
8d2d4c67 119 my $attr = (shift)->associated_attribute;
120 return sub {
ba38bf08 121 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
122 $attr->get_value($_[0]);
8d2d4c67 123 };
ba38bf08 124}
125
126sub generate_writer_method {
c7e28c19 127 Carp::cluck('The generate_writer_method method has been made private.'
128 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 129 shift->_generate_writer_method;
afc92ac6 130}
131
132sub _generate_writer_method {
8d2d4c67 133 my $attr = (shift)->associated_attribute;
ba38bf08 134 return sub {
135 $attr->set_value($_[0], $_[1]);
136 };
137}
138
139sub generate_predicate_method {
c7e28c19 140 Carp::cluck('The generate_predicate_method method has been made private.'
141 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 142 shift->_generate_predicate_method;
afc92ac6 143}
144
145sub _generate_predicate_method {
8d2d4c67 146 my $attr = (shift)->associated_attribute;
147 return sub {
3545c727 148 $attr->has_value($_[0])
ba38bf08 149 };
150}
151
152sub generate_clearer_method {
c7e28c19 153 Carp::cluck('The generate_clearer_method method has been made private.'
154 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 155 shift->_generate_clearer_method;
afc92ac6 156}
157
158sub _generate_clearer_method {
8d2d4c67 159 my $attr = (shift)->associated_attribute;
160 return sub {
3545c727 161 $attr->clear_value($_[0])
ba38bf08 162 };
163}
164
165## Inline methods
166
ba38bf08 167sub generate_accessor_method_inline {
c7e28c19 168 Carp::cluck('The generate_accessor_method_inline method has been made private.'
169 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 170 shift->_generate_accessor_method_inline;
afc92ac6 171}
172
173sub _generate_accessor_method_inline {
7f8de9b4 174 my $self = shift;
175 my $attr = $self->associated_attribute;
ba38bf08 176 my $attr_name = $attr->name;
177 my $meta_instance = $attr->associated_class->instance_metaclass;
178
e24b19fb 179 my ( $code, $e ) = $self->_eval_closure(
0c6f3280 180 {},
7f8de9b4 181 'sub {'
a71a4ccb 182 . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]')
7f8de9b4 183 . ' if scalar(@_) == 2; '
a71a4ccb 184 . $meta_instance->inline_get_slot_value('$_[0]', $attr_name)
7f8de9b4 185 . '}'
186 );
e24b19fb 187 confess "Could not generate inline accessor because : $e" if $e;
a6eef5a3 188
189 return $code;
ba38bf08 190}
191
192sub generate_reader_method_inline {
c7e28c19 193 Carp::cluck('The generate_reader_method_inline method has been made private.'
194 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 195 shift->_generate_reader_method_inline;
afc92ac6 196}
197
198sub _generate_reader_method_inline {
7f8de9b4 199 my $self = shift;
200 my $attr = $self->associated_attribute;
ba38bf08 201 my $attr_name = $attr->name;
202 my $meta_instance = $attr->associated_class->instance_metaclass;
203
e24b19fb 204 my ( $code, $e ) = $self->_eval_closure(
0c6f3280 205 {},
7f8de9b4 206 'sub {'
ba38bf08 207 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
e9a19694 208 . $meta_instance->inline_get_slot_value('$_[0]', $attr_name)
7f8de9b4 209 . '}'
210 );
e24b19fb 211 confess "Could not generate inline reader because : $e" if $e;
a6eef5a3 212
213 return $code;
ba38bf08 214}
215
216sub generate_writer_method_inline {
c7e28c19 217 Carp::cluck('The generate_writer_method_inline method has been made private.'
218 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 219 shift->_generate_writer_method_inline;
afc92ac6 220}
221
222sub _generate_writer_method_inline {
7f8de9b4 223 my $self = shift;
224 my $attr = $self->associated_attribute;
ba38bf08 225 my $attr_name = $attr->name;
226 my $meta_instance = $attr->associated_class->instance_metaclass;
227
e24b19fb 228 my ( $code, $e ) = $self->_eval_closure(
0c6f3280 229 {},
7f8de9b4 230 'sub {'
e9a19694 231 . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]')
7f8de9b4 232 . '}'
233 );
e24b19fb 234 confess "Could not generate inline writer because : $e" if $e;
a6eef5a3 235
236 return $code;
ba38bf08 237}
238
ba38bf08 239sub generate_predicate_method_inline {
c7e28c19 240 Carp::cluck('The generate_predicate_method_inline method has been made private.'
241 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 242 shift->_generate_predicate_method_inline;
afc92ac6 243}
244
245sub _generate_predicate_method_inline {
7f8de9b4 246 my $self = shift;
247 my $attr = $self->associated_attribute;
ba38bf08 248 my $attr_name = $attr->name;
249 my $meta_instance = $attr->associated_class->instance_metaclass;
250
e24b19fb 251 my ( $code, $e ) = $self->_eval_closure(
0c6f3280 252 {},
7f8de9b4 253 'sub {'
e9a19694 254 . $meta_instance->inline_is_slot_initialized('$_[0]', $attr_name)
7f8de9b4 255 . '}'
256 );
e24b19fb 257 confess "Could not generate inline predicate because : $e" if $e;
a6eef5a3 258
259 return $code;
ba38bf08 260}
261
262sub generate_clearer_method_inline {
c7e28c19 263 Carp::cluck('The generate_clearer_method_inline method has been made private.'
264 . " The public version is deprecated and will be removed in a future release.\n");
d5c7f638 265 shift->_generate_clearer_method_inline;
afc92ac6 266}
267
268sub _generate_clearer_method_inline {
7f8de9b4 269 my $self = shift;
270 my $attr = $self->associated_attribute;
ba38bf08 271 my $attr_name = $attr->name;
272 my $meta_instance = $attr->associated_class->instance_metaclass;
273
e24b19fb 274 my ( $code, $e ) = $self->_eval_closure(
0c6f3280 275 {},
7f8de9b4 276 'sub {'
e9a19694 277 . $meta_instance->inline_deinitialize_slot('$_[0]', $attr_name)
7f8de9b4 278 . '}'
279 );
e24b19fb 280 confess "Could not generate inline clearer because : $e" if $e;
a6eef5a3 281
282 return $code;
ba38bf08 283}
284
2851;
286
287__END__
288
289=pod
290
8d2d4c67 291=head1 NAME
ba38bf08 292
293Class::MOP::Method::Accessor - Method Meta Object for accessors
294
295=head1 SYNOPSIS
296
96e38ba6 297 use Class::MOP::Method::Accessor;
298
299 my $reader = Class::MOP::Method::Accessor->new(
300 attribute => $attribute,
301 is_inline => 1,
302 accessor_type => 'reader',
303 );
8d2d4c67 304
b7045e66 305 $reader->body->execute($instance); # call the reader method
ba38bf08 306
307=head1 DESCRIPTION
308
1385ad9d 309This is a subclass of <Class::MOP::Method> which is used by
310C<Class::MOP::Attribute> to generate accessor code. It handles
311generation of readers, writers, predicates and clearers. For each type
312of method, it can either create a subroutine reference, or actually
313inline code by generating a string and C<eval>'ing it.
96e38ba6 314
ba38bf08 315=head1 METHODS
316
317=over 4
318
1385ad9d 319=item B<< Class::MOP::Method::Accessor->new(%options) >>
ba38bf08 320
1385ad9d 321This returns a new C<Class::MOP::Method::Accessor> based on the
322C<%options> provided.
96e38ba6 323
324=over 4
325
9258c369 326=item * attribute
96e38ba6 327
1385ad9d 328This is the C<Class::MOP::Attribute> for which accessors are being
329generated. This option is required.
96e38ba6 330
9258c369 331=item * accessor_type
96e38ba6 332
1385ad9d 333This is a string which should be one of "reader", "writer",
334"accessor", "predicate", or "clearer". This is the type of method
335being generated. This option is required.
96e38ba6 336
9258c369 337=item * is_inline
96e38ba6 338
1385ad9d 339This indicates whether or not the accessor should be inlined. This
cb8d08c6 340defaults to false.
96e38ba6 341
9258c369 342=item * name
343
344The method name (without a package name). This is required.
345
346=item * package_name
347
348The package name for the method. This is required.
349
96e38ba6 350=back
ba38bf08 351
1385ad9d 352=item B<< $metamethod->accessor_type >>
ba38bf08 353
1385ad9d 354Returns the accessor type which was passed to C<new>.
96e38ba6 355
1385ad9d 356=item B<< $metamethod->is_inline >>
ba38bf08 357
1385ad9d 358Returns a boolean indicating whether or not the accessor is inlined.
96e38ba6 359
1385ad9d 360=item B<< $metamethod->associated_attribute >>
ba38bf08 361
1385ad9d 362This returns the L<Class::MOP::Attribute> object which was passed to
363C<new>.
96e38ba6 364
1385ad9d 365=item B<< $metamethod->body >>
96e38ba6 366
1385ad9d 367The method itself is I<generated> when the accessor object is
368constructed.
ba38bf08 369
370=back
371
372=head1 AUTHORS
373
374Stevan Little E<lt>stevan@iinteractive.comE<gt>
375
ba38bf08 376=head1 COPYRIGHT AND LICENSE
377
070bb6c9 378Copyright 2006-2009 by Infinity Interactive, Inc.
ba38bf08 379
380L<http://www.iinteractive.com>
381
382This library is free software; you can redistribute it and/or modify
8d2d4c67 383it under the same terms as Perl itself.
ba38bf08 384
385=cut
386