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=0c929a764471cbe6196615edacda92f86f9c77aa;hp=1d55a975aa9ef715681602734fc7a35e43d7f18c;hb=e128626c409797822ffd8a4079f833eb3dc0fd37;hpb=0126c27c413cb63f67e66e09b0fdfeb92117503a diff --git a/lib/Mouse/Meta/Method/Constructor.pm b/lib/Mouse/Meta/Method/Constructor.pm index 1d55a97..0c929a7 100644 --- a/lib/Mouse/Meta/Method/Constructor.pm +++ b/lib/Mouse/Meta/Method/Constructor.pm @@ -1,5 +1,10 @@ package Mouse::Meta::Method::Constructor; -use Mouse::Util; # enables strict and warnings +use Mouse::Util qw(:meta); # enables strict and warnings + +sub _inline_slot{ + my(undef, $self_var, $attr_name) = @_; + return sprintf '%s->{q{%s}}', $self_var, $attr_name; +} sub _generate_constructor { my ($class, $metaclass, $args) = @_; @@ -42,10 +47,16 @@ sub _generate_constructor { } sub _generate_processattrs { - my ($class, $metaclass, $attrs) = @_; + my ($method_class, $metaclass, $attrs) = @_; my @res; my $has_triggers; + my $strict_constructor = $metaclass->__strict_constructor; + + + if($strict_constructor){ + push @res, 'my $used = 0;'; + } for my $index (0 .. @$attrs - 1) { my $code = ''; @@ -55,9 +66,10 @@ sub _generate_processattrs { my $init_arg = $attr->init_arg; my $type_constraint = $attr->type_constraint; + my $is_weak_ref = $attr->is_weak_ref; my $need_coercion; - my $instance_slot = "\$instance->{q{$key}}"; + my $instance_slot = $method_class->_inline_slot('$instance', $key); my $attr_var = "\$attrs[$index]"; my $constraint_var; @@ -71,9 +83,9 @@ sub _generate_processattrs { my $post_process = ''; if(defined $type_constraint){ $post_process .= "\$checks[$index]->($instance_slot)"; - $post_process .= " or $attr_var->verify_type_constraint_error(q{$key}, $instance_slot, $constraint_var);\n"; + $post_process .= " or $attr_var->_throw_type_constraint_error($instance_slot, $constraint_var);\n"; } - if($attr->is_weak_ref){ + if($is_weak_ref){ $post_process .= "Scalar::Util::weaken($instance_slot) if ref $instance_slot;\n"; } @@ -94,7 +106,11 @@ sub _generate_processattrs { $code .= "push \@triggers, [$attr_var\->{trigger}, $instance_slot];\n"; } - $code .= "\n} else {\n"; + if ($strict_constructor){ + $code .= '$used++;' . "\n"; + } + + $code .= "\n} else {\n"; # $value exists } if ($attr->has_default || $attr->has_builder) { @@ -121,6 +137,9 @@ sub _generate_processattrs { } $code .= "$instance_slot = $value;\n"; + if($is_weak_ref){ + $code .= "Scalar::Util::weaken($instance_slot);\n"; + } } } elsif ($attr->is_required) { @@ -132,13 +151,18 @@ sub _generate_processattrs { push @res, $code; } + if($strict_constructor){ + push @res, q{if($used < keys %{$args})} + . q{{ Mouse::Meta::Method::Constructor::_report_unknown_args($metaclass, \@attrs, $instance, $args) }}; + } + if($metaclass->is_anon_class){ - push @res, q{$instnace->{__METACLASS__} = $metaclass;}; + push @res, q{$instance->{__METACLASS__} = $metaclass;}; } if($has_triggers){ unshift @res, q{my @triggers;}; - push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;}; + push @res, q{$_->[0]->($instance, $_->[1]) for @triggers;}; } return join "\n", @res; @@ -172,15 +196,50 @@ sub _generate_BUILDALL { my @code; for my $class ($metaclass->linearized_isa) { - no strict 'refs'; - no warnings 'once'; - - if (*{ $class . '::BUILD' }{CODE}) { + if (Mouse::Util::get_code_ref($class, 'BUILD')) { unshift @code, qq{${class}::BUILD(\$instance, \$args);}; } } return join "\n", @code; } +sub _report_unknown_args { + my($metaclass, $attrs, $instance, $args) = @_; + + my @unknowns; + my %init_args; + foreach my $attr(@{$attrs}){ + my $init_arg = $attr->init_arg; + if(defined $init_arg){ + $init_args{$init_arg}++; + } + } + + while(my $key = each %{$args}){ + if(!exists $init_args{$key}){ + push @unknowns, $key; + } + } + + $metaclass->throw_error( sprintf + "Unknown attribute passed to the constructor of %s: %s", + ref($instance), join ', ', @unknowns + ); +} + 1; __END__ + +=head1 NAME + +Mouse::Meta::Method::Constructor - A Mouse method generator for constructors + +=head1 VERSION + +This document describes Mouse version 0.50_01 + +=head1 SEE ALSO + +L + +=cut