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