accept hash ref to _new
[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 {';
26ffbb36 89 $source .= "\n" . 'my $class = shift;';
8d2d4c67 90
26ffbb36 91 $source .= "\n" . 'return Class::MOP::Class->initialize($class)->new_object(@_)';
8d2d4c67 92 $source .= "\n" . ' if $class ne \'' . $self->associated_metaclass->name . '\';';
93
26ffbb36 94 $source .= "\n" . 'my $params = @_ == 1 ? $_[0] : {@_};';
95
d90b42a6 96 $source .= "\n" . 'my $instance = ' . $self->meta_instance->inline_create_instance('$class');
8d2d4c67 97 $source .= ";\n" . (join ";\n" => map {
98 $self->_generate_slot_initializer($_)
d90b42a6 99 } 0 .. (@{$self->attributes} - 1));
100 $source .= ";\n" . 'return $instance';
8d2d4c67 101 $source .= ";\n" . '}';
102 warn $source if $self->options->{debug};
103
d90b42a6 104 my $code;
105 {
106 # NOTE:
107 # create the nessecary lexicals
8d2d4c67 108 # to be picked up in the eval
d90b42a6 109 my $attrs = $self->attributes;
8d2d4c67 110
d90b42a6 111 $code = eval $source;
112 confess "Could not eval the constructor :\n\n$source\n\nbecause :\n\n$@" if $@;
113 }
565f0cbb 114 return $code;
d90b42a6 115}
116
117sub _generate_slot_initializer {
118 my $self = shift;
119 my $index = shift;
8d2d4c67 120
d90b42a6 121 my $attr = $self->attributes->[$index];
8d2d4c67 122
d90b42a6 123 my $default;
124 if ($attr->has_default) {
125 # NOTE:
126 # default values can either be CODE refs
8d2d4c67 127 # in which case we need to call them. Or
d90b42a6 128 # they can be scalars (strings/numbers)
129 # in which case we can just deal with them
130 # in the code we eval.
131 if ($attr->is_default_a_coderef) {
132 $default = '$attrs->[' . $index . ']->default($instance)';
133 }
134 else {
135 $default = $attr->default;
136 # make sure to quote strings ...
137 unless (looks_like_number($default)) {
138 $default = "'$default'";
139 }
140 }
8d2d4c67 141 } elsif( $attr->has_builder ) {
142 $default = '$instance->'.$attr->builder;
d90b42a6 143 }
8d2d4c67 144
c16c9c1b 145 if ( defined $attr->init_arg ) {
146 return (
26ffbb36 147 'if(exists $params->{\'' . $attr->init_arg . '\'}){' . "\n" .
c16c9c1b 148 $self->meta_instance->inline_set_slot_value(
149 '$instance',
150 ("'" . $attr->name . "'"),
26ffbb36 151 '$params->{\'' . $attr->init_arg . '\'}' ) . "\n" .
c16c9c1b 152 '} ' . (!defined $default ? '' : 'else {' . "\n" .
153 $self->meta_instance->inline_set_slot_value(
154 '$instance',
155 ("'" . $attr->name . "'"),
156 $default ) . "\n" .
157 '}')
158 );
159 } elsif ( defined $default ) {
160 return (
161 $self->meta_instance->inline_set_slot_value(
162 '$instance',
163 ("'" . $attr->name . "'"),
164 $default ) . "\n"
165 );
166 } else { return '' }
d90b42a6 167}
168
1691;
170
d90b42a6 171__END__
172
173=pod
174
8d2d4c67 175=head1 NAME
d90b42a6 176
177Class::MOP::Method::Constructor - Method Meta Object for constructors
178
179=head1 SYNOPSIS
180
96e38ba6 181 use Class::MOP::Method::Constructor;
8d2d4c67 182
96e38ba6 183 my $constructor = Class::MOP::Method::Constructor->new(
8d2d4c67 184 metaclass => $metaclass,
96e38ba6 185 options => {
186 debug => 1, # this is all for now
8d2d4c67 187 },
96e38ba6 188 );
8d2d4c67 189
96e38ba6 190 # calling the constructor ...
191 $constructor->body->($metaclass->name, %params);
8d2d4c67 192
d90b42a6 193=head1 DESCRIPTION
194
8d2d4c67 195This is a subclass of C<Class::MOP::Method> which deals with
127d39a7 196class constructors. This is used when making a class immutable
197to generate an optimized constructor.
96e38ba6 198
d90b42a6 199=head1 METHODS
200
201=over 4
202
96e38ba6 203=item B<new (metaclass => $meta, options => \%options)>
d90b42a6 204
96e38ba6 205=item B<options>
206
207This returns the options HASH which is passed into C<new>.
208
209=item B<associated_metaclass>
210
211This returns the metaclass which is passed into C<new>.
c23184fc 212
d90b42a6 213=item B<attributes>
214
8d2d4c67 215This returns the list of attributes which are associated with the
96e38ba6 216metaclass which is passed into C<new>.
217
d90b42a6 218=item B<meta_instance>
219
8d2d4c67 220This returns the meta instance which is associated with the
96e38ba6 221metaclass which is passed into C<new>.
c23184fc 222
96e38ba6 223=item B<is_inline>
224
8d2d4c67 225This returns a boolean, but since constructors are very rarely
96e38ba6 226not inlined, this always returns true for now.
d90b42a6 227
565f0cbb 228=item B<initialize_body>
d90b42a6 229
8d2d4c67 230This creates the code reference for the constructor itself.
96e38ba6 231
d90b42a6 232=back
233
127d39a7 234=head2 Method Generators
565f0cbb 235
236=over 4
237
238=item B<generate_constructor_method>
239
240=item B<generate_constructor_method_inline>
241
242=back
243
d90b42a6 244=head1 AUTHORS
245
246Stevan Little E<lt>stevan@iinteractive.comE<gt>
247
248=head1 COPYRIGHT AND LICENSE
249
69e3ab0a 250Copyright 2006-2008 by Infinity Interactive, Inc.
d90b42a6 251
252L<http://www.iinteractive.com>
253
254This library is free software; you can redistribute it and/or modify
8d2d4c67 255it under the same terms as Perl itself.
d90b42a6 256
257=cut
258