X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FAttribute.pm;h=050ec49c2c2ece61e39e50b7ca0435972220e143;hb=1bbaa8edd19af95a395eb59d34602c1b85def6d6;hp=af9e737602b1377a6048b6c5003d361bf42da1e3;hpb=f7b11a21869769d2b49bba4fa72d33fa54ad0c2e;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Attribute.pm b/lib/Mouse/Meta/Attribute.pm index af9e737..050ec49 100644 --- a/lib/Mouse/Meta/Attribute.pm +++ b/lib/Mouse/Meta/Attribute.pm @@ -17,23 +17,6 @@ sub new { $args{is} ||= ''; - if ($args{lazy_build}) { - confess("You can not use lazy_build and default for the same attribute $name") - if exists $args{default}; - $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}); - } - } - bless \%args, $class; } @@ -184,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}; @@ -224,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; } @@ -403,6 +419,16 @@ this attribute's type constraint; Checks that the given value passes this attribute's type constraint. Returns 1 on success, otherwise Ces. +=head2 canonicalize_args Name, %args -> %args + +Canonicalizes some arguments to create. In particular, C is +canonicalized into C, C, etc. + +=head2 validate_args Name, \%args -> 1 | ERROR + +Checks that the arguments to create the attribute (ie those specified by +C) are valid. + =head2 clone_parent OwnerClass, AttributeName, %args -> Mouse::Meta::Attribute Creates a new attribute in OwnerClass, inheriting options from parent classes.