From: Stevan Little Date: Sat, 6 May 2006 15:01:04 +0000 (+0000) Subject: foo X-Git-Tag: 0_09_03~38 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=536f0b1739154330082a42ba0c096d9d43cdc134;p=gitmo%2FMoose.git foo --- diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index e441481..505c0f7 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -152,6 +152,13 @@ sub _process_options { if $options->{weak_ref}; } + if (exists $options->{auto_deref} && $options->{auto_deref}) { + (exists $options->{type_constraint}) + || confess "You cannot auto-dereference without specifying a type constraint"; + ($options->{type_constraint}->name =~ /^ArrayRef|HashRef$/) + || confess "You cannot auto-dereference anything other than a ArrayRef or HashRef"; + } + if (exists $options->{lazy} && $options->{lazy}) { (exists $options->{default}) || confess "You cannot have lazy attribute without specifying a default value for it"; @@ -245,15 +252,17 @@ sub _inline_auto_deref { return $ref_value unless $self->should_auto_deref; - my $type = eval { $self->type_constraint->name } || ''; - my $sigil; + my $type = $self->type_constraint->name; - if ( $type eq "ArrayRef" ) { + my $sigil; + if ($type eq "ArrayRef") { $sigil = '@'; - } elsif ( $type eq 'HashRef' ) { + } + elsif ($type eq 'HashRef') { $sigil = '%'; - } else { - confess "Can't auto deref unless type constraint is ArrayRef or HashRef"; + } + else { + confess "Can not auto de-reference the type constraint '$type'"; } "(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )"; @@ -281,7 +290,7 @@ sub generate_accessor_method { '$_[0]->{$attr_name} = ($attr->has_default ? $attr->default($_[0]) : undef)' . 'unless exists $_[0]->{$attr_name};' : '') - . 'return ' . $attr->_inline_auto_deref( $attr->_inline_get( $inv ) ) + . 'return ' . $attr->_inline_auto_deref($attr->_inline_get($inv)) . ' }'; my $sub = eval $code; warn "Could not create accessor for '$attr_name' because $@ \n code: $code" if $@; @@ -404,6 +413,14 @@ NOTE: lazy attributes, B have a C field set. Returns true if this meta-attribute should perform type coercion. +=item B + +Returns true if this meta-attribute should perform automatic +auto-dereferencing. + +NOTE: This can only be done for attributes whose type constraint is +either I or I. + =item B Returns true if this meta-attribute has a trigger set.