Fix compatibility tests
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
CommitLineData
fc1d8369 1package Mouse::Meta::Method::Constructor;
a5c683f6 2use Mouse::Util; # enables strict and warnings
fc1d8369 3
f8cbb121 4sub _inline_slot{
5 my(undef, $self_var, $attr_name) = @_;
6 return sprintf '%s->{q{%s}}', $self_var, $attr_name;
7}
8
380e1cd7 9sub _generate_constructor {
2a464664 10 my ($class, $metaclass, $args) = @_;
24ad3f66 11
2efc0af1 12 my $associated_metaclass_name = $metaclass->name;
0bfc7290 13
7ca5c5fb 14 my @attrs = $metaclass->get_all_attributes;
15
16 my $buildall = $class->_generate_BUILDALL($metaclass);
17 my $buildargs = $class->_generate_BUILDARGS($metaclass);
18 my $processattrs = $class->_generate_processattrs($metaclass, \@attrs);
19
0bfc7290 20 my @checks = map { $_ && $_->_compiled_type_constraint }
21 map { $_->type_constraint } @attrs;
ad087d11 22
23 my $source = sprintf("#line %d %s\n", __LINE__, __FILE__).<<"...";
24 sub \{
2a464664 25 my \$class = shift;
26 return \$class->Mouse::Object::new(\@_)
27 if \$class ne q{$associated_metaclass_name};
0bfc7290 28 # BUILDARGS
2a464664 29 $buildargs;
30 my \$instance = bless {}, \$class;
0bfc7290 31 # process attributes
2a464664 32 $processattrs;
0bfc7290 33 # BUILDALL
2a464664 34 $buildall;
35 return \$instance;
36 }
fc1d8369 37...
0bfc7290 38 #warn $source;
ad087d11 39 my $code;
40 my $e = do{
41 local $@;
42 $code = eval $source;
43 $@;
44 };
45 die $e if $e;
380e1cd7 46 return $code;
fc1d8369 47}
48
49sub _generate_processattrs {
f8cbb121 50 my ($method_class, $metaclass, $attrs) = @_;
fc1d8369 51 my @res;
9df6f0cd 52
2efc0af1 53 my $has_triggers;
54
9df6f0cd 55 for my $index (0 .. @$attrs - 1) {
0bfc7290 56 my $code = '';
57
c12edd9a 58 my $attr = $attrs->[$index];
fc1d8369 59 my $key = $attr->name;
9df6f0cd 60
0bfc7290 61 my $init_arg = $attr->init_arg;
62 my $type_constraint = $attr->type_constraint;
63 my $need_coercion;
c91862e8 64
f8cbb121 65 my $instance_slot = $method_class->_inline_slot('$instance', $key);
0bfc7290 66 my $attr_var = "\$attrs[$index]";
67 my $constraint_var;
c91862e8 68
0bfc7290 69 if(defined $type_constraint){
70 $constraint_var = "$attr_var\->{type_constraint}";
71 $need_coercion = ($attr->should_coerce && $type_constraint->has_coercion);
72 }
c91862e8 73
0bfc7290 74 $code .= "# initialize $key\n";
75
76 my $post_process = '';
77 if(defined $type_constraint){
78 $post_process .= "\$checks[$index]->($instance_slot)";
79 $post_process .= " or $attr_var->verify_type_constraint_error(q{$key}, $instance_slot, $constraint_var);\n";
80 }
81 if($attr->is_weak_ref){
82 $post_process .= "Scalar::Util::weaken($instance_slot) if ref $instance_slot;\n";
83 }
c91862e8 84
0bfc7290 85 if (defined $init_arg) {
86 my $value = "\$args->{q{$init_arg}}";
c91862e8 87
0bfc7290 88 $code .= "if (exists $value) {\n";
89
90 if($need_coercion){
620c3203 91 $value = "$constraint_var->coerce($value)";
41cdacce 92 }
c91862e8 93
0bfc7290 94 $code .= "$instance_slot = $value;\n";
95 $code .= $post_process;
96
9df6f0cd 97 if ($attr->has_trigger) {
2efc0af1 98 $has_triggers++;
620c3203 99 $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n";
fc1d8369 100 }
c91862e8 101
7756897f 102 $code .= "\n} else {\n";
9df6f0cd 103 }
c91862e8 104
9df6f0cd 105 if ($attr->has_default || $attr->has_builder) {
106 unless ($attr->is_lazy) {
107 my $default = $attr->default;
108 my $builder = $attr->builder;
e8ba7b26 109
0bfc7290 110 my $value;
111 if (defined($builder)) {
112 $value = "\$instance->$builder()";
ffbbf459 113 }
114 elsif (ref($default) eq 'CODE') {
0bfc7290 115 $value = "$attr_var\->{default}->(\$instance)";
ffbbf459 116 }
0bfc7290 117 elsif (defined($default)) {
118 $value = "$attr_var\->{default}";
ffbbf459 119 }
120 else {
0bfc7290 121 $value = 'undef';
ffbbf459 122 }
e8ba7b26 123
0bfc7290 124 if($need_coercion){
125 $value = "$constraint_var->coerce($value)";
9df6f0cd 126 }
9df6f0cd 127
0bfc7290 128 $code .= "$instance_slot = $value;\n";
fc1d8369 129 }
713a2a05 130 }
9df6f0cd 131 elsif ($attr->is_required) {
7756897f 132 $code .= "Carp::confess('Attribute ($key) is required');";
9df6f0cd 133 }
134
0bfc7290 135 $code .= "}\n" if defined $init_arg;
9df6f0cd 136
fc1d8369 137 push @res, $code;
138 }
9df6f0cd 139
2efc0af1 140 if($metaclass->is_anon_class){
a8391b11 141 push @res, q{$instance->{__METACLASS__} = $metaclass;};
2efc0af1 142 }
143
144 if($has_triggers){
145 unshift @res, q{my @triggers;};
146 push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
147 }
148
149 return join "\n", @res;
fc1d8369 150}
151
152sub _generate_BUILDARGS {
0bfc7290 153 my(undef, $metaclass) = @_;
9dcd7d23 154
0bfc7290 155 my $class = $metaclass->name;
156 if ( $class->can('BUILDARGS') && $class->can('BUILDARGS') != \&Mouse::Object::BUILDARGS ) {
53d4053e 157 return 'my $args = $class->BUILDARGS(@_)';
9dcd7d23 158 }
159
160 return <<'...';
53d4053e 161 my $args;
fc1d8369 162 if ( scalar @_ == 1 ) {
c9aefe26 163 ( ref( $_[0] ) eq 'HASH' )
fc1d8369 164 || Carp::confess "Single parameters to new() must be a HASH ref";
53d4053e 165 $args = +{ %{ $_[0] } };
fc1d8369 166 }
167 else {
53d4053e 168 $args = +{@_};
fc1d8369 169 }
fc1d8369 170...
171}
172
173sub _generate_BUILDALL {
0bfc7290 174 my (undef, $metaclass) = @_;
7ca5c5fb 175
2efc0af1 176 return '' unless $metaclass->name->can('BUILD');
fc1d8369 177
7ca5c5fb 178 my @code;
179 for my $class ($metaclass->linearized_isa) {
a5c683f6 180 if (Mouse::Util::get_code_ref($class, 'BUILD')) {
7ca5c5fb 181 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
fc1d8369 182 }
183 }
184 return join "\n", @code;
185}
186
1871;
0126c27c 188__END__
a25ca8d6 189
190=head1 NAME
191
192Mouse::Meta::Method::Constructor - A Mouse method generator for constructors
193
194=head1 VERSION
195
1e582397 196This document describes Mouse version 0.40_06
a25ca8d6 197
198=head1 SEE ALSO
199
200L<Moose::Meta::Method::Constructor>
201
202=cut