X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FMethod%2FConstructor.pm;h=7568dcdbb773941bdafb61ed8e30814f78fa3b9b;hb=6c169c5063b77a791818f5db2c1da3bd9b47d3f9;hp=c0cf03e9c43f3e094d552a5c0dc6725a7096eeb0;hpb=e8ba7b267968d4ca0e94d03276ca4c33fed40482;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index c0cf03e..7568dcd 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -37,17 +37,19 @@ sub _generate_processattrs { my $from = $attr->init_arg; my $key = $attr->name; - my $part1 = do { + my $set_value = do { my @code; - push @code, "my \$value = \$args->{'$from'};"; - - if ($attr->should_coerce) { - push @code, "\$value = \$attr->coerce_constraint( \$value );"; + 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( \$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;"; @@ -57,13 +59,13 @@ sub _generate_processattrs { } if ( $attr->has_trigger ) { - push @code, "\$attr->trigger->( \$instance, \$value, \$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 ) { @@ -73,18 +75,23 @@ sub _generate_processattrs { push @code, "my \$value = "; - if ($attr->should_coerce) { - push @code, "\$attr->coerce_constraint("; + 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, "\$attr->default()->()"; + push @code, "\$attrs[$index]->{default}->(\$instance)"; + } + elsif (!defined($default)) { + push @code, 'undef'; + } + elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) { + push @code, $default; } else { - push @code, "\$attr->default()"; + push @code, "'$default'"; } if ($attr->should_coerce) { @@ -95,7 +102,8 @@ sub _generate_processattrs { } 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;"; @@ -116,11 +124,16 @@ sub _generate_processattrs { }; my $code = <<"..."; { - my \$attr = \$attrs[$index]; if (exists(\$args->{'$from'})) { - $part1; + $set_value; +... + if ($make_default_value) { + $code .= <<"..."; } else { - $part2; + $make_default_value; +... + } + $code .= <<"..."; } } ... @@ -157,9 +170,9 @@ sub _generate_BUILDALL { 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;