Changes to date added for wu-lee.
[gitmo/Mouse.git] / lib / Mouse / Meta / Class.pm
index 8e89567..9e6ef57 100644 (file)
@@ -5,7 +5,7 @@ use warnings;
 use Mouse::Meta::Method::Constructor;
 use Mouse::Meta::Method::Destructor;
 use Scalar::Util qw/blessed/;
-use Mouse::Util qw/get_linear_isa/;
+use Mouse::Util qw/get_linear_isa version authority identifier/;
 use Carp 'confess';
 
 do {
@@ -20,8 +20,9 @@ do {
     }
 
     sub initialize {
-        my $class = shift;
-        my $name  = shift;
+        my $class = blessed($_[0]) || $_[0];
+        my $name  = $_[1];
+
         $METACLASS_CACHE{$name} = $class->new(name => $name)
             if !exists($METACLASS_CACHE{$name});
         return $METACLASS_CACHE{$name};
@@ -68,6 +69,12 @@ sub add_method {
     *{ $pkg . '::' . $name } = $code;
 }
 
+sub has_method {
+    my $self = shift;
+    my $name = shift;
+    $self->name->can($name);
+}
+
 # copied from Class::Inspector
 my $get_methods_for_class = sub {
     my $self = shift;
@@ -76,7 +83,7 @@ my $get_methods_for_class = sub {
     no strict 'refs';
     # Get all the CODE symbol table entries
     my @functions =
-      grep !/^(?:has|with|around|before|after|blessed|extends|confess|override|super)$/,
+      grep !/^(?:has|with|around|before|after|augment|inner|blessed|extends|confess|override|super)$/,
       grep { defined &{"${name}::$_"} }
       keys %{"${name}::"};
     push @functions, keys %{$self->{'methods'}->{$name}} if $self;
@@ -149,6 +156,10 @@ sub compute_all_applicable_attributes {
 sub get_attribute_map { $_[0]->{attributes} }
 sub has_attribute     { exists $_[0]->{attributes}->{$_[1]} }
 sub get_attribute     { $_[0]->{attributes}->{$_[1]} }
+sub get_attribute_list {
+    my $self = shift;
+    keys %{$self->get_attribute_map};
+}
 
 sub linearized_isa { @{ get_linear_isa($_[0]->name) } }
 
@@ -260,6 +271,22 @@ sub add_after_method_modifier {
     $self->_install_modifier( $self->name, 'after', $name, $code );
 }
 
+sub add_override_method_modifier {
+    my ($self, $name, $code) = @_;
+
+    my $pkg = $self->name;
+    my $method = "${pkg}::${name}";
+
+    # Class::Method::Modifiers won't do this for us, so do it ourselves
+
+    my $body = $pkg->can($name)
+        or confess "You cannot override '$method' because it has no super method";
+
+    no strict 'refs';
+    *$method = sub { $code->($pkg, $body, @_) };
+}
+
+
 sub roles { $_[0]->{roles} }
 
 sub does_role {
@@ -268,8 +295,11 @@ sub does_role {
     (defined $role_name)
         || confess "You must supply a role name to look for";
 
-    for my $role (@{ $self->{roles} }) {
-        return 1 if $role->name eq $role_name;
+    for my $class ($self->linearized_isa) {
+        next unless $class->can('meta') and $class->meta->can('roles');
+        for my $role (@{ $self->roles }) {
+            return 1 if $role->name eq $role_name;
+        }
     }
 
     return 0;
@@ -392,6 +422,12 @@ this class and its superclasses.
 Returns a mapping of attribute names to their corresponding
 L<Mouse::Meta::Attribute> objects.
 
+=head2 get_attribute_list -> { name => Mouse::Meta::Attribute }
+
+This returns a list of attribute names which are defined in the local
+class. If you want a list of all applicable attributes for a class,
+use the C<compute_all_applicable_attributes> method.
+
 =head2 has_attribute Name -> Bool
 
 Returns whether we have a L<Mouse::Meta::Attribute> with the given name.