Move type coercion mechanism from Util/TypeConstraints.pm to Meta/TypeConstraint.pm
[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 = sprintf("#line %d %s\n", __LINE__, __FILE__).<<"...";
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->{q{$from}}) {\n";
52
53             my $value = "\$args->{q{$from}}";
54             if(my $type_constraint = $attr->type_constraint){
55                 if($attr->should_coerce && $type_constraint->has_coercion){
56                     $code .= "my \$value = \$attrs[$index]->{type_constraint}->coerce(\$args->{q{$from}});\n";
57                     $value = '$value';
58                 }
59
60                 $code .= "\$compiled_constraints[$index]->($value)\n";
61                 $code .= "  or \$attrs[$index]->verify_type_constraint_error(q{$key}, $value, \$attrs[$index]->{type_constraint});\n";
62             }
63
64             $code .= "\$instance->{q{$key}} = $value;\n";
65
66             if ($attr->is_weak_ref) {
67                 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref($value);\n";
68             }
69
70             if ($attr->has_trigger) {
71                 $has_triggers++;
72                 $code .= "push \@triggers, [\$attrs[$index]->{trigger}, $value];\n";
73             }
74
75             $code .= "\n} else {\n";
76         }
77
78         if ($attr->has_default || $attr->has_builder) {
79             unless ($attr->is_lazy) {
80                 my $default = $attr->default;
81                 my $builder = $attr->builder;
82
83                 $code .= "my \$value = ";
84
85                 if ($attr->should_coerce && $attr->type_constraint) {
86                     $code .= "\$attrs[$index]->_coerce_and_verify(";
87                 }
88
89                 if ($attr->has_builder) {
90                     $code .= "\$instance->$builder()";
91                 }
92                 elsif (ref($default) eq 'CODE') {
93                     $code .= "\$attrs[$index]->{default}->(\$instance)";
94                 }
95                 elsif (!defined($default)) {
96                     $code .= 'undef';
97                 }
98                 elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
99                     $code .= $default;
100                 }
101                 else {
102                     $code .= "'$default'";
103                 }
104
105                 if ($attr->should_coerce) {
106                     $code .= ");\n";
107                 }
108                 else {
109                     $code .= ";\n";
110                 }
111
112                 if ($attr->has_type_constraint) {
113                     $code .= "{
114                         unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
115                             \$attrs[$index]->verify_type_constraint_error(q{$key}, \$value, \$attrs[$index]->type_constraint)
116                         }
117                     }";
118                 }
119
120                 $code .= "\$instance->{q{$key}} = \$value;\n";
121
122                 if ($attr->is_weak_ref) {
123                     $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
124                 }
125             }
126         }
127         elsif ($attr->is_required) {
128             $code .= "Carp::confess('Attribute ($key) is required');";
129         }
130
131         $code .= "}\n" if defined $attr->init_arg;
132
133         push @res, $code;
134     }
135
136     if($metaclass->is_anon_class){
137         push @res, q{$instnace->{__METACLASS__} = $metaclass;};
138     }
139
140     if($has_triggers){
141         unshift @res, q{my @triggers;};
142         push    @res,  q{$_->[0]->($instance, $_->[1]) for @triggers;};
143     }
144
145     return join "\n", @res;
146 }
147
148 sub _generate_BUILDARGS {
149     my($self, $metaclass) = @_;
150
151     if ($metaclass->name->can('BUILDARGS') && $metaclass->name->can('BUILDARGS') != \&Mouse::Object::BUILDARGS) {
152         return 'my $args = $class->BUILDARGS(@_)';
153     }
154
155     return <<'...';
156         my $args;
157         if ( scalar @_ == 1 ) {
158             ( ref( $_[0] ) eq 'HASH' )
159                 || Carp::confess "Single parameters to new() must be a HASH ref";
160             $args = +{ %{ $_[0] } };
161         }
162         else {
163             $args = +{@_};
164         }
165 ...
166 }
167
168 sub _generate_BUILDALL {
169     my ($class, $metaclass) = @_;
170
171     return '' unless $metaclass->name->can('BUILD');
172
173     my @code;
174     for my $class ($metaclass->linearized_isa) {
175         no strict 'refs';
176         no warnings 'once';
177
178         if (*{ $class . '::BUILD' }{CODE}) {
179             unshift  @code, qq{${class}::BUILD(\$instance, \$args);};
180         }
181     }
182     return join "\n", @code;
183 }
184
185 1;