Fix Makefile.PL to use Mouse::Spec
[gitmo/Mouse.git] / lib / Mouse / Meta / Class.pm
index 7084439..1093e02 100644 (file)
@@ -2,16 +2,19 @@ package Mouse::Meta::Class;
 use strict;
 use warnings;
 
-use Mouse::Meta::Method::Constructor;
-use Mouse::Meta::Method::Destructor;
 use Scalar::Util qw/blessed weaken/;
+
 use Mouse::Util qw/get_linear_isa not_supported/;
 
+use Mouse::Meta::Method::Constructor;
+use Mouse::Meta::Method::Destructor;
+use Mouse::Meta::Module;
+
 use base qw(Mouse::Meta::Module);
 
 sub method_metaclass(){ 'Mouse::Meta::Method' } # required for get_method()
 
-sub _new {
+sub _construct_meta {
     my($class, %args) = @_;
 
     $args{attributes} ||= {};
@@ -26,7 +29,7 @@ sub _new {
     #return Mouse::Meta::Class->initialize($class)->new_object(%args)
     #    if $class ne __PACKAGE__;
 
-    return bless \%args, $class;
+    return bless \%args, ref($class) || $class;
 }
 
 sub create_anon_class{
@@ -48,7 +51,23 @@ sub superclasses {
         @{ $self->{superclasses} } = @_;
     }
 
-    @{ $self->{superclasses} };
+    return @{ $self->{superclasses} };
+}
+
+sub find_method_by_name{
+    my($self, $method_name) = @_;
+    defined($method_name)
+        or $self->throw_error('You must define a method name to find');
+    foreach my $class( $self->linearized_isa ){
+        my $method = $self->initialize($class)->get_method($method_name);
+        return $method if defined $method;
+    }
+    return undef;
+}
+
+sub get_all_methods {
+    my($self) = @_;
+    return map{ $self->find_method_by_name($self) } $self->get_all_method_names;
 }
 
 sub get_all_method_names {
@@ -119,6 +138,8 @@ sub new_object {
 
     my $instance = bless {}, $self->name;
 
+    my @triggers_queue;
+
     foreach my $attribute ($self->get_all_attributes) {
         my $from = $attribute->init_arg;
         my $key  = $attribute->name;
@@ -134,7 +155,7 @@ sub new_object {
                 if ref($instance->{$key}) && $attribute->is_weak_ref;
 
             if ($attribute->has_trigger) {
-                $attribute->trigger->($instance, $args{$from});
+                push @triggers_queue, [ $attribute->trigger, $args{$from} ];
             }
         }
         else {
@@ -165,6 +186,16 @@ sub new_object {
             }
         }
     }
+
+    foreach my $trigger_and_value(@triggers_queue){
+        my($trigger, $value) = @{$trigger_and_value};
+        $trigger->($instance, $value);
+    }
+
+    if($self->is_anon_class){
+        $instance->{__METACLASS__} = $self;
+    }
+
     return $instance;
 }