Checking in changes prior to tagging of version 0.88.
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
1 package Mouse::Meta::Method::Constructor;
2 use Mouse::Util qw(:meta); # enables strict and warnings
3
4 use constant _MOUSE_DEBUG => $ENV{MOUSE_DEBUG} ? 1 : 0;
5
6 sub _inline_slot{
7     my(undef, $self_var, $attr_name) = @_;
8     return sprintf '%s->{q{%s}}', $self_var, $attr_name;
9 }
10
11 sub _generate_constructor {
12     my ($class, $metaclass, $args) = @_;
13
14     my $associated_metaclass_name = $metaclass->name;
15
16     my $buildall      = $class->_generate_BUILDALL($metaclass);
17     my $buildargs     = $class->_generate_BUILDARGS($metaclass);
18     my $initializer   = $metaclass->{_mouse_cache}{_initialize_object} ||=
19        $class->_generate_initialize_object($metaclass);
20     my $source = sprintf(<<'EOT', __FILE__, $metaclass->name, $buildargs, $buildall);
21 #line 1 "%s"
22         package %s;
23         sub {
24             my $class = shift;
25             return $class->Mouse::Object::new(@_)
26                 if $class ne __PACKAGE__;
27             # BUILDARGS
28             %s;
29             my $instance = bless {}, $class;
30             $metaclass->$initializer($instance, $args, 0);
31             # BUILDALL
32             %s;
33             return $instance;
34         }
35 EOT
36     warn $source if _MOUSE_DEBUG;
37     my $body;
38     my $e = do{
39         local $@;
40         $body = eval $source;
41         $@;
42     };
43     die $e if $e;
44     return $body;
45 }
46
47 sub _generate_initialize_object {
48     my ($method_class, $metaclass) = @_;
49     my @attrs  = $metaclass->get_all_attributes;
50
51     my @checks = map { $_ && $_->_compiled_type_constraint }
52                  map { $_->type_constraint } @attrs;
53
54     my @res;
55
56     my $has_triggers;
57     my $strict = $metaclass->strict_constructor;
58
59     if($strict){
60         push @res, 'my $used = 0;';
61     }
62
63     for my $index (0 .. @attrs - 1) {
64         my $code = '';
65
66         my $attr = $attrs[$index];
67         my $key  = $attr->name;
68
69         my $init_arg        = $attr->init_arg;
70         my $type_constraint = $attr->type_constraint;
71         my $is_weak_ref     = $attr->is_weak_ref;
72         my $need_coercion;
73
74         my $instance_slot  = $method_class->_inline_slot('$instance', $key);
75         my $attr_var       = "\$attrs[$index]";
76         my $constraint_var;
77
78         if(defined $type_constraint){
79              $constraint_var = "$attr_var\->{type_constraint}";
80              $need_coercion  = ($attr->should_coerce && $type_constraint->has_coercion);
81         }
82
83         $code .= "# initialize $key\n";
84
85         my $post_process = '';
86         if(defined $type_constraint){
87             $post_process .= "\$checks[$index]->($instance_slot)\n";
88             $post_process .= "  or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n";
89         }
90         if($is_weak_ref){
91             $post_process  = "Scalar::Util::weaken($instance_slot) "
92                              . "if ref $instance_slot;\n";
93         }
94
95         # build cde for an attribute
96         if (defined $init_arg) {
97             my $value = "\$args->{q{$init_arg}}";
98
99             $code .= "if (exists $value) {\n";
100
101             if($need_coercion){
102                 $value = "$constraint_var->coerce($value)";
103             }
104
105             $code .= "$instance_slot = $value;\n";
106             $code .= $post_process;
107
108             if ($attr->has_trigger) {
109                 $has_triggers++;
110                 $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n";
111             }
112
113             if ($strict){
114                 $code .= '++$used;' . "\n";
115             }
116
117             $code .= "\n} else {\n"; # $value exists
118         }
119
120         if ($attr->has_default || $attr->has_builder) {
121             unless ($attr->is_lazy) {
122                 my $default = $attr->default;
123                 my $builder = $attr->builder;
124
125                 my $value;
126                 if (defined($builder)) {
127                     $value = "\$instance->$builder()";
128                 }
129                 elsif (ref($default) eq 'CODE') {
130                     $value = "$attr_var\->{default}->(\$instance)";
131                 }
132                 elsif (defined($default)) {
133                     $value = "$attr_var\->{default}";
134                 }
135                 else {
136                     $value = 'undef';
137                 }
138
139                 if($need_coercion){
140                     $value = "$constraint_var->coerce($value)";
141                 }
142
143                 $code .= "$instance_slot = $value;\n";
144                 $code .= $post_process;
145             }
146         }
147         elsif ($attr->is_required) {
148             $code .= "\$meta->throw_error('Attribute ($key) is required')";
149             $code .= "    unless \$is_cloning;\n";
150         }
151
152         $code .= "}\n" if defined $init_arg;
153
154         push @res, $code;
155     }
156
157     if($strict){
158         push @res, q{if($used < keys %{$args})}
159             . q{{ $meta->_report_unknown_args(\@attrs, $args) }};
160     }
161
162     if($metaclass->is_anon_class){
163         push @res, q{$instance->{__METACLASS__} = $meta;};
164     }
165
166     if($has_triggers){
167         unshift @res, q{my @triggers;};
168         push    @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
169     }
170
171     my $source = sprintf <<'EOT', __FILE__, $metaclass->name, join "\n", @res;
172 #line 1 "%s"
173     package %s;
174     sub {
175         my($meta, $instance, $args, $is_cloning) = @_;
176         %s;
177         return $instance;
178     }
179 EOT
180     warn $source if _MOUSE_DEBUG;
181     my $body;
182     my $e = do {
183         local $@;
184         $body = eval $source;
185         $@;
186     };
187     die $e if $e;
188     return $body;
189 }
190
191 sub _generate_BUILDARGS {
192     my(undef, $metaclass) = @_;
193
194     my $class = $metaclass->name;
195     if ( $class->can('BUILDARGS') && $class->can('BUILDARGS') != \&Mouse::Object::BUILDARGS ) {
196         return 'my $args = $class->BUILDARGS(@_)';
197     }
198
199     return <<'...';
200         my $args;
201         if ( scalar @_ == 1 ) {
202             ( ref( $_[0] ) eq 'HASH' )
203                 || Carp::confess "Single parameters to new() must be a HASH ref";
204             $args = +{ %{ $_[0] } };
205         }
206         else {
207             $args = +{@_};
208         }
209 ...
210 }
211
212 sub _generate_BUILDALL {
213     my (undef, $metaclass) = @_;
214
215     return '' unless $metaclass->name->can('BUILD');
216
217     my @code;
218     for my $class ($metaclass->linearized_isa) {
219         if (Mouse::Util::get_code_ref($class, 'BUILD')) {
220             unshift  @code, qq{${class}::BUILD(\$instance, \$args);};
221         }
222     }
223     return join "\n", @code;
224 }
225
226 1;
227 __END__
228
229 =head1 NAME
230
231 Mouse::Meta::Method::Constructor - A Mouse method generator for constructors
232
233 =head1 VERSION
234
235 This document describes Mouse version 0.88
236
237 =head1 SEE ALSO
238
239 L<Moose::Meta::Method::Constructor>
240
241 =cut