Commit | Line | Data |
d90b42a6 |
1 | |
2 | package Class::MOP::Method::Constructor; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
7 | use Carp 'confess'; |
8 | use Scalar::Util 'blessed', 'weaken', 'looks_like_number'; |
9 | |
19042e4d |
10 | our $VERSION = '0.92'; |
d519662a |
11 | $VERSION = eval $VERSION; |
d90b42a6 |
12 | our $AUTHORITY = 'cpan:STEVAN'; |
13 | |
29d4e92a |
14 | use base 'Class::MOP::Method::Inlined'; |
d90b42a6 |
15 | |
16 | sub new { |
17 | my $class = shift; |
18 | my %options = @_; |
8d2d4c67 |
19 | |
ad315b75 |
20 | (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class')) |
21 | || confess "You must pass a metaclass instance if you want to inline" |
8d2d4c67 |
22 | if $options{is_inline}; |
23 | |
b38f3848 |
24 | ($options{package_name} && $options{name}) |
32202ce2 |
25 | || confess "You must supply the package_name and name parameters $Class::MOP::Method::UPGRADE_ERROR_TEXT"; |
b38f3848 |
26 | |
0bfc85b8 |
27 | my $self = $class->_new(\%options); |
d90b42a6 |
28 | |
8d2d4c67 |
29 | # we don't want this creating |
30 | # a cycle in the code, if not |
d90b42a6 |
31 | # needed |
8683db0e |
32 | weaken($self->{'associated_metaclass'}); |
d90b42a6 |
33 | |
8f7852d8 |
34 | $self->_initialize_body; |
d90b42a6 |
35 | |
8d2d4c67 |
36 | return $self; |
d90b42a6 |
37 | } |
38 | |
28b97bef |
39 | sub _new { |
0bfc85b8 |
40 | my $class = shift; |
812d58f9 |
41 | |
ec9e38e5 |
42 | return Class::MOP::Class->initialize($class)->new_object(@_) |
812d58f9 |
43 | if $class ne __PACKAGE__; |
ec9e38e5 |
44 | |
45 | my $params = @_ == 1 ? $_[0] : {@_}; |
46 | |
47 | return bless { |
48 | # inherited from Class::MOP::Method |
49 | body => $params->{body}, |
50 | # associated_metaclass => $params->{associated_metaclass}, # overriden |
51 | package_name => $params->{package_name}, |
52 | name => $params->{name}, |
53 | original_method => $params->{original_method}, |
54 | |
55 | # inherited from Class::MOP::Generated |
56 | is_inline => $params->{is_inline} || 0, |
57 | definition_context => $params->{definition_context}, |
58 | |
59 | # inherited from Class::MOP::Inlined |
60 | _expected_method_class => $params->{_expected_method_class}, |
61 | |
62 | # defined in this subclass |
63 | options => $params->{options} || {}, |
64 | associated_metaclass => $params->{metaclass}, |
28b97bef |
65 | }, $class; |
66 | } |
67 | |
565f0cbb |
68 | ## cached values ... |
d90b42a6 |
69 | |
8d2d4c67 |
70 | sub meta_instance { |
c7e28c19 |
71 | Carp::cluck('The meta_instance method has been made private.' |
72 | . " The public version is deprecated and will be removed in a future release.\n"); |
d5c7f638 |
73 | shift->_meta_instance; |
5a49fb79 |
74 | } |
75 | |
76 | sub _meta_instance { |
565f0cbb |
77 | my $self = shift; |
8683db0e |
78 | $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance; |
565f0cbb |
79 | } |
c23184fc |
80 | |
8d2d4c67 |
81 | sub attributes { |
c7e28c19 |
82 | Carp::cluck('The attributes method has been made private.' |
83 | . " The public version is deprecated and will be removed in a future release.\n"); |
6e2f5a81 |
84 | |
780f14c2 |
85 | return shift->_attributes; |
86 | } |
87 | |
88 | sub _attributes { |
565f0cbb |
89 | my $self = shift; |
2620be77 |
90 | $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ] |
565f0cbb |
91 | } |
d90b42a6 |
92 | |
93 | ## method |
94 | |
565f0cbb |
95 | sub initialize_body { |
c7e28c19 |
96 | Carp::cluck('The initialize_body method has been made private.' |
97 | . " The public version is deprecated and will be removed in a future release.\n"); |
d5c7f638 |
98 | shift->_initialize_body; |
8f7852d8 |
99 | } |
100 | |
101 | sub _initialize_body { |
565f0cbb |
102 | my $self = shift; |
ecb874a0 |
103 | my $method_name = '_generate_constructor_method'; |
8d2d4c67 |
104 | |
565f0cbb |
105 | $method_name .= '_inline' if $self->is_inline; |
8d2d4c67 |
106 | |
8683db0e |
107 | $self->{'body'} = $self->$method_name; |
565f0cbb |
108 | } |
109 | |
110 | sub generate_constructor_method { |
c7e28c19 |
111 | Carp::cluck('The generate_constructor_method method has been made private.' |
112 | . " The public version is deprecated and will be removed in a future release.\n"); |
d5c7f638 |
113 | shift->_generate_constructor_method; |
ecb874a0 |
114 | } |
115 | |
116 | sub _generate_constructor_method { |
2a2b8458 |
117 | return sub { Class::MOP::Class->initialize(shift)->new_object(@_) } |
565f0cbb |
118 | } |
119 | |
120 | sub generate_constructor_method_inline { |
c7e28c19 |
121 | Carp::cluck('The generate_constructor_method_inline method has been made private.' |
122 | . " The public version is deprecated and will be removed in a future release.\n"); |
d5c7f638 |
123 | shift->_generate_constructor_method_inline; |
ecb874a0 |
124 | } |
125 | |
126 | sub _generate_constructor_method_inline { |
d90b42a6 |
127 | my $self = shift; |
565f0cbb |
128 | |
0c6f3280 |
129 | my $close_over = {}; |
130 | |
d90b42a6 |
131 | my $source = 'sub {'; |
26ffbb36 |
132 | $source .= "\n" . 'my $class = shift;'; |
8d2d4c67 |
133 | |
26ffbb36 |
134 | $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)'; |
8d2d4c67 |
135 | $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';'; |
136 | |
26ffbb36 |
137 | $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};'; |
138 | |
5a49fb79 |
139 | $source .= "\n" . 'my $instance = ' . $self->_meta_instance->inline_create_instance('$class'); |
8d2d4c67 |
140 | $source .= ";\n" . (join ";\n" => map { |
82cff1ae |
141 | $self->_generate_slot_initializer($_, $close_over) |
780f14c2 |
142 | } @{ $self->_attributes }); |
d90b42a6 |
143 | $source .= ";\n" . 'return $instance'; |
8d2d4c67 |
144 | $source .= ";\n" . '}'; |
145 | warn $source if $self->options->{debug}; |
146 | |
e24b19fb |
147 | my ( $code, $e ) = $self->_eval_closure( |
ffe92c8b |
148 | $close_over, |
149 | $source |
150 | ); |
e24b19fb |
151 | confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e" if $e; |
a6eef5a3 |
152 | |
153 | return $code; |
d90b42a6 |
154 | } |
155 | |
156 | sub _generate_slot_initializer { |
157 | my $self = shift; |
64adcd8d |
158 | my $attr = shift; |
82cff1ae |
159 | my $close = shift; |
8d2d4c67 |
160 | |
d90b42a6 |
161 | my $default; |
162 | if ($attr->has_default) { |
163 | # NOTE: |
164 | # default values can either be CODE refs |
8d2d4c67 |
165 | # in which case we need to call them. Or |
d90b42a6 |
166 | # they can be scalars (strings/numbers) |
167 | # in which case we can just deal with them |
168 | # in the code we eval. |
169 | if ($attr->is_default_a_coderef) { |
82cff1ae |
170 | my $idx = @{$close->{'@defaults'}||=[]}; |
171 | push(@{$close->{'@defaults'}}, $attr->default); |
172 | $default = '$defaults[' . $idx . ']->($instance)'; |
d90b42a6 |
173 | } |
174 | else { |
175 | $default = $attr->default; |
176 | # make sure to quote strings ... |
177 | unless (looks_like_number($default)) { |
178 | $default = "'$default'"; |
179 | } |
180 | } |
8d2d4c67 |
181 | } elsif( $attr->has_builder ) { |
182 | $default = '$instance->'.$attr->builder; |
d90b42a6 |
183 | } |
8d2d4c67 |
184 | |
2f2329a4 |
185 | if ( defined(my $init_arg = $attr->init_arg) ) { |
c16c9c1b |
186 | return ( |
2f2329a4 |
187 | 'if(exists $params->{\'' . $init_arg . '\'}){' . "\n" . |
5a49fb79 |
188 | $self->_meta_instance->inline_set_slot_value( |
c16c9c1b |
189 | '$instance', |
e9a19694 |
190 | $attr->name, |
2f2329a4 |
191 | '$params->{\'' . $init_arg . '\'}' ) . "\n" . |
c16c9c1b |
192 | '} ' . (!defined $default ? '' : 'else {' . "\n" . |
5a49fb79 |
193 | $self->_meta_instance->inline_set_slot_value( |
c16c9c1b |
194 | '$instance', |
e9a19694 |
195 | $attr->name, |
c16c9c1b |
196 | $default ) . "\n" . |
197 | '}') |
198 | ); |
199 | } elsif ( defined $default ) { |
200 | return ( |
5a49fb79 |
201 | $self->_meta_instance->inline_set_slot_value( |
c16c9c1b |
202 | '$instance', |
e9a19694 |
203 | $attr->name, |
c16c9c1b |
204 | $default ) . "\n" |
205 | ); |
206 | } else { return '' } |
d90b42a6 |
207 | } |
208 | |
209 | 1; |
210 | |
d90b42a6 |
211 | __END__ |
212 | |
213 | =pod |
214 | |
8d2d4c67 |
215 | =head1 NAME |
d90b42a6 |
216 | |
217 | Class::MOP::Method::Constructor - Method Meta Object for constructors |
218 | |
219 | =head1 SYNOPSIS |
220 | |
96e38ba6 |
221 | use Class::MOP::Method::Constructor; |
8d2d4c67 |
222 | |
96e38ba6 |
223 | my $constructor = Class::MOP::Method::Constructor->new( |
8d2d4c67 |
224 | metaclass => $metaclass, |
96e38ba6 |
225 | options => { |
226 | debug => 1, # this is all for now |
8d2d4c67 |
227 | }, |
96e38ba6 |
228 | ); |
8d2d4c67 |
229 | |
96e38ba6 |
230 | # calling the constructor ... |
b7045e66 |
231 | $constructor->body->execute($metaclass->name, %params); |
8d2d4c67 |
232 | |
d90b42a6 |
233 | =head1 DESCRIPTION |
234 | |
3fd960d9 |
235 | This is a subclass of C<Class::MOP::Method> which generates |
236 | constructor methods. |
96e38ba6 |
237 | |
d90b42a6 |
238 | =head1 METHODS |
239 | |
240 | =over 4 |
241 | |
3fd960d9 |
242 | =item B<< Class::MOP::Method::Constructor->new(%options) >> |
d90b42a6 |
243 | |
3fd960d9 |
244 | This creates a new constructor object. It accepts a hash reference of |
245 | options. |
96e38ba6 |
246 | |
3fd960d9 |
247 | =over 8 |
96e38ba6 |
248 | |
3fd960d9 |
249 | =item * metaclass |
96e38ba6 |
250 | |
3fd960d9 |
251 | This should be a L<Class::MOP::Class> object. It is required. |
c23184fc |
252 | |
3fd960d9 |
253 | =item * name |
d90b42a6 |
254 | |
3fd960d9 |
255 | The method name (without a package name). This is required. |
96e38ba6 |
256 | |
3fd960d9 |
257 | =item * package_name |
d90b42a6 |
258 | |
3fd960d9 |
259 | The package name for the method. This is required. |
c23184fc |
260 | |
3fd960d9 |
261 | =item * is_inline |
96e38ba6 |
262 | |
3fd960d9 |
263 | This indicates whether or not the constructor should be inlined. This |
264 | defaults to false. |
d90b42a6 |
265 | |
3fd960d9 |
266 | =back |
f0de47d9 |
267 | |
3fd960d9 |
268 | =item B<< $metamethod->is_inline >> |
f0de47d9 |
269 | |
3fd960d9 |
270 | Returns a boolean indicating whether or not the constructor is |
271 | inlined. |
d90b42a6 |
272 | |
3fd960d9 |
273 | =item B<< $metamethod->associated_metaclass >> |
96e38ba6 |
274 | |
3fd960d9 |
275 | This returns the L<Class::MOP::Class> object for the method. |
d90b42a6 |
276 | |
565f0cbb |
277 | =back |
278 | |
d90b42a6 |
279 | =head1 AUTHORS |
280 | |
281 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
282 | |
283 | =head1 COPYRIGHT AND LICENSE |
284 | |
070bb6c9 |
285 | Copyright 2006-2009 by Infinity Interactive, Inc. |
d90b42a6 |
286 | |
287 | L<http://www.iinteractive.com> |
288 | |
289 | This library is free software; you can redistribute it and/or modify |
8d2d4c67 |
290 | it under the same terms as Perl itself. |
d90b42a6 |
291 | |
292 | =cut |
293 | |