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