Switch to Mouse::Exporter
[gitmo/Mouse.git] / lib / Mouse / Meta / Method / Constructor.pm
CommitLineData
fc1d8369 1package Mouse::Meta::Method::Constructor;
bc69ee88 2use Mouse::Util; # enables strict and warnings
fc1d8369 3
2a464664 4sub _generate_constructor_method {
5 my ($class, $metaclass, $args) = @_;
24ad3f66 6
2efc0af1 7 my $associated_metaclass_name = $metaclass->name;
7ca5c5fb 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
8215d725 14 my @compiled_constraints = map { $_ ? $_->_compiled_type_constraint : undef }
15 map { $_->type_constraint } @attrs;
fc1d8369 16
2a464664 17
ad087d11 18
19 my $source = sprintf("#line %d %s\n", __LINE__, __FILE__).<<"...";
20 sub \{
2a464664 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 }
fc1d8369 30...
24ad3f66 31
ad087d11 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;
fc1d8369 42}
43
44sub _generate_processattrs {
2efc0af1 45 my ($class, $metaclass, $attrs) = @_;
fc1d8369 46 my @res;
9df6f0cd 47
2efc0af1 48 my $has_triggers;
49
9df6f0cd 50 for my $index (0 .. @$attrs - 1) {
c12edd9a 51 my $attr = $attrs->[$index];
fc1d8369 52 my $key = $attr->name;
9df6f0cd 53 my $code = '';
54
55 if (defined $attr->init_arg) {
56 my $from = $attr->init_arg;
c91862e8 57
ffbbf459 58 $code .= "if (exists \$args->{q{$from}}) {\n";
c91862e8 59
ffbbf459 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 }
c91862e8 66
ffbbf459 67 $code .= "\$compiled_constraints[$index]->($value)\n";
68 $code .= " or \$attrs[$index]->verify_type_constraint_error(q{$key}, $value, \$attrs[$index]->{type_constraint});\n";
fc1d8369 69 }
c91862e8 70
ffbbf459 71 $code .= "\$instance->{q{$key}} = $value;\n";
c91862e8 72
41cdacce 73 if ($attr->is_weak_ref) {
ffbbf459 74 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref($value);\n";
41cdacce 75 }
c91862e8 76
9df6f0cd 77 if ($attr->has_trigger) {
2efc0af1 78 $has_triggers++;
ffbbf459 79 $code .= "push \@triggers, [\$attrs[$index]->{trigger}, $value];\n";
fc1d8369 80 }
c91862e8 81
7756897f 82 $code .= "\n} else {\n";
9df6f0cd 83 }
c91862e8 84
9df6f0cd 85 if ($attr->has_default || $attr->has_builder) {
86 unless ($attr->is_lazy) {
87 my $default = $attr->default;
88 my $builder = $attr->builder;
e8ba7b26 89
9df6f0cd 90 $code .= "my \$value = ";
e8ba7b26 91
9df6f0cd 92 if ($attr->should_coerce && $attr->type_constraint) {
ffbbf459 93 $code .= "\$attrs[$index]->_coerce_and_verify(";
9df6f0cd 94 }
e8ba7b26 95
ffbbf459 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 }
e8ba7b26 111
9df6f0cd 112 if ($attr->should_coerce) {
7756897f 113 $code .= ");\n";
9df6f0cd 114 }
115 else {
7756897f 116 $code .= ";\n";
9df6f0cd 117 }
e8ba7b26 118
9df6f0cd 119 if ($attr->has_type_constraint) {
7756897f 120 $code .= "{
684db121 121 unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
53c495ce 122 \$attrs[$index]->verify_type_constraint_error(q{$key}, \$value, \$attrs[$index]->type_constraint)
7756897f 123 }
124 }";
fc1d8369 125 }
9df6f0cd 126
53c495ce 127 $code .= "\$instance->{q{$key}} = \$value;\n";
9df6f0cd 128
129 if ($attr->is_weak_ref) {
53c495ce 130 $code .= "Scalar::Util::weaken( \$instance->{q{$key}} ) if ref( \$value );\n";
fc1d8369 131 }
132 }
713a2a05 133 }
9df6f0cd 134 elsif ($attr->is_required) {
7756897f 135 $code .= "Carp::confess('Attribute ($key) is required');";
9df6f0cd 136 }
137
7756897f 138 $code .= "}\n" if defined $attr->init_arg;
9df6f0cd 139
fc1d8369 140 push @res, $code;
141 }
9df6f0cd 142
2efc0af1 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;
fc1d8369 153}
154
155sub _generate_BUILDARGS {
2efc0af1 156 my($self, $metaclass) = @_;
9dcd7d23 157
7ca5c5fb 158 if ($metaclass->name->can('BUILDARGS') && $metaclass->name->can('BUILDARGS') != \&Mouse::Object::BUILDARGS) {
53d4053e 159 return 'my $args = $class->BUILDARGS(@_)';
9dcd7d23 160 }
161
162 return <<'...';
53d4053e 163 my $args;
fc1d8369 164 if ( scalar @_ == 1 ) {
c9aefe26 165 ( ref( $_[0] ) eq 'HASH' )
fc1d8369 166 || Carp::confess "Single parameters to new() must be a HASH ref";
53d4053e 167 $args = +{ %{ $_[0] } };
fc1d8369 168 }
169 else {
53d4053e 170 $args = +{@_};
fc1d8369 171 }
fc1d8369 172...
173}
174
175sub _generate_BUILDALL {
2efc0af1 176 my ($class, $metaclass) = @_;
7ca5c5fb 177
2efc0af1 178 return '' unless $metaclass->name->can('BUILD');
fc1d8369 179
7ca5c5fb 180 my @code;
181 for my $class ($metaclass->linearized_isa) {
182 no strict 'refs';
b898bac8 183 no warnings 'once';
7ca5c5fb 184
185 if (*{ $class . '::BUILD' }{CODE}) {
186 unshift @code, qq{${class}::BUILD(\$instance, \$args);};
fc1d8369 187 }
188 }
189 return join "\n", @code;
190}
191
1921;