X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FAttribute.pm;h=479c43c945ca0203c5363fe0fb73989d0fe3037a;hb=2b9094e8e533f1635eae1481eef711828f521508;hp=051ebe3130cded641f88319f102907f28f53dc4d;hpb=7f7406a5026ff1f517e7753e12068a69620dace6;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Attribute.pm b/lib/Mouse/Meta/Attribute.pm index 051ebe3..479c43c 100644 --- a/lib/Mouse/Meta/Attribute.pm +++ b/lib/Mouse/Meta/Attribute.pm @@ -2,9 +2,10 @@ package Mouse::Meta::Attribute; use strict; use warnings; +require overload; use Carp 'confess'; -use Mouse::Util qw/blessed weaken/; +require Mouse::Util; sub new { my $class = shift; @@ -36,6 +37,7 @@ sub type_constraint { $_[0]->{type_constraint} } sub trigger { $_[0]->{trigger} } sub builder { $_[0]->{builder} } sub should_auto_deref { $_[0]->{auto_deref} } +sub should_coerce { $_[0]->{should_coerce} } sub has_default { exists $_[0]->{default} } sub has_predicate { exists $_[0]->{predicate} } @@ -60,71 +62,82 @@ sub inlined_name { sub generate_accessor { my $attribute = shift; - my $name = $attribute->name; - my $key = $name; - my $default = $attribute->default; - my $type = $attribute->type_constraint; - my $constraint = $attribute->find_type_constraint; - my $builder = $attribute->builder; - my $trigger = $attribute->trigger; + my $name = $attribute->name; + my $default = $attribute->default; + my $type = $attribute->type_constraint; + my $constraint = $attribute->find_type_constraint; + my $builder = $attribute->builder; + my $trigger = $attribute->trigger; + my $is_weak = $attribute->is_weak_ref; + my $should_deref = $attribute->should_auto_deref; + my $should_coerce = $attribute->should_coerce; - my $accessor = 'sub { - my $self = shift;'; + my $self = '$_[0]'; + my $key = $attribute->inlined_name; + my $accessor = "sub {\n"; if ($attribute->_is_metadata eq 'rw') { - $accessor .= 'if (@_) { - '; + $accessor .= 'if (scalar(@_) >= 2) {' . "\n"; + + my $value = '$_[1]'; if ($constraint) { - $accessor .= 'local $_ = $_[0]; + if ($should_coerce) { + $accessor .= $value.' = $attribute->coerce_constraint('.$value.');'; + } + $accessor .= 'local $_ = '.$value.';'; + $accessor .= ' unless ($constraint->()) { my $display = defined($_) ? overload::StrVal($_) : "undef"; Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display"); - }' + }' . "\n" } - $accessor .= '$self->{$key} = $_[0];'; + # if there's nothing left to do for the attribute we can return during + # this setter + $accessor .= 'return ' if !$is_weak && !$trigger && !$should_deref; + + $accessor .= $self.'->{'.$key.'} = '.$value.';' . "\n"; - if ($attribute->is_weak_ref) { - $accessor .= 'weaken($self->{$key}) if ref($self->{$key});'; + if ($is_weak) { + $accessor .= 'Mouse::Util::weaken('.$self.'->{'.$key.'}) if ref('.$self.'->{'.$key.'});' . "\n"; } if ($trigger) { - $accessor .= '$trigger->($self, $_[0], $attribute);'; + $accessor .= '$trigger->('.$self.', '.$value.', $attribute);' . "\n"; } - $accessor .= '}'; + $accessor .= "}\n"; } else { - $accessor .= 'confess "Cannot assign a value to a read-only accessor" if @_;'; + $accessor .= 'confess "Cannot assign a value to a read-only accessor" if scalar(@_) >= 2;' . "\n"; } if ($attribute->is_lazy) { - $accessor .= '$self->{$key} = '; + $accessor .= $self.'->{'.$key.'} = '; $accessor .= $attribute->has_builder - ? '$self->$builder' - : ref($default) eq 'CODE' - ? '$default->($self)' - : '$default'; - - $accessor .= ' if !exists($self->{$key});'; + ? $self.'->$builder' + : ref($default) eq 'CODE' + ? '$default->('.$self.')' + : '$default'; + $accessor .= ' if !exists '.$self.'->{'.$key.'};' . "\n"; } - if ($attribute->should_auto_deref) { + if ($should_deref) { if ($attribute->type_constraint eq 'ArrayRef') { $accessor .= 'if (wantarray) { - return @{ $self->{$key} || [] }; + return @{ '.$self.'->{'.$key.'} || [] }; }'; } else { $accessor .= 'if (wantarray) { - return %{ $self->{$key} || {} }; + return %{ '.$self.'->{'.$key.'} || {} }; }'; } } - $accessor .= 'return $self->{$key}; + $accessor .= 'return '.$self.'->{'.$key.'}; }'; my $sub = eval $accessor; @@ -134,9 +147,9 @@ sub generate_accessor { sub generate_predicate { my $attribute = shift; - my $key = $attribute->name; + my $key = $attribute->inlined_name; - my $predicate = 'sub { exists($_[0]->{$key}) }'; + my $predicate = 'sub { exists($_[0]->{'.$key.'}) }'; my $sub = eval $predicate; confess $@ if $@; @@ -145,9 +158,9 @@ sub generate_predicate { sub generate_clearer { my $attribute = shift; - my $key = $attribute->name; + my $key = $attribute->inlined_name; - my $clearer = 'sub { delete($_[0]->{$key}) }'; + my $clearer = 'sub { delete($_[0]->{'.$key.'}) }'; my $sub = eval $clearer; confess $@ if $@; @@ -185,6 +198,9 @@ sub create { %args = $self->canonicalize_args($name, %args); $self->validate_args($name, \%args); + $args{should_coerce} = delete $args{coerce} + if exists $args{coerce}; + $args{type_constraint} = delete $args{isa} if exists $args{isa}; @@ -285,10 +301,10 @@ sub find_type_constraint { return unless $type; - my $checker = Mouse::TypeRegistry->optimized_constraints->{$type}; + my $checker = Mouse::TypeRegistry->optimized_constraints()->{$type}; return $checker if $checker; - return sub { blessed($_) && blessed($_) eq $type }; + return sub { Mouse::Util::blessed($_) && Mouse::Util::blessed($_) eq $type }; } sub verify_type_constraint { @@ -306,6 +322,13 @@ sub verify_type_constraint { Carp::confess("Attribute ($name) does not pass the type constraint because: Validation failed for \'$type\' failed with value $display"); } +sub coerce_constraint { + my($self, $value) = @_; + my $type = $self->type_constraint + or return $value; + return Mouse::TypeRegistry->typecast_constraints($self->associated_class->name, $type, $value); +} + sub _canonicalize_handles { my $self = shift; my $handles = shift;