From: gfx Date: Sun, 4 Oct 2009 07:49:21 +0000 (+0900) Subject: Add find_attribute_by_name() X-Git-Tag: 0.37_02~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2b908b7953b17a0b76e2dedf46235b1cecf2d083;p=gitmo%2FMouse.git Add find_attribute_by_name() --- diff --git a/lib/Mouse/Meta/Class.pm b/lib/Mouse/Meta/Class.pm index dfbce24..ece4323 100644 --- a/lib/Mouse/Meta/Class.pm +++ b/lib/Mouse/Meta/Class.pm @@ -79,6 +79,16 @@ sub get_all_method_names { $self->linearized_isa; } +sub find_attribute_by_name{ + my($self, $name) = @_; + my $attr; + foreach my $class($self->linearized_isa){ + my $meta = Mouse::Util::get_metaclass_by_name($class) or next; + $attr = $meta->get_attribute($name) and last; + } + return $attr; +} + sub add_attribute { my $self = shift; @@ -102,15 +112,7 @@ sub add_attribute { or $self->throw_error('You must provide a name for the attribute'); if ($name =~ s/^\+//) { # inherited attributes - my $inherited_attr; - - # find_attribute_by_name - foreach my $class($self->linearized_isa){ - my $meta = Mouse::Util::get_metaclass_by_name($class) or next; - $inherited_attr = $meta->get_attribute($name) and last; - } - - defined($inherited_attr) + my $inherited_attr = $self->find_attribute_by_name($name) 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(%args);