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