X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FMethod%2FConstructor.pm;h=83bf3efd82e06f0241ead220f4ec30e99cd81d5c;hp=77cced9787de9070d81ef18ffab60193d3e68799;hb=6d46df7af00723bd7a0972abf5e5d62481ff6435;hpb=63d74d7adc05bdf93288048a851adaefd6ffc8d3 diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index 77cced9..83bf3ef 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -5,7 +5,7 @@ use warnings; sub generate_constructor_method_inline { my ($class, $meta) = @_; - my @attrs = $meta->compute_all_applicable_attributes; # this one is using by evaled code + my @attrs = $meta->compute_all_applicable_attributes; my $buildall = $class->_generate_BUILDALL($meta); my $buildargs = $class->_generate_BUILDARGS(); my $processattrs = $class->_generate_processattrs($meta, \@attrs); @@ -21,8 +21,6 @@ sub generate_constructor_method_inline { } ... - warn $code if $ENV{DEBUG}; - local $@; my $res = eval $code; die $@ if $@; @@ -40,25 +38,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 +73,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 +100,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 +122,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 .= <<"..."; } } ...