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