Use @CARP_NOT instead of %Carp::Internal
[gitmo/Mouse.git] / lib / Mouse / Meta / Attribute.pm
index d376101..10a9450 100644 (file)
@@ -5,6 +5,46 @@ use Carp ();
 
 use Mouse::Meta::TypeConstraint;
 
+my %valid_options = map { $_ => undef } (
+  'accessor',
+  'auto_deref',
+  'builder',
+  'clearer',
+  'coerce',
+  'default',
+  'documentation',
+  'does',
+  'handles',
+  'init_arg',
+  'is',
+  'isa',
+  'lazy',
+  'lazy_build',
+  'name',
+  'predicate',
+  'reader',
+  'required',
+  'traits',
+  'trigger',
+  'type_constraint',
+  'weak_ref',
+  'writer',
+
+  # internally used
+  'associated_class',
+  'associated_methods',
+
+  # Moose defines, but Mouse doesn't
+  #'definition_context',
+  #'initializer',
+  #'insertion_order',
+
+  # special case for AttributeHelpers
+  'provides',
+  'curries',
+);
+
+our @CARP_NOT = qw(Mouse::Meta::Class);
 
 sub new {
     my $class = shift;
@@ -21,6 +61,28 @@ sub new {
 
     $args->{name} = $name;
 
+    # check options
+    # (1) known by core
+    my @bad = grep{ !exists $valid_options{$_} } keys %{$args};
+
+    # (2) known by subclasses
+    if(@bad && $class ne __PACKAGE__){
+        my %valid_attrs = (
+            map { $_ => undef }
+            grep { defined }
+            map { $_->init_arg() }
+            $class->meta->get_all_attributes()
+        );
+        @bad = grep{ !exists $valid_attrs{$_} } @bad;
+    }
+
+    # (3) bad options found
+    if(@bad){
+        Carp::carp(
+            "Found unknown argument(s) passed to '$name' attribute constructor in '$class': "
+            . Mouse::Util::english_list(@bad));
+    }
+
     my $self = bless $args, $class;
 
     # extra attributes
@@ -28,13 +90,6 @@ sub new {
         $class->meta->_initialize_object($self, $args);
     }
 
-# XXX: there is no fast way to check attribute validity
-#    my @bad = ...;
-#    if(@bad){
-#        @bad = sort @bad;
-#        Carp::cluck("Found unknown argument(s) passed to '$name' attribute constructor in '$class': @bad");
-#    }
-
     return $self;
 }
 
@@ -147,6 +202,14 @@ sub clone_and_inherit_options{
             $args->{$name} = $self->{$name};
         }
     }
+
+    # remove temporary caches
+    foreach my $attr(keys %{$args}){
+        if($attr =~ /\A _/xms){
+            delete $args->{$attr};
+        }
+    }
+
     return $attribute_class->new($self->name, $args);
 }
 
@@ -312,10 +375,8 @@ sub _canonicalize_handles {
 
 sub _make_delegation_method {
     my($self, $handle, $method_to_call) = @_;
-    my $delegator = $self->delegation_metaclass;
-    Mouse::Util::load_class($delegator);
-
-    return $delegator->_generate_delegation($self, $handle, $method_to_call);
+    return Mouse::Util::load_class($self->delegation_metaclass)
+        ->_generate_delegation($self, $handle, $method_to_call);
 }
 
 sub throw_error{
@@ -334,7 +395,7 @@ Mouse::Meta::Attribute - The Mouse attribute metaclass
 
 =head1 VERSION
 
-This document describes Mouse version 0.47
+This document describes Mouse version 0.50_03
 
 =head1 METHODS