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