bump version to 0.92_01 and add release date
[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.92_01';
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     my $self = shift;
77     $self->{'meta_instance'} ||= $self->associated_metaclass->get_meta_instance;
78 }
79
80 sub _attributes {
81     my $self = shift;
82     $self->{'attributes'} ||= [ $self->associated_metaclass->get_all_attributes ]
83 }
84
85 ## method
86
87 sub _initialize_body {
88     my $self        = shift;
89     my $method_name = '_generate_constructor_method';
90
91     $method_name .= '_inline' if $self->is_inline;
92
93     $self->{'body'} = $self->$method_name;
94 }
95
96 sub _generate_constructor_method {
97     return sub { Class::MOP::Class->initialize(shift)->new_object(@_) }
98 }
99
100 sub _generate_constructor_method_inline {
101     my $self = shift;
102
103     my $close_over = {};
104
105     my $source = 'sub {';
106     $source .= "\n" . 'my $class = shift;';
107
108     $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)';
109     $source .= "\n" . '    if $class ne \'' . $self->associated_metaclass->name . '\';';
110
111     $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};';
112
113     $source .= "\n" . 'my $instance = ' . $self->_meta_instance->inline_create_instance('$class');
114     $source .= ";\n" . (join ";\n" => map {
115         $self->_generate_slot_initializer($_, $close_over)
116     } @{ $self->_attributes });
117     $source .= ";\n" . 'return $instance';
118     $source .= ";\n" . '}';
119     warn $source if $self->options->{debug};
120
121     my ( $code, $e ) = $self->_eval_closure(
122         $close_over,
123         $source
124     );
125     confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$e" if $e;
126
127     return $code;
128 }
129
130 sub _generate_slot_initializer {
131     my $self  = shift;
132     my $attr  = shift;
133     my $close = shift;
134
135     my $default;
136     if ($attr->has_default) {
137         # NOTE:
138         # default values can either be CODE refs
139         # in which case we need to call them. Or
140         # they can be scalars (strings/numbers)
141         # in which case we can just deal with them
142         # in the code we eval.
143         if ($attr->is_default_a_coderef) {
144             my $idx = @{$close->{'@defaults'}||=[]};
145             push(@{$close->{'@defaults'}}, $attr->default);
146             $default = '$defaults[' . $idx . ']->($instance)';
147         }
148         else {
149             $default = $attr->default;
150             # make sure to quote strings ...
151             unless (looks_like_number($default)) {
152                 $default = "'$default'";
153             }
154         }
155     } elsif( $attr->has_builder ) {
156         $default = '$instance->'.$attr->builder;
157     }
158
159     if ( defined(my $init_arg = $attr->init_arg) ) {
160       return (
161           'if(exists $params->{\'' . $init_arg . '\'}){' . "\n" .
162                 $self->_meta_instance->inline_set_slot_value(
163                     '$instance',
164                     $attr->name,
165                     '$params->{\'' . $init_arg . '\'}' ) . "\n" .
166            '} ' . (!defined $default ? '' : 'else {' . "\n" .
167                 $self->_meta_instance->inline_set_slot_value(
168                     '$instance',
169                     $attr->name,
170                      $default ) . "\n" .
171            '}')
172         );
173     } elsif ( defined $default ) {
174         return (
175             $self->_meta_instance->inline_set_slot_value(
176                 '$instance',
177                 $attr->name,
178                  $default ) . "\n"
179         );
180     } else { return '' }
181 }
182
183 1;
184
185 __END__
186
187 =pod
188
189 =head1 NAME
190
191 Class::MOP::Method::Constructor - Method Meta Object for constructors
192
193 =head1 SYNOPSIS
194
195   use Class::MOP::Method::Constructor;
196
197   my $constructor = Class::MOP::Method::Constructor->new(
198       metaclass => $metaclass,
199       options   => {
200           debug => 1, # this is all for now
201       },
202   );
203
204   # calling the constructor ...
205   $constructor->body->execute($metaclass->name, %params);
206
207 =head1 DESCRIPTION
208
209 This is a subclass of C<Class::MOP::Method> which generates
210 constructor methods.
211
212 =head1 METHODS
213
214 =over 4
215
216 =item B<< Class::MOP::Method::Constructor->new(%options) >>
217
218 This creates a new constructor object. It accepts a hash reference of
219 options.
220
221 =over 8
222
223 =item * metaclass
224
225 This should be a L<Class::MOP::Class> object. It is required.
226
227 =item * name
228
229 The method name (without a package name). This is required.
230
231 =item * package_name
232
233 The package name for the method. This is required.
234
235 =item * is_inline
236
237 This indicates whether or not the constructor should be inlined. This
238 defaults to false.
239
240 =back
241
242 =item B<< $metamethod->is_inline >>
243
244 Returns a boolean indicating whether or not the constructor is
245 inlined.
246
247 =item B<< $metamethod->associated_metaclass >>
248
249 This returns the L<Class::MOP::Class> object for the method.
250
251 =back
252
253 =head1 AUTHORS
254
255 Stevan Little E<lt>stevan@iinteractive.comE<gt>
256
257 =head1 COPYRIGHT AND LICENSE
258
259 Copyright 2006-2009 by Infinity Interactive, Inc.
260
261 L<http://www.iinteractive.com>
262
263 This library is free software; you can redistribute it and/or modify
264 it under the same terms as Perl itself.
265
266 =cut
267