move-delegation-to-attr
[gitmo/Moose.git] / lib / Moose / Meta / Class.pm
index e41d199..5b0a8e5 100644 (file)
@@ -7,9 +7,9 @@ use warnings;
 use Class::MOP;
 
 use Carp         'confess';
-use Scalar::Util 'weaken', 'blessed';
+use Scalar::Util 'weaken', 'blessed', 'reftype';
 
-our $VERSION = '0.04';
+our $VERSION = '0.05';
 
 use base 'Class::MOP::Class';
 
@@ -18,6 +18,15 @@ __PACKAGE__->meta->add_attribute('roles' => (
     default => sub { [] }
 ));
 
+sub initialize {
+    my $class = shift;
+    my $pkg   = shift;
+    $class->SUPER::initialize($pkg,
+        ':attribute_metaclass' => 'Moose::Meta::Attribute', 
+        ':instance_metaclass'  => 'Moose::Meta::Instance', 
+        @_);
+}
+
 sub add_role {
     my ($self, $role) = @_;
     (blessed($role) && $role->isa('Moose::Meta::Role'))
@@ -39,47 +48,22 @@ sub new_object {
     my ($class, %params) = @_;
     my $self = $class->SUPER::new_object(%params);
     foreach my $attr ($class->compute_all_applicable_attributes()) {
-        next unless $params{$attr->name} && $attr->has_trigger;
-        $attr->trigger->($self, $params{$attr->name});
+        next unless $params{$attr->init_arg} && $attr->can('has_trigger') && $attr->has_trigger;
+        $attr->trigger->($self, $params{$attr->init_arg}, $attr);
     }
     return $self;    
 }
 
 sub construct_instance {
     my ($class, %params) = @_;
-    my $instance = $params{'__INSTANCE__'} || {};
+    my $meta_instance = $class->get_meta_instance;
+    # FIXME:
+    # the code below is almost certainly incorrect
+    # but this is foreign inheritence, so we might
+    # have to kludge it in the end. 
+    my $instance = $params{'__INSTANCE__'} || $meta_instance->create_instance();
     foreach my $attr ($class->compute_all_applicable_attributes()) {
-        my $init_arg = $attr->init_arg();
-        # try to fetch the init arg from the %params ...
-        my $val;        
-        if (exists $params{$init_arg}) {
-            $val = $params{$init_arg};
-        }
-        else {
-            # skip it if it's lazy
-            next if $attr->is_lazy;
-            # and die if it's required and doesn't have a default value
-            confess "Attribute (" . $attr->name . ") is required" 
-                if $attr->is_required && !$attr->has_default;
-        }
-        # if nothing was in the %params, we can use the 
-        # attribute's default value (if it has one)
-        if (!defined $val && $attr->has_default) {
-            $val = $attr->default($instance); 
-        }
-               if (defined $val) {
-                   if ($attr->has_type_constraint) {
-                   if ($attr->should_coerce && $attr->type_constraint->has_coercion) {
-                       $val = $attr->type_constraint->coercion->coerce($val);
-                   }   
-                (defined($attr->type_constraint->check($val))) 
-                    || confess "Attribute (" . $attr->name . ") does not pass the type contraint with '$val'";                 
-            }
-               }
-        $instance->{$attr->name} = $val;
-        if (defined $val && $attr->is_weak_ref) {
-            weaken($instance->{$attr->name});
-        }
+        $attr->initialize_instance_slot($meta_instance, $instance, \%params)
     }
     return $instance;
 }
@@ -99,7 +83,6 @@ sub has_method {
     return $self->SUPER::has_method($method_name);    
 }
 
-
 sub add_override_method_modifier {
     my ($self, $name, $method, $_super_package) = @_;
     # need this for roles ...
@@ -184,6 +167,8 @@ to the L<Class::MOP::Class> documentation.
 
 =over 4
 
+=item B<initialize>
+
 =item B<new_object>
 
 We override this method to support the C<trigger> attribute option.
@@ -229,6 +214,27 @@ This will test if this class C<does> a given C<$role_name>. It will
 not only check it's local roles, but ask them as well in order to 
 cascade down the role hierarchy.
 
+=item B<add_attribute $attr_name, %params>
+
+This method does the same thing as L<Class::MOP::Class/add_attribute>, but adds
+suport for delegation.
+
+=back
+
+=head1 INTERNAL METHODS
+
+=over 4
+
+=item compute_delegation
+
+=item generate_delegation_list
+
+=item generate_delgate_method
+
+=item get_delegatable_methods
+
+=item filter_delegations
+
 =back
 
 =head1 BUGS
@@ -250,4 +256,5 @@ L<http://www.iinteractive.com>
 This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself. 
 
-=cut
\ No newline at end of file
+=cut
+