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 | |
0c6f4c4a |
10 | our $VERSION = '0.04'; |
d90b42a6 |
11 | our $AUTHORITY = 'cpan:STEVAN'; |
12 | |
565f0cbb |
13 | use base 'Class::MOP::Method::Generated'; |
d90b42a6 |
14 | |
15 | sub new { |
16 | my $class = shift; |
17 | my %options = @_; |
8d2d4c67 |
18 | |
ad315b75 |
19 | (blessed $options{metaclass} && $options{metaclass}->isa('Class::MOP::Class')) |
20 | || confess "You must pass a metaclass instance if you want to inline" |
8d2d4c67 |
21 | if $options{is_inline}; |
22 | |
d90b42a6 |
23 | my $self = bless { |
24 | # from our superclass |
c23184fc |
25 | '&!body' => undef, |
d90b42a6 |
26 | # specific to this subclass |
ad315b75 |
27 | '%!options' => $options{options} || {}, |
c23184fc |
28 | '$!associated_metaclass' => $options{metaclass}, |
8d2d4c67 |
29 | '$!is_inline' => ($options{is_inline} || 0), |
d90b42a6 |
30 | } => $class; |
31 | |
8d2d4c67 |
32 | # we don't want this creating |
33 | # a cycle in the code, if not |
d90b42a6 |
34 | # needed |
8d2d4c67 |
35 | weaken($self->{'$!associated_metaclass'}); |
d90b42a6 |
36 | |
565f0cbb |
37 | $self->initialize_body; |
d90b42a6 |
38 | |
8d2d4c67 |
39 | return $self; |
d90b42a6 |
40 | } |
41 | |
8d2d4c67 |
42 | ## accessors |
c23184fc |
43 | |
565f0cbb |
44 | sub options { (shift)->{'%!options'} } |
45 | sub associated_metaclass { (shift)->{'$!associated_metaclass'} } |
c23184fc |
46 | |
565f0cbb |
47 | ## cached values ... |
d90b42a6 |
48 | |
8d2d4c67 |
49 | sub meta_instance { |
565f0cbb |
50 | my $self = shift; |
51 | $self->{'$!meta_instance'} ||= $self->associated_metaclass->get_meta_instance; |
52 | } |
c23184fc |
53 | |
8d2d4c67 |
54 | sub attributes { |
565f0cbb |
55 | my $self = shift; |
56 | $self->{'@!attributes'} ||= [ $self->associated_metaclass->compute_all_applicable_attributes ] |
57 | } |
d90b42a6 |
58 | |
59 | ## method |
60 | |
565f0cbb |
61 | sub initialize_body { |
62 | my $self = shift; |
63 | my $method_name = 'generate_constructor_method'; |
8d2d4c67 |
64 | |
565f0cbb |
65 | $method_name .= '_inline' if $self->is_inline; |
8d2d4c67 |
66 | |
565f0cbb |
67 | $self->{'&!body'} = $self->$method_name; |
68 | } |
69 | |
70 | sub generate_constructor_method { |
2a2b8458 |
71 | return sub { Class::MOP::Class->initialize(shift)->new_object(@_) } |
565f0cbb |
72 | } |
73 | |
74 | sub generate_constructor_method_inline { |
d90b42a6 |
75 | my $self = shift; |
565f0cbb |
76 | |
d90b42a6 |
77 | my $source = 'sub {'; |
78 | $source .= "\n" . 'my ($class, %params) = @_;'; |
8d2d4c67 |
79 | |
2a2b8458 |
80 | $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(%params)'; |
8d2d4c67 |
81 | $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';'; |
82 | |
d90b42a6 |
83 | $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class'); |
8d2d4c67 |
84 | $source .= ";\n" . (join ";\n" => map { |
85 | $self->_generate_slot_initializer($_) |
d90b42a6 |
86 | } 0 .. (@{$self->attributes} - 1)); |
87 | $source .= ";\n" . 'return $instance'; |
8d2d4c67 |
88 | $source .= ";\n" . '}'; |
89 | warn $source if $self->options->{debug}; |
90 | |
d90b42a6 |
91 | my $code; |
92 | { |
93 | # NOTE: |
94 | # create the nessecary lexicals |
8d2d4c67 |
95 | # to be picked up in the eval |
d90b42a6 |
96 | my $attrs = $self->attributes; |
8d2d4c67 |
97 | |
d90b42a6 |
98 | $code = eval $source; |
99 | confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@; |
100 | } |
565f0cbb |
101 | return $code; |
d90b42a6 |
102 | } |
103 | |
104 | sub _generate_slot_initializer { |
105 | my $self = shift; |
106 | my $index = shift; |
8d2d4c67 |
107 | |
d90b42a6 |
108 | my $attr = $self->attributes->[$index]; |
8d2d4c67 |
109 | |
d90b42a6 |
110 | my $default; |
111 | if ($attr->has_default) { |
112 | # NOTE: |
113 | # default values can either be CODE refs |
8d2d4c67 |
114 | # in which case we need to call them. Or |
d90b42a6 |
115 | # they can be scalars (strings/numbers) |
116 | # in which case we can just deal with them |
117 | # in the code we eval. |
118 | if ($attr->is_default_a_coderef) { |
119 | $default = '$attrs->[' . $index . ']->default($instance)'; |
120 | } |
121 | else { |
122 | $default = $attr->default; |
123 | # make sure to quote strings ... |
124 | unless (looks_like_number($default)) { |
125 | $default = "'$default'"; |
126 | } |
127 | } |
8d2d4c67 |
128 | } elsif( $attr->has_builder ) { |
129 | $default = '$instance->'.$attr->builder; |
d90b42a6 |
130 | } |
8d2d4c67 |
131 | |
c16c9c1b |
132 | if ( defined $attr->init_arg ) { |
133 | return ( |
134 | 'if(exists $params{\'' . $attr->init_arg . '\'}){' . "\n" . |
135 | $self->meta_instance->inline_set_slot_value( |
136 | '$instance', |
137 | ("'" . $attr->name . "'"), |
138 | '$params{\'' . $attr->init_arg . '\'}' ) . "\n" . |
139 | '} ' . (!defined $default ? '' : 'else {' . "\n" . |
140 | $self->meta_instance->inline_set_slot_value( |
141 | '$instance', |
142 | ("'" . $attr->name . "'"), |
143 | $default ) . "\n" . |
144 | '}') |
145 | ); |
146 | } elsif ( defined $default ) { |
147 | return ( |
148 | $self->meta_instance->inline_set_slot_value( |
149 | '$instance', |
150 | ("'" . $attr->name . "'"), |
151 | $default ) . "\n" |
152 | ); |
153 | } else { return '' } |
d90b42a6 |
154 | } |
155 | |
156 | 1; |
157 | |
158 | 1; |
159 | |
160 | __END__ |
161 | |
162 | =pod |
163 | |
8d2d4c67 |
164 | =head1 NAME |
d90b42a6 |
165 | |
166 | Class::MOP::Method::Constructor - Method Meta Object for constructors |
167 | |
168 | =head1 SYNOPSIS |
169 | |
96e38ba6 |
170 | use Class::MOP::Method::Constructor; |
8d2d4c67 |
171 | |
96e38ba6 |
172 | my $constructor = Class::MOP::Method::Constructor->new( |
8d2d4c67 |
173 | metaclass => $metaclass, |
96e38ba6 |
174 | options => { |
175 | debug => 1, # this is all for now |
8d2d4c67 |
176 | }, |
96e38ba6 |
177 | ); |
8d2d4c67 |
178 | |
96e38ba6 |
179 | # calling the constructor ... |
180 | $constructor->body->($metaclass->name, %params); |
8d2d4c67 |
181 | |
d90b42a6 |
182 | =head1 DESCRIPTION |
183 | |
8d2d4c67 |
184 | This is a subclass of C<Class::MOP::Method> which deals with |
185 | class constructors. |
96e38ba6 |
186 | |
d90b42a6 |
187 | =head1 METHODS |
188 | |
189 | =over 4 |
190 | |
96e38ba6 |
191 | =item B<new (metaclass => $meta, options => \%options)> |
d90b42a6 |
192 | |
96e38ba6 |
193 | =item B<options> |
194 | |
195 | This returns the options HASH which is passed into C<new>. |
196 | |
197 | =item B<associated_metaclass> |
198 | |
199 | This returns the metaclass which is passed into C<new>. |
c23184fc |
200 | |
d90b42a6 |
201 | =item B<attributes> |
202 | |
8d2d4c67 |
203 | This returns the list of attributes which are associated with the |
96e38ba6 |
204 | metaclass which is passed into C<new>. |
205 | |
d90b42a6 |
206 | =item B<meta_instance> |
207 | |
8d2d4c67 |
208 | This returns the meta instance which is associated with the |
96e38ba6 |
209 | metaclass which is passed into C<new>. |
c23184fc |
210 | |
96e38ba6 |
211 | =item B<is_inline> |
212 | |
8d2d4c67 |
213 | This returns a boolean, but since constructors are very rarely |
96e38ba6 |
214 | not inlined, this always returns true for now. |
d90b42a6 |
215 | |
565f0cbb |
216 | =item B<initialize_body> |
d90b42a6 |
217 | |
8d2d4c67 |
218 | This creates the code reference for the constructor itself. |
96e38ba6 |
219 | |
d90b42a6 |
220 | =back |
221 | |
565f0cbb |
222 | =head2 Method Generators |
223 | |
224 | =over 4 |
225 | |
226 | =item B<generate_constructor_method> |
227 | |
228 | =item B<generate_constructor_method_inline> |
229 | |
230 | =back |
231 | |
d90b42a6 |
232 | =head1 AUTHORS |
233 | |
234 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
235 | |
236 | =head1 COPYRIGHT AND LICENSE |
237 | |
69e3ab0a |
238 | Copyright 2006-2008 by Infinity Interactive, Inc. |
d90b42a6 |
239 | |
240 | L<http://www.iinteractive.com> |
241 | |
242 | This library is free software; you can redistribute it and/or modify |
8d2d4c67 |
243 | it under the same terms as Perl itself. |
d90b42a6 |
244 | |
245 | =cut |
246 | |