X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FMethod%2FConstructor.pm;h=0df3eabd6250a63536b62ef2a7842a579e0816aa;hb=f2560fde38fb3827df7a380a5774e265b092a6fd;hp=aee0a7f4e18c25c737c132535c87abeeb5dcb0b8;hpb=ca3bebbd17ab60180dd0e722f43fd29be9b80f06;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index aee0a7f..0df3eab 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -40,25 +40,26 @@ sub _generate_processattrs { my $set_value = do { my @code; - if ($attr->should_coerce) { - push @code, "my \$value = \$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( \$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( \$value );"; + push @code, "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );"; } if ( $attr->has_trigger ) { - push @code, "\$attr->trigger->( \$instance, \$value, \$attr );"; + push @code, "\$attrs[$index]->{trigger}->( \$instance, \$value, \$attrs[$index] );"; } join "\n", @code; @@ -74,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) { @@ -96,13 +102,14 @@ 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;"; if ($attr->is_weak_ref) { - push @code, "weaken( \$instance->{'$key'} ) if ref( \$value );"; + push @code, "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );"; } } join "\n", @code; @@ -117,11 +124,16 @@ sub _generate_processattrs { }; my $code = <<"..."; { - my \$attr = \$attrs[$index]; if (exists(\$args->{'$from'})) { $set_value; +... + if ($make_default_value) { + $code .= <<"..."; } else { $make_default_value; +... + } + $code .= <<"..."; } } ... @@ -160,7 +172,7 @@ sub _generate_BUILDALL { no strict 'refs'; for my $klass ($meta->linearized_isa) { if (*{ $klass . '::BUILD' }{CODE}) { - push @code, qq{${klass}::BUILD->(\$instance, \$args);}; + push @code, qq{${klass}::BUILD(\$instance, \$args);}; } } return join "\n", @code;