X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FMethod%2FConstructor.pm;h=4c22fd8f7b017fb62e94b65cd6f451d992b38fe6;hb=c91d12e031bd905978b1e664df94fc41bc647d34;hp=b1db7d4bb471187b12ad1e29d54b2b4b6023d712;hpb=24ad3f66c198a2cad61e0d7c91c956b1926746f9;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index b1db7d4..4c22fd8 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -5,9 +5,10 @@ use warnings; sub generate_constructor_method_inline { my ($class, $meta) = @_; + my @attrs = $meta->compute_all_applicable_attributes; my $buildall = $class->_generate_BUILDALL($meta); - my $buildargs = $class->_generate_BUILDARGS(); - my $processattrs = $class->_generate_processattrs($meta); + my $buildargs = $class->_generate_BUILDARGS($meta); + my $processattrs = $class->_generate_processattrs($meta, \@attrs); my $code = <<"..."; sub { @@ -20,8 +21,6 @@ sub generate_constructor_method_inline { } ... - warn $code if $ENV{DEBUG}; - local $@; my $res = eval $code; die $@ if $@; @@ -29,92 +28,122 @@ sub generate_constructor_method_inline { } sub _generate_processattrs { - my ($class, $meta, ) = @_; - my @attrs = $meta->compute_all_applicable_attributes; + my ($class, $meta, $attrs) = @_; my @res; - for my $attr (@attrs) { - my $from = $attr->init_arg; + + for my $index (0 .. @$attrs - 1) { + my $attr = $attrs->[$index]; my $key = $attr->name; - my $part1 = do { - my @code; - if ($attr->should_coerce) { - push @code, "\$args->{\$from} = \$attr->coerce_constraint( \$args->{\$from} );"; + my $code = ''; + + if (defined $attr->init_arg) { + my $from = $attr->init_arg; + + $code .= "if (exists \$args->{'$from'}) {\n"; + + if ($attr->should_coerce && $attr->type_constraint) { + $code .= "my \$value = Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{find_type_constraint}, \$attrs[$index]->{type_constraint}, \$args->{'$from'});\n"; + } + else { + $code .= "my \$value = \$args->{'$from'};\n"; } + if ($attr->has_type_constraint) { - push @code, "\$attr->verify_type_constraint( \$args->{\$from} );"; + $code .= "{ + unless (\$attrs[$index]->{find_type_constraint}->(\$value)) { + \$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint) + } + }"; } - push @code, "\$instance->{\$key} = \$args->{\$from};"; + + $code .= "\$instance->{'$key'} = \$value;\n"; + if ($attr->is_weak_ref) { - push @code, "weaken( \$instance->{\$key} ) if ref( \$instance->{\$key} );"; + $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n"; } - if ( $attr->has_trigger ) { - push @code, "\$attr->trigger->( \$instance, \$args->{\$from}, \$attr );"; + + if ($attr->has_trigger) { + $code .= "\$attrs[$index]->{trigger}->( \$instance, \$value );\n"; } - join "\n", @code; - }; - my $part2 = do { - my @code; - if ( $attr->has_default || $attr->has_builder ) { - unless ( $attr->is_lazy ) { - my $default = $attr->default; - my $builder = $attr->builder; + + $code .= "\n} else {\n"; + } + + if ($attr->has_default || $attr->has_builder) { + unless ($attr->is_lazy) { + my $default = $attr->default; + my $builder = $attr->builder; + + $code .= "my \$value = "; + + if ($attr->should_coerce && $attr->type_constraint) { + $code .= "Mouse::Util::TypeConstraints->typecast_constraints('".$attr->associated_class->name."', \$attrs[$index]->{find_type_constraint}, \$attrs[$index]->{type_constraint}, "; + } + 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();"; + $code .= "\$instance->$builder"; } - if ($attr->should_coerce) { - push @code, "\$value = \$attr->coerce_constraint(\$value);"; + elsif (ref($default) eq 'CODE') { + $code .= "\$attrs[$index]->{default}->(\$instance)"; } - if ($attr->has_type_constraint) { - push @code, "\$attr->verify_type_constraint(\$value);"; + elsif (!defined($default)) { + $code .= 'undef'; } - push @code, "\$instance->{\$key} = \$value;"; - if ($attr->is_weak_ref) { - push @code, "weaken( \$instance->{\$key} ) if ref( \$instance->{\$key} );"; + elsif ($default =~ /^\-?[0-9]+(?:\.[0-9]+)$/) { + $code .= $default; } + else { + $code .= "'$default'"; + } + + if ($attr->should_coerce) { + $code .= ");\n"; } - join "\n", @code; - } - else { - if ( $attr->is_required ) { - q{Carp::confess("Attribute (} . $attr->name . q{) is required");}; - } else { - "" + else { + $code .= ";\n"; } - } - }; - my $code = <<"..."; - { - my \$attr = \$meta->get_attribute('$key'); - my \$from = '$from'; - my \$key = '$key'; - if (defined(\$from) && exists(\$args->{\$from})) { - $part1; - } else { - $part2; + + if ($attr->has_type_constraint) { + $code .= "{ + unless (\$attrs[$index]->{find_type_constraint}->(\$value)) { + \$attrs[$index]->verify_type_constraint_error('$key', \$_, \$attrs[$index]->type_constraint) + } + }"; + } + + $code .= "\$instance->{'$key'} = \$value;\n"; + + if ($attr->is_weak_ref) { + $code .= "Scalar::Util::weaken( \$instance->{'$key'} ) if ref( \$value );\n"; } } -... + } + elsif ($attr->is_required) { + $code .= "Carp::confess('Attribute ($key) is required');"; + } + + $code .= "}\n" if defined $attr->init_arg; + push @res, $code; } + return join "\n", @res; } sub _generate_BUILDARGS { - <<'...'; + my $self = shift; + my $meta = shift; + + if ($meta->name->can('BUILDARGS') && $meta->name->can('BUILDARGS') != Mouse::Object->can('BUILDARGS')) { + return '$class->BUILDARGS(@_)'; + } + + return <<'...'; do { if ( scalar @_ == 1 ) { - if ( defined $_[0] ) { - ( ref( $_[0] ) eq 'HASH' ) + ( ref( $_[0] ) eq 'HASH' ) || Carp::confess "Single parameters to new() must be a HASH ref"; - +{ %{ $_[0] } }; - } - else { - +{}; - } + +{ %{ $_[0] } }; } else { +{@_}; @@ -131,9 +160,10 @@ 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);}; + no warnings 'once'; + for my $klass ($meta->linearized_isa) { + if (*{ $klass . '::BUILD' }{CODE}) { + push @code, qq{${klass}::BUILD(\$instance, \$args);}; } } return join "\n", @code;