reader presedence bug and tests
[gitmo/Moose.git] / lib / Moose / Meta / Attribute.pm
index 91132d5..47587a6 100644 (file)
@@ -7,7 +7,8 @@ use warnings;
 use Scalar::Util 'blessed', 'weaken', 'reftype';
 use Carp         'confess';
 
-our $VERSION = '0.08';
+our $VERSION   = '0.10';
+our $AUTHORITY = 'cpan:STEVAN';
 
 use Moose::Meta::Method::Accessor;
 use Moose::Util::TypeConstraints ();
@@ -38,6 +39,10 @@ __PACKAGE__->meta->add_attribute('handles' => (
     reader    => 'handles',
     predicate => 'has_handles',
 ));
+__PACKAGE__->meta->add_attribute('documentation' => (
+    reader    => 'documentation',
+    predicate => 'has_documentation',
+));
 
 sub new {
        my ($class, $name, %options) = @_;
@@ -47,9 +52,9 @@ sub new {
 
 sub clone_and_inherit_options {
     my ($self, %options) = @_;
-    # you can change default, required and coerce 
+    # you can change default, required, coerce and documentation 
     my %actual_options;
-    foreach my $legal_option (qw(default coerce required)) {
+    foreach my $legal_option (qw(default coerce required documentation)) {
         if (exists $options{$legal_option}) {
             $actual_options{$legal_option} = $options{$legal_option};
             delete $options{$legal_option};
@@ -88,7 +93,7 @@ sub _process_options {
     
        if (exists $options->{is}) {
                if ($options->{is} eq 'ro') {
-                       $options->{reader} = $name;
+                       $options->{reader} ||= $name;
                        (!exists $options->{trigger})
                            || confess "Cannot have a trigger on a read-only attribute";
                }
@@ -207,6 +212,7 @@ sub initialize_instance_slot {
     if (!defined $val && $self->has_default) {
         $val = $self->default($instance); 
     }
+    
        if (defined $val) {
            if ($self->has_type_constraint) {
                my $type_constraint = $self->type_constraint;
@@ -231,18 +237,79 @@ sub initialize_instance_slot {
 
 sub set_value {
     my ($self, $instance, $value) = @_;
+    
+    my $attr_name = $self->name;
+    
+    if ($self->is_required) {
+        defined($value) 
+            || confess "Attribute ($attr_name) is required, so cannot be set to undef";
+    }
+    
+    if ($self->has_type_constraint) {
+        
+        my $type_constraint = $self->type_constraint;
+        
+        if ($self->should_coerce) {
+            $value = $type_constraint->coerce($value);           
+        }
+        defined($type_constraint->_compiled_type_constraint->($value))
+               || confess "Attribute ($attr_name) does not pass the type constraint ("
+               . $type_constraint->name . ") with " . (defined($value) ? ("'" . $value . "'") : "undef")
+          if defined($value);
+    }
+    
+    my $meta_instance = Class::MOP::Class->initialize(blessed($instance))
+                                         ->get_meta_instance;
+                                         
+    $meta_instance->set_slot_value($instance, $attr_name, $value);  
+      
+    if (ref $value && $self->is_weak_ref) {
+        $meta_instance->weaken_slot_value($instance, $attr_name);            
+    }
+    
+    if ($self->has_trigger) {
+        $self->trigger->($instance, $value, $self);
+    }
 }
 
 sub get_value {
     my ($self, $instance) = @_;
-}
-
-sub has_value {
-    my ($self, $instance) = @_;   
-}
-
-sub clear_value {
-    my ($self, $instance) = @_;   
+    
+    if ($self->is_lazy) {
+           unless ($self->has_value($instance)) {
+               if ($self->has_default) {
+                   my $default = $self->default($instance);
+                   $self->set_value($instance, $default);
+               }
+               else {
+                $self->set_value($instance, undef);
+               }
+           }   
+    }
+    
+    if ($self->should_auto_deref) {
+        
+        my $type_constraint = $self->type_constraint;
+
+        if ($type_constraint->is_a_type_of('ArrayRef')) {
+            my $rv = $self->SUPER::get_value($instance);
+            return unless defined $rv;
+            return wantarray ? @{ $rv } : $rv;
+        } 
+        elsif ($type_constraint->is_a_type_of('HashRef')) {
+            my $rv = $self->SUPER::get_value($instance);
+            return unless defined $rv;
+            return wantarray ? %{ $rv } : $rv;
+        } 
+        else {
+            confess "Can not auto de-reference the type constraint '" . $type_constraint->name . "'";
+        }
+               
+    }
+    else {
+        
+        return $self->SUPER::get_value($instance);
+    }    
 }
 
 ## installing accessors 
@@ -279,6 +346,13 @@ sub install_accessors {
             (!$associated_class->has_method($handle))
                 || confess "You cannot overwrite a locally defined method ($handle) with a delegation";
             
+            # NOTE:
+            # handles is not allowed to delegate
+            # any of these methods, as they will 
+            # override the ones in your class, which 
+            # is almost certainly not what you want.
+            next if $handle =~ /^BUILD|DEMOLISH$/ || Moose::Object->can($handle);
+            
             if ((reftype($method_to_call) || '') eq 'CODE') {
                 $associated_class->add_method($handle => $method_to_call);                
             }
@@ -312,7 +386,7 @@ sub _canonicalize_handles {
         ($self->has_type_constraint)
             || confess "Cannot delegate methods based on a RegExpr without a type constraint (isa)";
         return map  { ($_ => $_) } 
-               grep {  $handles  } $self->_get_delegate_method_list;
+               grep { /$handles/ } $self->_get_delegate_method_list;
     }
     elsif (ref($handles) eq 'CODE') {
         return $handles->($self, $self->_find_delegate_metaclass);
@@ -394,13 +468,13 @@ will behave just as L<Class::MOP::Attribute> does.
 
 =item B<initialize_instance_slot>
 
-=item B<generate_accessor_method>
+=item B<install_accessors>
 
-=item B<generate_writer_method>
+=item B<accessor_metaclass>
 
-=item B<generate_reader_method>
+=item B<get_value>
 
-=item B<install_accessors>
+=item B<set_value>
 
 =back
 
@@ -472,6 +546,16 @@ value of an attribute is assigned. The CODE ref will get two values,
 the invocant and the new value. This can be used to handle I<basic> 
 bi-directional relations.
 
+=item B<documentation>
+
+This is a string which contains the documentation for this attribute. 
+It serves no direct purpose right now, but it might in the future
+in some kind of automated documentation system perhaps.
+
+=item B<has_documentation>
+
+Returns true if this meta-attribute has any documentation.
+
 =back
 
 =head1 BUGS
@@ -488,7 +572,7 @@ Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006 by Infinity Interactive, Inc.
+Copyright 2006, 2007 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>