1 package Mouse::Meta::Method::Constructor;
5 sub generate_constructor_method_inline {
6 my ($class, $meta) = @_;
8 my @attrs = $meta->compute_all_applicable_attributes;
9 my $buildall = $class->_generate_BUILDALL($meta);
10 my $buildargs = $class->_generate_BUILDARGS($meta);
11 my $processattrs = $class->_generate_processattrs($meta, \@attrs);
16 my \$args = $buildargs;
17 my \$instance = bless {}, \$class;
30 sub _generate_processattrs {
31 my ($class, $meta, $attrs) = @_;
34 for my $index (0 .. @$attrs - 1) {
35 my $attr = $attrs->[$index];
36 my $key = $attr->name;
39 if (defined $attr->init_arg) {
40 my $from = $attr->init_arg;
42 $code .= "if (exists \$args->{'$from'}) {\n";
44 if ($attr->should_coerce && $attr->type_constraint) {
45 $code .= "my \$value = Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{find_type_constraint}, \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n";
48 $code .= "my \$value = \$args->{'$from'};\n";
51 if ($attr->has_type_constraint) {
54 unless (\$attrs[$index]->{find_type_constraint}->(\$_)) {
55 \$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint)
60 $code .= "\$instance->{'$key'} = \$value;\n";
62 if ($attr->is_weak_ref) {
63 $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n";
66 if ($attr->has_trigger) {
67 $code .= "\$attrs[$index]->{trigger}->( \$instance, \$value, \$attrs[$index] );\n";
70 $code .= "\n} else {\n";
73 if ($attr->has_default || $attr->has_builder) {
74 unless ($attr->is_lazy) {
75 my $default = $attr->default;
76 my $builder = $attr->builder;
78 $code .= "my \$value = ";
80 if ($attr->should_coerce && $attr->type_constraint) {
81 $code .= "Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{find_type_constraint}, \$attrs[$index]->{type_constraint}, ";
84 if ($attr->has_builder) {
85 $code .= "\$instance->$builder";
87 elsif (ref($default) eq 'CODE') {
88 $code .= "\$attrs[$index]->{default}->(\$instance)";
90 elsif (!defined($default)) {
93 elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
97 $code .= "'$default'";
100 if ($attr->should_coerce) {
107 if ($attr->has_type_constraint) {
110 unless (\$attrs[$index]->{find_type_constraint}->(\$_)) {
111 \$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint)
116 $code .= "\$instance->{'$key'} = \$value;\n";
118 if ($attr->is_weak_ref) {
119 $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n";
123 elsif ($attr->is_required) {
124 $code .= "Carp::confess('Attribute ($key) is required');";
127 $code .= "}\n" if defined $attr->init_arg;
132 return join "\n", @res;
135 sub _generate_BUILDARGS {
139 if ($meta->name->can('BUILDARGS') != Mouse::Object->can('BUILDARGS')) {
140 return '$class->BUILDARGS(@_)';
145 if ( scalar @_ == 1 ) {
146 if ( defined $_[0] ) {
147 ( ref( $_[0] ) eq 'HASH' )
148 || Carp::confess "Single parameters to new() must be a HASH ref";
162 sub _generate_BUILDALL {
163 my ($class, $meta) = @_;
164 return '' unless $meta->name->can('BUILD');
167 push @code, q{no strict 'refs';};
168 push @code, q{no warnings 'once';};
171 for my $klass ($meta->linearized_isa) {
172 if (*{ $klass . '::BUILD' }{CODE}) {
173 push @code, qq{${klass}::BUILD(\$instance, \$args);};
176 return join "\n", @code;