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