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