X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FClass.pm;h=cea2064030148f3e3d68c929e5719d03bdb949a9;hp=06c4f354ccbf437aa64556b1c1f0f6c1c7788bc3;hb=87ca293be7ca0041bbb86ad9609a498fb2010f4f;hpb=ef9070cc6ee59ca3691f3765ad96ef0922e064ca diff --git a/lib/Mouse/Meta/Class.pm b/lib/Mouse/Meta/Class.pm index 06c4f35..cea2064 100644 --- a/lib/Mouse/Meta/Class.pm +++ b/lib/Mouse/Meta/Class.pm @@ -78,40 +78,48 @@ sub get_all_method_names { $self->linearized_isa; } -sub _process_attribute{ +sub add_attribute { my $self = shift; - my $name = shift; - - my $args = (@_ == 1) ? $_[0] : { @_ }; - defined($name) - or $self->throw_error('You must provide a name for the attribute'); + my($attr, $name); - if ($name =~ s/^\+//) { - my $inherited_attr; - - foreach my $class($self->linearized_isa){ - my $meta = Mouse::Meta::Module::get_metaclass_by_name($class) or next; - $inherited_attr = $meta->get_attribute($name) and last; - } + if(blessed $_[0]){ + $attr = $_[0]; - defined($inherited_attr) - or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name); + $attr->isa('Mouse::Meta::Attribute') + || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)"); - return $inherited_attr->clone_and_inherit_options($name, $args); + $name = $attr->name; } else{ - return Mouse::Meta::Attribute->interpolate_class_and_new($name, $args); - } -} + # _process_attribute + $name = shift; -sub add_attribute { - my $self = shift; + my %args = (@_ == 1) ? %{$_[0]} : @_; - my $attr = blessed($_[0]) ? $_[0] : $self->_process_attribute(@_); + defined($name) + or $self->throw_error('You must provide a name for the attribute'); - $attr->isa('Mouse::Meta::Attribute') - || $self->throw_error("Your attribute must be an instance of Mouse::Meta::Attribute (or a subclass)"); + if ($name =~ s/^\+//) { # inherited attributes + my $inherited_attr; + + foreach my $class($self->linearized_isa){ + my $meta = Mouse::Meta::Module::get_metaclass_by_name($class) or next; + $inherited_attr = $meta->get_attribute($name) and last; + } + + defined($inherited_attr) + or $self->throw_error("Could not find an attribute by the name of '$name' to inherit from in ".$self->name); + + $attr = $inherited_attr->clone_and_inherit_options($name, \%args); + } + else{ + my($attribute_class, @traits) = Mouse::Meta::Attribute->interpolate_class($name, \%args); + $args{traits} = \@traits if @traits; + + $attr = $attribute_class->new($name, \%args); + } + } weaken( $attr->{associated_class} = $self );