From: gfx Date: Thu, 8 Oct 2009 11:04:56 +0000 (+0900) Subject: Fix some issues on attributes X-Git-Tag: 0.37_04~9 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=95ecd6f132112c6763cdaf2e6bc72c39e9ab76b5;p=gitmo%2FMouse.git Fix some issues on attributes --- diff --git a/lib/Mouse.pm b/lib/Mouse.pm index d57d75e..d964a84 100644 --- a/lib/Mouse.pm +++ b/lib/Mouse.pm @@ -46,6 +46,9 @@ sub has { my $meta = Mouse::Meta::Class->initialize(scalar caller); my $name = shift; + $meta->throw_error(q{Usage: has 'name' => ( key => value, ... )}) + if @_ % 2; # odd number of arguments + $meta->add_attribute($_ => @_) for ref($name) ? @{$name} : $name; } diff --git a/lib/Mouse/Meta/Class.pm b/lib/Mouse/Meta/Class.pm index f418d6c..beaeee5 100644 --- a/lib/Mouse/Meta/Class.pm +++ b/lib/Mouse/Meta/Class.pm @@ -369,12 +369,8 @@ sub _install_modifier { $impl = sub { my ( $self, $type, $name, $code ) = @_; my $into = $self->name; - $install_modifier->( - $into, - $type, - $name, - $code - ); + $install_modifier->($into, $type, $name, $code); + $self->add_method($name => do{ no strict 'refs'; \&{ $into . '::' . $name }; @@ -456,8 +452,8 @@ sub does_role { || $self->throw_error("You must supply a role name to look for"); for my $class ($self->linearized_isa) { - my $meta = Mouse::Util::get_metaclass_by_name($class); - next unless $meta && $meta->can('roles'); + my $meta = Mouse::Util::get_metaclass_by_name($class) + or next; for my $role (@{ $meta->roles }) { @@ -469,7 +465,6 @@ sub does_role { } 1; - __END__ =head1 NAME diff --git a/lib/Mouse/Meta/Method/Accessor.pm b/lib/Mouse/Meta/Method/Accessor.pm index d073cb5..9c5b746 100755 --- a/lib/Mouse/Meta/Method/Accessor.pm +++ b/lib/Mouse/Meta/Method/Accessor.pm @@ -88,11 +88,20 @@ sub _generate_accessor{ $value = '$default'; } + $accessor .= "if(!exists $slot){\n"; if($should_coerce){ - $value = "\$constraint->coerce($value)"; + $accessor .= "$slot = \$constraint->coerce($value)"; } - - $accessor .= "$slot = $value if !exists $slot;\n"; + elsif(defined $constraint){ + $accessor .= "my \$tmp = $value;\n"; + $accessor .= "\$compiled_type_constraint->(\$tmp)"; + $accessor .= "or \$attribute->verify_type_constraint_error(\$name, \$tmp, \$constraint);\n"; + $accessor .= "$slot = \$tmp;\n"; + } + else{ + $accessor .= "$slot = $value;\n"; + } + $accessor .= "}\n"; } if ($should_deref) {