my $attr = $attrs->[$index];
my $from = $attr->init_arg;
my $key = $attr->name;
- my $part1 = do {
+
+ my $set_value = do {
my @code;
- if ($attr->should_coerce) {
- push @code, "\$args->{'$from'} = \$attr->coerce_constraint( \$args->{'$from'} );";
+
+ if ($attr->should_coerce && $attr->type_constraint) {
+ push @code, "my \$value = Mouse::TypeRegistry->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{find_type_constraint}, \$attrs[$index]->{type_constraint}, \$args->{'$from'});";
+ }
+ else {
+ push @code, "my \$value = \$args->{'$from'};";
}
+
if ($attr->has_type_constraint) {
- push @code, "\$attr->verify_type_constraint( \$args->{'$from'} );";
+ push @code, "{local \$_ = \$value; unless (\$attrs[$index]->{find_type_constraint}->(\$_)) {";
+ push @code, "\$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint)}}";
}
- push @code, "\$instance->{'$key'} = \$args->{'$from'};";
+
+ push @code, "\$instance->{'$key'} = \$value;";
+
if ($attr->is_weak_ref) {
- push @code, "weaken( \$instance->{'$key'} ) if ref( \$instance->{'$key'} );";
+ push @code, "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );";
}
+
if ( $attr->has_trigger ) {
- push @code, "\$attr->trigger->( \$instance, \$args->{'$from'}, \$attr );";
+ push @code, "\$attrs[$index]->{trigger}->( \$instance, \$value, \$attrs[$index] );";
}
+
join "\n", @code;
};
- my $part2 = do {
+
+ my $make_default_value = do {
my @code;
+
if ( $attr->has_default || $attr->has_builder ) {
unless ( $attr->is_lazy ) {
my $default = $attr->default;
my $builder = $attr->builder;
- if ($attr->has_builder) {
- push @code, "my \$value = \$instance->$builder;";
- } elsif (ref($default) eq 'CODE') {
- push @code, "my \$value = \$attr->default()->();";
- } else {
- push @code, "my \$value = \$attr->default();";
+
+ push @code, "my \$value = ";
+
+ if ($attr->should_coerce && $attr->type_constraint) {
+ push @code, "Mouse::TypeRegistry->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{find_type_constraint}, \$attrs[$index]->{type_constraint}, ";
}
+ if ($attr->has_builder) {
+ push @code, "\$instance->$builder";
+ }
+ elsif (ref($default) eq 'CODE') {
+ push @code, "\$attrs[$index]->{default}->(\$instance)";
+ }
+ elsif (!defined($default)) {
+ push @code, 'undef';
+ }
+ elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) {
+ push @code, $default;
+ }
+ else {
+ push @code, "'$default'";
+ }
+
if ($attr->should_coerce) {
- push @code, "\$value = \$attr->coerce_constraint(\$value);";
+ push @code, ");";
}
+ else {
+ push @code, ";";
+ }
+
if ($attr->has_type_constraint) {
- push @code, "\$attr->verify_type_constraint(\$value);";
+ push @code, "{local \$_ = \$value; unless (\$attrs[$index]->{find_type_constraint}->(\$_)) {";
+ push @code, "\$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint)}}";
}
+
push @code, "\$instance->{'$key'} = \$value;";
+
if ($attr->is_weak_ref) {
- push @code, "weaken( \$instance->{'$key'} ) if ref( \$instance->{'$key'} );";
+ push @code, "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );";
}
}
join "\n", @code;
};
my $code = <<"...";
{
- my \$attr = \$attrs[$index];
if (exists(\$args->{'$from'})) {
- $part1;
+ $set_value;
+...
+ if ($make_default_value) {
+ $code .= <<"...";
} else {
- $part2;
+ $make_default_value;
+...
+ }
+ $code .= <<"...";
}
}
...
push @code, q{no strict 'refs';};
push @code, q{no warnings 'once';};
no strict 'refs';
- for my $class ($meta->linearized_isa) {
- if (*{ $class . '::BUILD' }{CODE}) {
- push @code, qq{${class}::BUILD->(\$instance, \$args);};
+ for my $klass ($meta->linearized_isa) {
+ if (*{ $klass . '::BUILD' }{CODE}) {
+ push @code, qq{${klass}::BUILD(\$instance, \$args);};
}
}
return join "\n", @code;