We need Attribute->validate_args to take a hashref instead of a hash
[gitmo/Mouse.git] / lib / Mouse / Meta / Attribute.pm
index 733c129..050ec49 100644 (file)
@@ -10,16 +10,13 @@ sub new {
     my $class = shift;
     my %args  = @_;
 
-    $args{init_arg} = $args{name}
+    my $name = $args{name};
+
+    $args{init_arg} = $name
         unless exists $args{init_arg};
 
     $args{is} ||= '';
 
-    if ( $args{lazy_build} ) {
-        $args{lazy} = 1;
-        $args{builder} ||= "_build_$args{name}";
-    }
-
     bless \%args, $class;
 }
 
@@ -29,6 +26,7 @@ sub _is_metadata      { $_[0]->{is}              }
 sub is_required       { $_[0]->{required}        }
 sub default           { $_[0]->{default}         }
 sub is_lazy           { $_[0]->{lazy}            }
+sub is_lazy_build     { $_[0]->{lazy_build}      }
 sub predicate         { $_[0]->{predicate}       }
 sub clearer           { $_[0]->{clearer}         }
 sub handles           { $_[0]->{handles}         }
@@ -169,7 +167,8 @@ sub create {
     $args{name} = $name;
     $args{class} = $class;
 
-    $self->validate_args($name, %args);
+    %args = $self->canonicalize_args($name, %args);
+    $self->validate_args($name, \%args);
 
     $args{type_constraint} = delete $args{isa}
         if exists $args{isa};
@@ -209,25 +208,57 @@ sub create {
     return $attribute;
 }
 
-sub validate_args {
+sub canonicalize_args {
     my $self = shift;
     my $name = shift;
     my %args = @_;
 
+    if ($args{lazy_build}) {
+        $args{lazy}      = 1;
+        $args{required}  = 1;
+        $args{builder}   = "_build_${name}"
+            if !exists($args{builder});
+        if ($name =~ /^_/) {
+            $args{clearer}   = "_clear${name}" if !exists($args{clearer});
+            $args{predicate} = "_has${name}" if !exists($args{predicate});
+        }
+        else {
+            $args{clearer}   = "clear_${name}" if !exists($args{clearer});
+            $args{predicate} = "has_${name}" if !exists($args{predicate});
+        }
+    }
+
+    return %args;
+}
+
+sub validate_args {
+    my $self = shift;
+    my $name = shift;
+    my $args = shift;
+
+    confess "You can not use lazy_build and default for the same attribute ($name)"
+        if $args->{lazy_build} && exists $args->{default};
+
     confess "You cannot have lazy attribute ($name) without specifying a default value for it"
-        if $args{lazy} && !exists($args{default}) && !exists($args{builder});
+        if $args->{lazy}
+        && !exists($args->{default})
+        && !exists($args->{builder});
 
     confess "References are not allowed as default values, you must wrap the default of '$name' in a CODE reference (ex: sub { [] } and not [])"
-        if ref($args{default})
-        && ref($args{default}) ne 'CODE';
+        if ref($args->{default})
+        && ref($args->{default}) ne 'CODE';
 
     confess "You cannot auto-dereference without specifying a type constraint on attribute $name"
-        if $args{auto_deref} && !exists($args{isa});
+        if $args->{auto_deref} && !exists($args->{isa});
 
     confess "You cannot auto-dereference anything other than a ArrayRef or HashRef on attribute $name"
-        if $args{auto_deref}
-        && $args{isa} ne 'ArrayRef'
-        && $args{isa} ne 'HashRef';
+        if $args->{auto_deref}
+        && $args->{isa} ne 'ArrayRef'
+        && $args->{isa} ne 'HashRef';
+
+    confess "Trigger must be a CODE or HASH ref on attribute ($name)"
+        if $args{trigger}
+        && ref($args{trigger}) ne 'CODE' && ref($args{trigger}) ne 'HASH';
 
     return 1;
 }
@@ -289,7 +320,7 @@ sub get_parent_args {
     my $name  = shift;
 
     for my $super ($class->meta->linearized_isa) {
-        my $super_attr = $super->meta->get_attribute($name)
+        my $super_attr = $super->can("meta") && $super->meta->get_attribute($name)
             or next;
         return %{ $super_attr->_create_args };
     }
@@ -356,6 +387,8 @@ installed. Some error checking is done.
 
 =head2 has_builder -> Bool
 
+=head2 is_lazy_build => Bool
+
 =head2 should_auto_deref -> Bool
 
 Informational methods.
@@ -386,5 +419,25 @@ this attribute's type constraint;
 Checks that the given value passes this attribute's type constraint. Returns 1
 on success, otherwise C<confess>es.
 
+=head2 canonicalize_args Name, %args -> %args
+
+Canonicalizes some arguments to create. In particular, C<lazy_build> is
+canonicalized into C<lazy>, C<builder>, etc.
+
+=head2 validate_args Name, \%args -> 1 | ERROR
+
+Checks that the arguments to create the attribute (ie those specified by
+C<has>) are valid.
+
+=head2 clone_parent OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute
+
+Creates a new attribute in OwnerClass, inheriting options from parent classes.
+Accessors and helper methods are installed. Some error checking is done.
+
+=head2 get_parent_args OwnerClass, AttributeName -> Hash
+
+Returns the options that the parent class of C<OwnerClass> used for attribute
+C<AttributeName>.
+
 =cut