Use AuthorRequires to force authors to run all tests
[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
2440993d 10our $VERSION = '1.08';
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(@_)
812d58f9 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
e9497117 77sub _initialize_body {
ba38bf08 78 my $self = shift;
8d2d4c67 79
ba38bf08 80 my $method_name = join "_" => (
afc92ac6 81 '_generate',
8d2d4c67 82 $self->accessor_type,
ba38bf08 83 'method',
d90b42a6 84 ($self->is_inline ? 'inline' : ())
ba38bf08 85 );
8d2d4c67 86
1be5f78f 87 $self->{'body'} = $self->$method_name();
ba38bf08 88}
89
90## generators
91
afc92ac6 92sub _generate_accessor_method {
8d2d4c67 93 my $attr = (shift)->associated_attribute;
ba38bf08 94 return sub {
95 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
96 $attr->get_value($_[0]);
97 };
98}
99
afc92ac6 100sub _generate_reader_method {
8d2d4c67 101 my $attr = (shift)->associated_attribute;
102 return sub {
ba38bf08 103 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
104 $attr->get_value($_[0]);
8d2d4c67 105 };
ba38bf08 106}
107
afc92ac6 108
109sub _generate_writer_method {
8d2d4c67 110 my $attr = (shift)->associated_attribute;
ba38bf08 111 return sub {
112 $attr->set_value($_[0], $_[1]);
113 };
114}
115
afc92ac6 116sub _generate_predicate_method {
8d2d4c67 117 my $attr = (shift)->associated_attribute;
118 return sub {
3545c727 119 $attr->has_value($_[0])
ba38bf08 120 };
121}
122
afc92ac6 123sub _generate_clearer_method {
8d2d4c67 124 my $attr = (shift)->associated_attribute;
125 return sub {
3545c727 126 $attr->clear_value($_[0])
ba38bf08 127 };
128}
129
130## Inline methods
131
afc92ac6 132sub _generate_accessor_method_inline {
7f8de9b4 133 my $self = shift;
134 my $attr = $self->associated_attribute;
ba38bf08 135 my $attr_name = $attr->name;
136 my $meta_instance = $attr->associated_class->instance_metaclass;
137
e24b19fb 138 my ( $code, $e ) = $self->_eval_closure(
0c6f3280 139 {},
7f8de9b4 140 'sub {'
a71a4ccb 141 . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]')
7f8de9b4 142 . ' if scalar(@_) == 2; '
a71a4ccb 143 . $meta_instance->inline_get_slot_value('$_[0]', $attr_name)
7f8de9b4 144 . '}'
145 );
e24b19fb 146 confess "Could not generate inline accessor because : $e" if $e;
a6eef5a3 147
148 return $code;
ba38bf08 149}
150
afc92ac6 151sub _generate_reader_method_inline {
7f8de9b4 152 my $self = shift;
153 my $attr = $self->associated_attribute;
ba38bf08 154 my $attr_name = $attr->name;
155 my $meta_instance = $attr->associated_class->instance_metaclass;
156
e24b19fb 157 my ( $code, $e ) = $self->_eval_closure(
0c6f3280 158 {},
7f8de9b4 159 'sub {'
ba38bf08 160 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
e9a19694 161 . $meta_instance->inline_get_slot_value('$_[0]', $attr_name)
7f8de9b4 162 . '}'
163 );
e24b19fb 164 confess "Could not generate inline reader because : $e" if $e;
a6eef5a3 165
166 return $code;
ba38bf08 167}
168
afc92ac6 169sub _generate_writer_method_inline {
7f8de9b4 170 my $self = shift;
171 my $attr = $self->associated_attribute;
ba38bf08 172 my $attr_name = $attr->name;
173 my $meta_instance = $attr->associated_class->instance_metaclass;
174
e24b19fb 175 my ( $code, $e ) = $self->_eval_closure(
0c6f3280 176 {},
7f8de9b4 177 'sub {'
e9a19694 178 . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]')
7f8de9b4 179 . '}'
180 );
e24b19fb 181 confess "Could not generate inline writer because : $e" if $e;
a6eef5a3 182
183 return $code;
ba38bf08 184}
185
afc92ac6 186sub _generate_predicate_method_inline {
7f8de9b4 187 my $self = shift;
188 my $attr = $self->associated_attribute;
ba38bf08 189 my $attr_name = $attr->name;
190 my $meta_instance = $attr->associated_class->instance_metaclass;
191
e24b19fb 192 my ( $code, $e ) = $self->_eval_closure(
0c6f3280 193 {},
7f8de9b4 194 'sub {'
e9a19694 195 . $meta_instance->inline_is_slot_initialized('$_[0]', $attr_name)
7f8de9b4 196 . '}'
197 );
e24b19fb 198 confess "Could not generate inline predicate because : $e" if $e;
a6eef5a3 199
200 return $code;
ba38bf08 201}
202
afc92ac6 203sub _generate_clearer_method_inline {
7f8de9b4 204 my $self = shift;
205 my $attr = $self->associated_attribute;
ba38bf08 206 my $attr_name = $attr->name;
207 my $meta_instance = $attr->associated_class->instance_metaclass;
208
e24b19fb 209 my ( $code, $e ) = $self->_eval_closure(
0c6f3280 210 {},
7f8de9b4 211 'sub {'
e9a19694 212 . $meta_instance->inline_deinitialize_slot('$_[0]', $attr_name)
7f8de9b4 213 . '}'
214 );
e24b19fb 215 confess "Could not generate inline clearer because : $e" if $e;
a6eef5a3 216
217 return $code;
ba38bf08 218}
219
2201;
221
222__END__
223
224=pod
225
8d2d4c67 226=head1 NAME
ba38bf08 227
228Class::MOP::Method::Accessor - Method Meta Object for accessors
229
230=head1 SYNOPSIS
231
96e38ba6 232 use Class::MOP::Method::Accessor;
233
234 my $reader = Class::MOP::Method::Accessor->new(
235 attribute => $attribute,
236 is_inline => 1,
237 accessor_type => 'reader',
238 );
8d2d4c67 239
b7045e66 240 $reader->body->execute($instance); # call the reader method
ba38bf08 241
242=head1 DESCRIPTION
243
1385ad9d 244This is a subclass of <Class::MOP::Method> which is used by
245C<Class::MOP::Attribute> to generate accessor code. It handles
246generation of readers, writers, predicates and clearers. For each type
247of method, it can either create a subroutine reference, or actually
248inline code by generating a string and C<eval>'ing it.
96e38ba6 249
ba38bf08 250=head1 METHODS
251
252=over 4
253
1385ad9d 254=item B<< Class::MOP::Method::Accessor->new(%options) >>
ba38bf08 255
1385ad9d 256This returns a new C<Class::MOP::Method::Accessor> based on the
257C<%options> provided.
96e38ba6 258
259=over 4
260
9258c369 261=item * attribute
96e38ba6 262
1385ad9d 263This is the C<Class::MOP::Attribute> for which accessors are being
264generated. This option is required.
96e38ba6 265
9258c369 266=item * accessor_type
96e38ba6 267
1385ad9d 268This is a string which should be one of "reader", "writer",
269"accessor", "predicate", or "clearer". This is the type of method
270being generated. This option is required.
96e38ba6 271
9258c369 272=item * is_inline
96e38ba6 273
1385ad9d 274This indicates whether or not the accessor should be inlined. This
cb8d08c6 275defaults to false.
96e38ba6 276
9258c369 277=item * name
278
279The method name (without a package name). This is required.
280
281=item * package_name
282
283The package name for the method. This is required.
284
96e38ba6 285=back
ba38bf08 286
1385ad9d 287=item B<< $metamethod->accessor_type >>
ba38bf08 288
1385ad9d 289Returns the accessor type which was passed to C<new>.
96e38ba6 290
1385ad9d 291=item B<< $metamethod->is_inline >>
ba38bf08 292
1385ad9d 293Returns a boolean indicating whether or not the accessor is inlined.
96e38ba6 294
1385ad9d 295=item B<< $metamethod->associated_attribute >>
ba38bf08 296
1385ad9d 297This returns the L<Class::MOP::Attribute> object which was passed to
298C<new>.
96e38ba6 299
1385ad9d 300=item B<< $metamethod->body >>
96e38ba6 301
1385ad9d 302The method itself is I<generated> when the accessor object is
303constructed.
ba38bf08 304
305=back
306
307=head1 AUTHORS
308
309Stevan Little E<lt>stevan@iinteractive.comE<gt>
310
ba38bf08 311=head1 COPYRIGHT AND LICENSE
312
3e2c8600 313Copyright 2006-2010 by Infinity Interactive, Inc.
ba38bf08 314
315L<http://www.iinteractive.com>
316
317This library is free software; you can redistribute it and/or modify
8d2d4c67 318it under the same terms as Perl itself.
ba38bf08 319
320=cut
321