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