Move type coercion mechanism from Util/TypeConstraints.pm to Meta/TypeConstraint.pm
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
CommitLineData
fc1d8369 1package Mouse::Meta::Method::Constructor;
2use strict;
3use warnings;
4
5sub generate_constructor_method_inline {
2efc0af1 6 my ($class, $metaclass) = @_;
24ad3f66 7
2efc0af1 8 my $associated_metaclass_name = $metaclass->name;
7ca5c5fb 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
3b89ea91 15 my @compiled_constraints = map { $_->_compiled_type_constraint }
16 map { $_->{type_constraint} ? $_->{type_constraint} : () } @attrs;
fc1d8369 17
ffbbf459 18 my $code = sprintf("#line %d %s\n", __LINE__, __FILE__).<<"...";
fc1d8369 19 sub {
20 my \$class = shift;
4e145304 21 return \$class->Mouse::Object::new(\@_)
7ca5c5fb 22 if \$class ne q{$associated_metaclass_name};
53d4053e 23 $buildargs;
24ad3f66 24 my \$instance = bless {}, \$class;
fc1d8369 25 $processattrs;
26 $buildall;
27 return \$instance;
28 }
29...
24ad3f66 30
24ad3f66 31 local $@;
32 my $res = eval $code;
33 die $@ if $@;
34 $res;
fc1d8369 35}
36
37sub _generate_processattrs {
2efc0af1 38 my ($class, $metaclass, $attrs) = @_;
fc1d8369 39 my @res;
9df6f0cd 40
2efc0af1 41 my $has_triggers;
42
9df6f0cd 43 for my $index (0 .. @$attrs - 1) {
c12edd9a 44 my $attr = $attrs->[$index];
fc1d8369 45 my $key = $attr->name;
9df6f0cd 46 my $code = '';
47
48 if (defined $attr->init_arg) {
49 my $from = $attr->init_arg;
c91862e8 50
ffbbf459 51 $code .= "if (exists \$args->{q{$from}}) {\n";
c91862e8 52
ffbbf459 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 }
c91862e8 59
ffbbf459 60 $code .= "\$compiled_constraints[$index]->($value)\n";
61 $code .= " or \$attrs[$index]->verify_type_constraint_error(q{$key}, $value, \$attrs[$index]->{type_constraint});\n";
fc1d8369 62 }
c91862e8 63
ffbbf459 64 $code .= "\$instance->{q{$key}} = $value;\n";
c91862e8 65
41cdacce 66 if ($attr->is_weak_ref) {
ffbbf459 67 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref($value);\n";
41cdacce 68 }
c91862e8 69
9df6f0cd 70 if ($attr->has_trigger) {
2efc0af1 71 $has_triggers++;
ffbbf459 72 $code .= "push \@triggers, [\$attrs[$index]->{trigger}, $value];\n";
fc1d8369 73 }
c91862e8 74
7756897f 75 $code .= "\n} else {\n";
9df6f0cd 76 }
c91862e8 77
9df6f0cd 78 if ($attr->has_default || $attr->has_builder) {
79 unless ($attr->is_lazy) {
80 my $default = $attr->default;
81 my $builder = $attr->builder;
e8ba7b26 82
9df6f0cd 83 $code .= "my \$value = ";
e8ba7b26 84
9df6f0cd 85 if ($attr->should_coerce && $attr->type_constraint) {
ffbbf459 86 $code .= "\$attrs[$index]->_coerce_and_verify(";
9df6f0cd 87 }
e8ba7b26 88
ffbbf459 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 }
e8ba7b26 104
9df6f0cd 105 if ($attr->should_coerce) {
7756897f 106 $code .= ");\n";
9df6f0cd 107 }
108 else {
7756897f 109 $code .= ";\n";
9df6f0cd 110 }
e8ba7b26 111
9df6f0cd 112 if ($attr->has_type_constraint) {
7756897f 113 $code .= "{
684db121 114 unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
53c495ce 115 \$attrs[$index]->verify_type_constraint_error(q{$key}, \$value, \$attrs[$index]->type_constraint)
7756897f 116 }
117 }";
fc1d8369 118 }
9df6f0cd 119
53c495ce 120 $code .= "\$instance->{q{$key}} = \$value;\n";
9df6f0cd 121
122 if ($attr->is_weak_ref) {
53c495ce 123 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
fc1d8369 124 }
125 }
713a2a05 126 }
9df6f0cd 127 elsif ($attr->is_required) {
7756897f 128 $code .= "Carp::confess('Attribute ($key) is required');";
9df6f0cd 129 }
130
7756897f 131 $code .= "}\n" if defined $attr->init_arg;
9df6f0cd 132
fc1d8369 133 push @res, $code;
134 }
9df6f0cd 135
2efc0af1 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;
fc1d8369 146}
147
148sub _generate_BUILDARGS {
2efc0af1 149 my($self, $metaclass) = @_;
9dcd7d23 150
7ca5c5fb 151 if ($metaclass->name->can('BUILDARGS') && $metaclass->name->can('BUILDARGS') != \&Mouse::Object::BUILDARGS) {
53d4053e 152 return 'my $args = $class->BUILDARGS(@_)';
9dcd7d23 153 }
154
155 return <<'...';
53d4053e 156 my $args;
fc1d8369 157 if ( scalar @_ == 1 ) {
c9aefe26 158 ( ref( $_[0] ) eq 'HASH' )
fc1d8369 159 || Carp::confess "Single parameters to new() must be a HASH ref";
53d4053e 160 $args = +{ %{ $_[0] } };
fc1d8369 161 }
162 else {
53d4053e 163 $args = +{@_};
fc1d8369 164 }
fc1d8369 165...
166}
167
168sub _generate_BUILDALL {
2efc0af1 169 my ($class, $metaclass) = @_;
7ca5c5fb 170
2efc0af1 171 return '' unless $metaclass->name->can('BUILD');
fc1d8369 172
7ca5c5fb 173 my @code;
174 for my $class ($metaclass->linearized_isa) {
175 no strict 'refs';
b898bac8 176 no warnings 'once';
7ca5c5fb 177
178 if (*{ $class . '::BUILD' }{CODE}) {
179 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
fc1d8369 180 }
181 }
182 return join "\n", @code;
183}
184
1851;