Fix the timing triggers are invoked
[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, $meta) = @_;
7
8     my $associated_metaclass_name = $meta->name;
9     my @attrs = $meta->get_all_attributes;
10     my $buildall = $class->_generate_BUILDALL($meta);
11     my $buildargs = $class->_generate_BUILDARGS($meta);
12     my $processattrs = $class->_generate_processattrs($meta, \@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, $meta, $attrs) = @_;
37     my @res;
38
39     for my $index (0 .. @$attrs - 1) {
40         my $attr = $attrs->[$index];
41         my $key  = $attr->name;
42         my $code = '';
43
44         if (defined $attr->init_arg) {
45             my $from = $attr->init_arg;
46
47             $code .= "if (exists \$args->{'$from'}) {\n";
48
49             if ($attr->should_coerce && $attr->type_constraint) {
50                 $code .= "my \$value = Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n";
51             }
52             else {
53                 $code .= "my \$value = \$args->{'$from'};\n";
54             }
55
56             if ($attr->has_type_constraint) {
57                 if ($attr->type_constraint->{_compiled_type_constraint}) {
58                     $code .= "unless (\$compiled_constraints[$index](\$value)) {";
59                 } else {
60                     $code .= "unless (\$attrs[$index]->{type_constraint}->check(\$value)) {";
61                 }
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                 $code .= "push \@triggers, [\$attrs[$index]->{trigger}, \$value];\n";
78             }
79
80             $code .= "\n} else {\n";
81         }
82
83         if ($attr->has_default || $attr->has_builder) {
84             unless ($attr->is_lazy) {
85                 my $default = $attr->default;
86                 my $builder = $attr->builder;
87
88                 $code .= "my \$value = ";
89
90                 if ($attr->should_coerce && $attr->type_constraint) {
91                     $code .= "Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, ";
92                 }
93
94                     if ($attr->has_builder) {
95                         $code .= "\$instance->$builder";
96                     }
97                     elsif (ref($default) eq 'CODE') {
98                         $code .= "\$attrs[$index]->{default}->(\$instance)";
99                     }
100                     elsif (!defined($default)) {
101                         $code .= 'undef';
102                     }
103                     elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
104                         $code .= $default;
105                     }
106                     else {
107                         $code .= "'$default'";
108                     }
109
110                 if ($attr->should_coerce) {
111                     $code .= ");\n";
112                 }
113                 else {
114                     $code .= ";\n";
115                 }
116
117                 if ($attr->has_type_constraint) {
118                     $code .= "{
119                         unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
120                             \$attrs[$index]->verify_type_constraint_error(q{$key}, \$value, \$attrs[$index]->type_constraint)
121                         }
122                     }";
123                 }
124
125                 $code .= "\$instance->{q{$key}} = \$value;\n";
126
127                 if ($attr->is_weak_ref) {
128                     $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
129                 }
130             }
131         }
132         elsif ($attr->is_required) {
133             $code .= "Carp::confess('Attribute ($key) is required');";
134         }
135
136         $code .= "}\n" if defined $attr->init_arg;
137
138         push @res, $code;
139     }
140
141     return join "\n", q{my @triggers;}, @res, q{$_->[0]->($instance, $_->[1]) for @triggers;};
142 }
143
144 sub _generate_BUILDARGS {
145     my $self = shift;
146     my $meta = shift;
147
148     if ($meta->name->can('BUILDARGS') && $meta->name->can('BUILDARGS') != Mouse::Object->can('BUILDARGS')) {
149         return 'my $args = $class->BUILDARGS(@_)';
150     }
151
152     return <<'...';
153         my $args;
154         if ( scalar @_ == 1 ) {
155             ( ref( $_[0] ) eq 'HASH' )
156                 || Carp::confess "Single parameters to new() must be a HASH ref";
157             $args = +{ %{ $_[0] } };
158         }
159         else {
160             $args = +{@_};
161         }
162 ...
163 }
164
165 sub _generate_BUILDALL {
166     my ($class, $meta) = @_;
167     return '' unless $meta->name->can('BUILD');
168
169     my @code = ();
170     push @code, q{no strict 'refs';};
171     push @code, q{no warnings 'once';};
172     no strict 'refs';
173     no warnings 'once';
174     for my $klass ($meta->linearized_isa) {
175         if (*{ $klass . '::BUILD' }{CODE}) {
176             unshift  @code, qq{${klass}::BUILD(\$instance, \$args);};
177         }
178     }
179     return join "\n", @code;
180 }
181
182 1;