Add various docs about checking types of attributes against constraints
[gitmo/Moose.git] / lib / Moose / Meta / Attribute.pm
index 95cbe11..f3d8967 100644 (file)
@@ -6,8 +6,10 @@ use warnings;
 
 use Scalar::Util 'blessed', 'weaken', 'reftype';
 use Carp         'confess';
+use overload     ();
 
-our $VERSION = '0.08';
+our $VERSION   = '0.11';
+our $AUTHORITY = 'cpan:STEVAN';
 
 use Moose::Meta::Method::Accessor;
 use Moose::Util::TypeConstraints ();
@@ -38,6 +40,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,14 +53,23 @@ sub new {
 
 sub clone_and_inherit_options {
     my ($self, %options) = @_;
-    # you can change default, required and coerce 
+    # you can change default, required, coerce, documentation and lazy
     my %actual_options;
-    foreach my $legal_option (qw(default coerce required)) {
+    foreach my $legal_option (qw(default coerce required documentation lazy)) {
         if (exists $options{$legal_option}) {
             $actual_options{$legal_option} = $options{$legal_option};
             delete $options{$legal_option};
         }
     }
+    
+    # handles can only be added, not changed
+    if ($options{handles}) {
+        confess "You can only add the 'handles' option, you cannot change it"
+            if $self->has_handles;
+        $actual_options{handles} = $options{handles};
+        delete $options{handles};
+    }
+    
     # isa can be changed, but only if the 
     # new type is a subtype    
     if ($options{isa}) {
@@ -88,7 +103,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";
                }
@@ -193,6 +208,10 @@ sub initialize_instance_slot {
     my $val;        
     if (exists $params->{$init_arg}) {
         $val = $params->{$init_arg};
+        
+        if (!defined $val && $self->is_required) {
+            confess "Attribute (" . $self->name . ") is required and cannot be undef";             
+        }
     }
     else {
         # skip it if it's lazy
@@ -206,8 +225,9 @@ sub initialize_instance_slot {
     # attribute's default value (if it has one)
     if (!defined $val && $self->has_default) {
         $val = $self->default($instance); 
-    }
-       if (defined $val) {
+    }   
+    
+       if (defined $val || $self->has_default) {
            if ($self->has_type_constraint) {
                my $type_constraint = $self->type_constraint;
                    if ($self->should_coerce && $type_constraint->has_coercion) {
@@ -218,7 +238,13 @@ sub initialize_instance_slot {
                            $self->name . 
                            ") does not pass the type constraint (" . 
                            $type_constraint->name .
-                           ") with '$val'";                    
+                           ") with '" . 
+                           (defined $val 
+                               ? (overload::Overloaded($val) 
+                                    ? overload::StrVal($val) 
+                                    : $val) 
+                               : 'undef') . 
+                           "'";                        
         }
        }
 
@@ -248,7 +274,11 @@ sub set_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")
+               . $type_constraint->name 
+               . ") with " 
+               . (defined($value) 
+                    ? ("'" . (overload::Overloaded($value) ? overload::StrVal($value) : $value) . "'") 
+                    : "undef")
           if defined($value);
     }
     
@@ -340,6 +370,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);                
             }
@@ -349,7 +386,9 @@ sub install_accessors {
                     # we should check for lack of 
                     # a callable return value from 
                     # the accessor here 
-                    ((shift)->$accessor_name())->$method_to_call(@_);
+                    my $proxy = (shift)->$accessor_name();
+                    @_ = ($proxy, @_);
+                    goto &{ $proxy->can($method_to_call)};
                 });
             }
         }
@@ -363,23 +402,39 @@ sub install_accessors {
 sub _canonicalize_handles {
     my $self    = shift;
     my $handles = $self->handles;
-    if (ref($handles) eq 'HASH') {
-        return %{$handles};
-    }
-    elsif (ref($handles) eq 'ARRAY') {
-        return map { $_ => $_ } @{$handles};
-    }
-    elsif (ref($handles) eq 'Regexp') {
-        ($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;
-    }
-    elsif (ref($handles) eq 'CODE') {
-        return $handles->($self, $self->_find_delegate_metaclass);
+    if (my $handle_type = ref($handles)) {
+        if ($handle_type eq 'HASH') {
+            return %{$handles};
+        }
+        elsif ($handle_type eq 'ARRAY') {
+            return map { $_ => $_ } @{$handles};
+        }
+        elsif ($handle_type eq 'Regexp') {
+            ($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;
+        }
+        elsif ($handle_type eq 'CODE') {
+            return $handles->($self, $self->_find_delegate_metaclass);
+        }
+        else {
+            confess "Unable to canonicalize the 'handles' option with $handles";
+        }
     }
     else {
-        confess "Unable to canonicalize the 'handles' option with $handles";
+        my $role_meta = eval { $handles->meta };
+        if ($@) {
+            confess "Unable to canonicalize the 'handles' option with $handles because : $@";            
+        }
+
+        (blessed $role_meta && $role_meta->isa('Moose::Meta::Role'))
+            || confess "Unable to canonicalize the 'handles' option with $handles because ->meta is not a Moose::Meta::Role";
+        
+        return map { $_ => $_ } (
+            $role_meta->get_method_list, 
+            $role_meta->get_required_method_list
+        );
     }
 }
 
@@ -463,6 +518,25 @@ will behave just as L<Class::MOP::Attribute> does.
 
 =item B<set_value>
 
+  eval { $point->meta->get_attribute('x')->set_value($point, 'fourty-two') };
+  if($@) {
+    print "Oops: $@\n";
+  }
+
+I<Attribute (x) does not pass the type constraint (Int) with 'fourty-two'>
+
+Before setting the value, a check is made on the type constraint of
+the attribute, if it has one, to see if the value passes it. If the
+value fails to pass, the set operation dies with a L<Carp/confess>.
+
+Any coercion to convert values is done before checking the type constraint.
+
+To check a value against a type constraint before setting it, fetch the
+attribute instance using L<Moose::Meta::Attribute/find_attribute_by_name>,
+fetch the type_constraint from the attribute using L<Moose::Meta::Attribute/type_constraint>
+and call L<Moose::Meta::TypeConstraint/check>. See L<Moose::Cookbook::RecipeX>
+for an example.
+
 =back
 
 =head2 Additional Moose features
@@ -533,6 +607,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
@@ -549,7 +633,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>