X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FAttribute.pm;h=9aa79ada2ec21a5e8dd368f94efba084fb1a9ef3;hb=230dd14a72d301932a9c10cd5121046bc50659ce;hp=c4068e1b19952417a1691a68a7f4dee2a45a4535;hpb=bd76a699d654075009bb9fd707021721eecf8299;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Attribute.pm b/lib/Mouse/Meta/Attribute.pm index c4068e1..9aa79ad 100644 --- a/lib/Mouse/Meta/Attribute.pm +++ b/lib/Mouse/Meta/Attribute.pm @@ -5,37 +5,91 @@ use Carp (); use Mouse::Meta::TypeConstraint; +my %valid_options = map { $_ => undef } ( + 'accessor', + 'auto_deref', + 'builder', + 'clearer', + 'coerce', + 'default', + 'documentation', + 'does', + 'handles', + 'init_arg', + 'is', + 'isa', + 'lazy', + 'lazy_build', + 'name', + 'predicate', + 'reader', + 'required', + 'traits', + 'trigger', + 'type_constraint', + 'weak_ref', + 'writer', + + # internally used + 'associated_class', + 'associated_methods', + + # Moose defines, but Mouse doesn't + #'definition_context', + #'initializer', + #'insertion_order', + + # special case for AttributeHelpers + 'provides', + 'curries', +); + +our @CARP_NOT = qw(Mouse::Meta::Class); sub new { my $class = shift; my $name = shift; - my %args = (@_ == 1) ? %{ $_[0] } : @_; - + my $args = $class->Mouse::Object::BUILDARGS(@_); # XXX: for backward compatibility (with method modifiers) if($class->can('canonicalize_args') != \&canonicalize_args){ - %args = $class->canonicalize_args($name, %args); + %{$args} = $class->canonicalize_args($name, %{$args}); } - $class->_process_options($name, \%args); + $class->_process_options($name, $args); + + $args->{name} = $name; + + # check options + # (1) known by core + my @bad = grep{ !exists $valid_options{$_} } keys %{$args}; + + # (2) known by subclasses + if(@bad && $class ne __PACKAGE__){ + my %valid_attrs = ( + map { $_ => undef } + grep { defined } + map { $_->init_arg() } + $class->meta->get_all_attributes() + ); + @bad = grep{ !exists $valid_attrs{$_} } @bad; + } - $args{name} = $name; + # (3) bad options found + if(@bad){ + Carp::carp( + "Found unknown argument(s) passed to '$name' attribute constructor in '$class': " + . Mouse::Util::english_list(@bad)); + } - my $self = bless \%args, $class; + my $self = bless $args, $class; # extra attributes if($class ne __PACKAGE__){ - $class->meta->_initialize_object($self, \%args); + $class->meta->_initialize_object($self, $args); } -# XXX: there is no fast way to check attribute validity -# my @bad = ...; -# if(@bad){ -# @bad = sort @bad; -# Carp::cluck("Found unknown argument(s) passed to '$name' attribute constructor in '$class': @bad"); -# } - return $self; } @@ -82,7 +136,8 @@ sub interpolate_class{ } sub canonicalize_args{ # DEPRECATED - my ($self, $name, %args) = @_; + #my($self, $name, %args) = @_; + my($self, undef, %args) = @_; Carp::cluck("$self->canonicalize_args has been deprecated." . "Use \$self->_process_options instead."); @@ -91,7 +146,8 @@ sub canonicalize_args{ # DEPRECATED } sub create { # DEPRECATED - my ($self, $class, $name, %args) = @_; + #my($self, $class, $name, %args) = @_; + my($self) = @_; Carp::cluck("$self->create has been deprecated." . "Use \$meta->add_attribute and \$attr->install_accessors instead."); @@ -101,7 +157,8 @@ sub create { # DEPRECATED } sub _coerce_and_verify { - my($self, $value, $instance) = @_; + #my($self, $value, $instance) = @_; + my($self, $value) = @_; my $type_constraint = $self->{type_constraint}; return $value if !defined $type_constraint; @@ -136,18 +193,27 @@ sub _throw_type_constraint_error { } sub clone_and_inherit_options{ - my($self, %args) = @_; + my $self = shift; + my $args = $self->Mouse::Object::BUILDARGS(@_); - my($attribute_class, @traits) = ref($self)->interpolate_class(\%args); + my($attribute_class, @traits) = ref($self)->interpolate_class($args); - $args{traits} = \@traits if @traits; + $args->{traits} = \@traits if @traits; # do not inherit the 'handles' attribute foreach my $name(keys %{$self}){ - if(!exists $args{$name} && $name ne 'handles'){ - $args{$name} = $self->{$name}; + if(!exists $args->{$name} && $name ne 'handles'){ + $args->{$name} = $self->{$name}; + } + } + + # remove temporary caches + foreach my $attr(keys %{$args}){ + if($attr =~ /\A _/xms){ + delete $args->{$attr}; } } - return $attribute_class->new($self->name, %args); + + return $attribute_class->new($self->name, $args); } sub clone_parent { # DEPRECATED @@ -237,7 +303,8 @@ sub clear_value { sub associate_method{ - my ($attribute, $method_name) = @_; + #my($attribute, $method_name) = @_; + my($attribute) = @_; $attribute->{associated_methods}++; return; } @@ -312,10 +379,8 @@ sub _canonicalize_handles { sub _make_delegation_method { my($self, $handle, $method_to_call) = @_; - my $delegator = $self->delegation_metaclass; - Mouse::Util::load_class($delegator); - - return $delegator->_generate_delegation($self, $handle, $method_to_call); + return Mouse::Util::load_class($self->delegation_metaclass) + ->_generate_delegation($self, $handle, $method_to_call); } sub throw_error{ @@ -334,7 +399,7 @@ Mouse::Meta::Attribute - The Mouse attribute metaclass =head1 VERSION -This document describes Mouse version 0.47 +This document describes Mouse version 0.50_08 =head1 METHODS