From: Shawn M Moore Date: Fri, 27 Jun 2008 03:56:02 +0000 (+0000) Subject: Add canonicalize_args, factor lazy_build canonicalization/validation into the right... X-Git-Tag: 0.19~270 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=commitdiff_plain;h=93d190e0204bf7f6b9dd6920c0539866ed89b487;hp=f7b11a21869769d2b49bba4fa72d33fa54ad0c2e Add canonicalize_args, factor lazy_build canonicalization/validation into the right spot --- diff --git a/lib/Mouse/Meta/Attribute.pm b/lib/Mouse/Meta/Attribute.pm index af9e737..df091cc 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,6 +167,7 @@ sub create { $args{name} = $name; $args{class} = $class; + %args = $self->canonicalize_args($name, %args); $self->validate_args($name, %args); $args{type_constraint} = delete $args{isa} @@ -224,11 +208,37 @@ sub create { return $attribute; } +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 = @_; + 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}); @@ -403,6 +413,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. diff --git a/t/023-builder.t b/t/023-builder.t index f8a2a2e..6b3e35e 100644 --- a/t/023-builder.t +++ b/t/023-builder.t @@ -106,7 +106,7 @@ do { lazy_build => 1, default => 1, ); - } qr/You can not use lazy_build and default for the same attribute error/; + } qr/You can not use lazy_build and default for the same attribute \(error\)/; }; my @calls; @@ -120,7 +120,6 @@ do { builder => 'build_my_customs', predicate => 'has_my_customs', clearer => 'clear_my_customs', - ); sub build_my_customs { @@ -129,7 +128,6 @@ do { } }; - my $cb = Class::CustomBuilder->new; ok(!$cb->has_my_customs, "correct predicate"); is($cb->custom, 'yo'); @@ -160,3 +158,4 @@ is_deeply([splice @calls], ['_build__attr']); ok($cub->_has_attr, "correct predicate"); ok($cub->_clear_attr, "correct clearer"); ok(!$cub->_has_attr, "correct predicate"); +