Add a "no warnings 'once'"
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
1 package Mouse::Meta::Method::Constructor;
2 use strict;
3 use warnings;
4
5 sub generate_constructor_method_inline {
6     my ($class, $metaclass) = @_;
7
8     my $associated_metaclass_name = $metaclass->name;
9     my @attrs         = $metaclass->get_all_attributes;
10
11     my $buildall      = $class->_generate_BUILDALL($metaclass);
12     my $buildargs     = $class->_generate_BUILDARGS($metaclass);
13     my $processattrs  = $class->_generate_processattrs($metaclass, \@attrs);
14
15     my @compiled_constraints = map { $_ ? $_->{_compiled_type_constraint} : undef } map { $_->{type_constraint} } @attrs;
16
17     my $code = <<"...";
18     sub {
19         my \$class = shift;
20         return \$class->Mouse::Object::new(\@_)
21             if \$class ne q{$associated_metaclass_name};
22         $buildargs;
23         my \$instance = bless {}, \$class;
24         $processattrs;
25         $buildall;
26         return \$instance;
27     }
28 ...
29
30     local $@;
31     my $res = eval $code;
32     die $@ if $@;
33     $res;
34 }
35
36 sub _generate_processattrs {
37     my ($class, $metaclass, $attrs) = @_;
38     my @res;
39
40     my $has_triggers;
41
42     for my $index (0 .. @$attrs - 1) {
43         my $attr = $attrs->[$index];
44         my $key  = $attr->name;
45         my $code = '';
46
47         if (defined $attr->init_arg) {
48             my $from = $attr->init_arg;
49
50             $code .= "if (exists \$args->{'$from'}) {\n";
51
52             if ($attr->should_coerce && $attr->type_constraint) {
53                 $code .= "my \$value = Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n";
54             }
55             else {
56                 $code .= "my \$value = \$args->{'$from'};\n";
57             }
58
59             if ($attr->has_type_constraint) {
60                 if ($attr->type_constraint->{_compiled_type_constraint}) {
61                     $code .= "unless (\$compiled_constraints[$index](\$value)) {";
62                 } else {
63                     $code .= "unless (\$attrs[$index]->{type_constraint}->check(\$value)) {";
64                 }
65                 $code .= "
66                         \$attrs[$index]->verify_type_constraint_error(
67                             q{$key}, \$value, \$attrs[$index]->type_constraint
68                         )
69                     }
70                 ";
71             }
72
73             $code .= "\$instance->{q{$key}} = \$value;\n";
74
75             if ($attr->is_weak_ref) {
76                 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
77             }
78
79             if ($attr->has_trigger) {
80                 $has_triggers++;
81                 $code .= "push \@triggers, [\$attrs[$index]->{trigger}, \$value];\n";
82             }
83
84             $code .= "\n} else {\n";
85         }
86
87         if ($attr->has_default || $attr->has_builder) {
88             unless ($attr->is_lazy) {
89                 my $default = $attr->default;
90                 my $builder = $attr->builder;
91
92                 $code .= "my \$value = ";
93
94                 if ($attr->should_coerce && $attr->type_constraint) {
95                     $code .= "Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, ";
96                 }
97
98                     if ($attr->has_builder) {
99                         $code .= "\$instance->$builder";
100                     }
101                     elsif (ref($default) eq 'CODE') {
102                         $code .= "\$attrs[$index]->{default}->(\$instance)";
103                     }
104                     elsif (!defined($default)) {
105                         $code .= 'undef';
106                     }
107                     elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
108                         $code .= $default;
109                     }
110                     else {
111                         $code .= "'$default'";
112                     }
113
114                 if ($attr->should_coerce) {
115                     $code .= ");\n";
116                 }
117                 else {
118                     $code .= ";\n";
119                 }
120
121                 if ($attr->has_type_constraint) {
122                     $code .= "{
123                         unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
124                             \$attrs[$index]->verify_type_constraint_error(q{$key}, \$value, \$attrs[$index]->type_constraint)
125                         }
126                     }";
127                 }
128
129                 $code .= "\$instance->{q{$key}} = \$value;\n";
130
131                 if ($attr->is_weak_ref) {
132                     $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
133                 }
134             }
135         }
136         elsif ($attr->is_required) {
137             $code .= "Carp::confess('Attribute ($key) is required');";
138         }
139
140         $code .= "}\n" if defined $attr->init_arg;
141
142         push @res, $code;
143     }
144
145     if($metaclass->is_anon_class){
146         push @res, q{$instnace->{__METACLASS__} = $metaclass;};
147     }
148
149     if($has_triggers){
150         unshift @res, q{my @triggers;};
151         push    @res,  q{$_->[0]->($instance, $_->[1]) for @triggers;};
152     }
153
154     return join "\n", @res;
155 }
156
157 sub _generate_BUILDARGS {
158     my($self, $metaclass) = @_;
159
160     if ($metaclass->name->can('BUILDARGS') && $metaclass->name->can('BUILDARGS') != \&Mouse::Object::BUILDARGS) {
161         return 'my $args = $class->BUILDARGS(@_)';
162     }
163
164     return <<'...';
165         my $args;
166         if ( scalar @_ == 1 ) {
167             ( ref( $_[0] ) eq 'HASH' )
168                 || Carp::confess "Single parameters to new() must be a HASH ref";
169             $args = +{ %{ $_[0] } };
170         }
171         else {
172             $args = +{@_};
173         }
174 ...
175 }
176
177 sub _generate_BUILDALL {
178     my ($class, $metaclass) = @_;
179
180     return '' unless $metaclass->name->can('BUILD');
181
182     my @code;
183     for my $class ($metaclass->linearized_isa) {
184         no strict 'refs';
185         no warnings 'once';
186
187         if (*{ $class . '::BUILD' }{CODE}) {
188             unshift  @code, qq{${class}::BUILD(\$instance, \$args);};
189         }
190     }
191     return join "\n", @code;
192 }
193
194 1;