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]->{type_constraint}, \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n";
48 $code .= "my \$value = \$args->{'$from'};\n";
51 if ($attr->has_type_constraint) {
53 unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
54 \$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint)
59 $code .= "\$instance->{'$key'} = \$value;\n";
61 if ($attr->is_weak_ref) {
62 $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n";
65 if ($attr->has_trigger) {
66 $code .= "\$attrs[$index]->{trigger}->( \$instance, \$value );\n";
69 $code .= "\n} else {\n";
72 if ($attr->has_default || $attr->has_builder) {
73 unless ($attr->is_lazy) {
74 my $default = $attr->default;
75 my $builder = $attr->builder;
77 $code .= "my \$value = ";
79 if ($attr->should_coerce && $attr->type_constraint) {
80 $code .= "Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{type_constraint}, \$attrs[$index]->{type_constraint}, ";
83 if ($attr->has_builder) {
84 $code .= "\$instance->$builder";
86 elsif (ref($default) eq 'CODE') {
87 $code .= "\$attrs[$index]->{default}->(\$instance)";
89 elsif (!defined($default)) {
92 elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
96 $code .= "'$default'";
99 if ($attr->should_coerce) {
106 if ($attr->has_type_constraint) {
108 unless (\$attrs[$index]->{type_constraint}->check(\$value)) {
109 \$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint)
114 $code .= "\$instance->{'$key'} = \$value;\n";
116 if ($attr->is_weak_ref) {
117 $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n";
121 elsif ($attr->is_required) {
122 $code .= "Carp::confess('Attribute ($key) is required');";
125 $code .= "}\n" if defined $attr->init_arg;
130 return join "\n", @res;
133 sub _generate_BUILDARGS {
137 if ($meta->name->can('BUILDARGS') && $meta->name->can('BUILDARGS') != Mouse::Object->can('BUILDARGS')) {
138 return '$class->BUILDARGS(@_)';
143 if ( scalar @_ == 1 ) {
144 ( ref( $_[0] ) eq 'HASH' )
145 || Carp::confess "Single parameters to new() must be a HASH ref";
155 sub _generate_BUILDALL {
156 my ($class, $meta) = @_;
157 return '' unless $meta->name->can('BUILD');
160 push @code, q{no strict 'refs';};
161 push @code, q{no warnings 'once';};
164 for my $klass ($meta->linearized_isa) {
165 if (*{ $klass . '::BUILD' }{CODE}) {
166 push @code, qq{${klass}::BUILD(\$instance, \$args);};
169 return join "\n", @code;