bump the version so I can make svn Moose require this version
[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
28fa06b5 10our $VERSION = '0.78';
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
565f0cbb 39 $self->initialize_body;
8d2d4c67 40
ba38bf08 41 return $self;
42}
43
a9e38dc7 44sub _new {
0bfc85b8 45 my $class = shift;
46 my $options = @_ == 1 ? $_[0] : {@_};
a9e38dc7 47
0bfc85b8 48 $options->{is_inline} ||= 0;
a9e38dc7 49
0bfc85b8 50 return bless $options, $class;
a9e38dc7 51}
52
ba38bf08 53## accessors
54
8683db0e 55sub associated_attribute { (shift)->{'attribute'} }
56sub accessor_type { (shift)->{'accessor_type'} }
ba38bf08 57
8d2d4c67 58## factory
ba38bf08 59
565f0cbb 60sub initialize_body {
ba38bf08 61 my $self = shift;
8d2d4c67 62
ba38bf08 63 my $method_name = join "_" => (
8d2d4c67 64 'generate',
65 $self->accessor_type,
ba38bf08 66 'method',
d90b42a6 67 ($self->is_inline ? 'inline' : ())
ba38bf08 68 );
8d2d4c67 69
ffe92c8b 70 $self->{'body'} = $self->$method_name();
ba38bf08 71}
72
73## generators
74
75sub generate_accessor_method {
8d2d4c67 76 my $attr = (shift)->associated_attribute;
ba38bf08 77 return sub {
78 $attr->set_value($_[0], $_[1]) if scalar(@_) == 2;
79 $attr->get_value($_[0]);
80 };
81}
82
83sub generate_reader_method {
8d2d4c67 84 my $attr = (shift)->associated_attribute;
85 return sub {
ba38bf08 86 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
87 $attr->get_value($_[0]);
8d2d4c67 88 };
ba38bf08 89}
90
91sub generate_writer_method {
8d2d4c67 92 my $attr = (shift)->associated_attribute;
ba38bf08 93 return sub {
94 $attr->set_value($_[0], $_[1]);
95 };
96}
97
98sub generate_predicate_method {
8d2d4c67 99 my $attr = (shift)->associated_attribute;
100 return sub {
3545c727 101 $attr->has_value($_[0])
ba38bf08 102 };
103}
104
105sub generate_clearer_method {
8d2d4c67 106 my $attr = (shift)->associated_attribute;
107 return sub {
3545c727 108 $attr->clear_value($_[0])
ba38bf08 109 };
110}
111
112## Inline methods
113
114
115sub generate_accessor_method_inline {
7f8de9b4 116 my $self = shift;
117 my $attr = $self->associated_attribute;
ba38bf08 118 my $attr_name = $attr->name;
119 my $meta_instance = $attr->associated_class->instance_metaclass;
120
ffe92c8b 121 return $self->_eval_closure(
0c6f3280 122 {},
7f8de9b4 123 'sub {'
ffe92c8b 124 . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]')
7f8de9b4 125 . ' if scalar(@_) == 2; '
ffe92c8b 126 . $meta_instance->inline_get_slot_value('$_[0]', $attr_name)
7f8de9b4 127 . '}'
128 );
ba38bf08 129}
130
131sub generate_reader_method_inline {
7f8de9b4 132 my $self = shift;
133 my $attr = $self->associated_attribute;
ba38bf08 134 my $attr_name = $attr->name;
135 my $meta_instance = $attr->associated_class->instance_metaclass;
136
ffe92c8b 137 return $self->_eval_closure(
0c6f3280 138 {},
7f8de9b4 139 'sub {'
ba38bf08 140 . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
e9a19694 141 . $meta_instance->inline_get_slot_value('$_[0]', $attr_name)
7f8de9b4 142 . '}'
143 );
ba38bf08 144}
145
146sub generate_writer_method_inline {
7f8de9b4 147 my $self = shift;
148 my $attr = $self->associated_attribute;
ba38bf08 149 my $attr_name = $attr->name;
150 my $meta_instance = $attr->associated_class->instance_metaclass;
151
ffe92c8b 152 return $self->_eval_closure(
0c6f3280 153 {},
7f8de9b4 154 'sub {'
e9a19694 155 . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]')
7f8de9b4 156 . '}'
157 );
ba38bf08 158}
159
160
161sub generate_predicate_method_inline {
7f8de9b4 162 my $self = shift;
163 my $attr = $self->associated_attribute;
ba38bf08 164 my $attr_name = $attr->name;
165 my $meta_instance = $attr->associated_class->instance_metaclass;
166
ffe92c8b 167 return $self->_eval_closure(
0c6f3280 168 {},
7f8de9b4 169 'sub {'
e9a19694 170 . $meta_instance->inline_is_slot_initialized('$_[0]', $attr_name)
7f8de9b4 171 . '}'
172 );
ba38bf08 173}
174
175sub generate_clearer_method_inline {
7f8de9b4 176 my $self = shift;
177 my $attr = $self->associated_attribute;
ba38bf08 178 my $attr_name = $attr->name;
179 my $meta_instance = $attr->associated_class->instance_metaclass;
180
ffe92c8b 181 return $self->_eval_closure(
0c6f3280 182 {},
7f8de9b4 183 'sub {'
e9a19694 184 . $meta_instance->inline_deinitialize_slot('$_[0]', $attr_name)
7f8de9b4 185 . '}'
186 );
ba38bf08 187}
188
1891;
190
191__END__
192
193=pod
194
8d2d4c67 195=head1 NAME
ba38bf08 196
197Class::MOP::Method::Accessor - Method Meta Object for accessors
198
199=head1 SYNOPSIS
200
96e38ba6 201 use Class::MOP::Method::Accessor;
202
203 my $reader = Class::MOP::Method::Accessor->new(
204 attribute => $attribute,
205 is_inline => 1,
206 accessor_type => 'reader',
207 );
8d2d4c67 208
b7045e66 209 $reader->body->execute($instance); # call the reader method
ba38bf08 210
211=head1 DESCRIPTION
212
8d2d4c67 213This is a C<Class::MOP::Method> subclass which is used interally
214by C<Class::MOP::Attribute> to generate accessor code. It can
215handle generation of readers, writers, predicate and clearer
96e38ba6 216methods, both as closures and as more optimized inline methods.
217
ba38bf08 218=head1 METHODS
219
220=over 4
221
96e38ba6 222=item B<new (%options)>
ba38bf08 223
8d2d4c67 224This creates the method based on the criteria in C<%options>,
96e38ba6 225these options are:
226
227=over 4
228
229=item I<attribute>
230
8d2d4c67 231This must be an instance of C<Class::MOP::Attribute> which this
96e38ba6 232accessor is being generated for. This paramter is B<required>.
233
234=item I<accessor_type>
235
8d2d4c67 236This is a string from the following set; reader, writer, accessor,
237predicate or clearer. This is used to determine which type of
96e38ba6 238method is to be generated.
239
240=item I<is_inline>
241
242This is a boolean to indicate if the method should be generated
243as a closure, or as a more optimized inline version.
244
245=back
ba38bf08 246
247=item B<accessor_type>
248
96e38ba6 249This returns the accessor type which was passed into C<new>.
250
d90b42a6 251=item B<is_inline>
ba38bf08 252
96e38ba6 253This returns the boolean which was passed into C<new>.
254
ba38bf08 255=item B<associated_attribute>
256
96e38ba6 257This returns the attribute instance which was passed into C<new>.
258
565f0cbb 259=item B<initialize_body>
96e38ba6 260
8d2d4c67 261This will actually generate the method based on the specified
96e38ba6 262criteria passed to the constructor.
263
264=back
265
266=head2 Method Generators
267
8d2d4c67 268These methods will generate appropriate code references for
269the various types of accessors which are supported by
96e38ba6 270C<Class::MOP::Attribute>. The names pretty much explain it all.
271
272=over 4
273
ba38bf08 274=item B<generate_accessor_method>
275
276=item B<generate_accessor_method_inline>
277
278=item B<generate_clearer_method>
279
280=item B<generate_clearer_method_inline>
281
282=item B<generate_predicate_method>
283
284=item B<generate_predicate_method_inline>
285
286=item B<generate_reader_method>
287
288=item B<generate_reader_method_inline>
289
290=item B<generate_writer_method>
291
292=item B<generate_writer_method_inline>
293
294=back
295
296=head1 AUTHORS
297
298Stevan Little E<lt>stevan@iinteractive.comE<gt>
299
ba38bf08 300=head1 COPYRIGHT AND LICENSE
301
69e3ab0a 302Copyright 2006-2008 by Infinity Interactive, Inc.
ba38bf08 303
304L<http://www.iinteractive.com>
305
306This library is free software; you can redistribute it and/or modify
8d2d4c67 307it under the same terms as Perl itself.
ba38bf08 308
309=cut
310