X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FMouse%2FMeta%2FAttribute.pm;h=42a212b6fa83f66cd27a3817eda230df0bfad45b;hb=8fd9e611aa5ab3bc951c0ff4e300ecbe1f9b0c0f;hp=2e626b0162f36ff143514d258665b9a52badf281;hpb=306290e864ac23e5f1692c8495b0c173081a1ebb;p=gitmo%2FMouse.git diff --git a/lib/Mouse/Meta/Attribute.pm b/lib/Mouse/Meta/Attribute.pm index 2e626b0..42a212b 100644 --- a/lib/Mouse/Meta/Attribute.pm +++ b/lib/Mouse/Meta/Attribute.pm @@ -17,20 +17,21 @@ sub new { bless \%args, $class; } -sub name { $_[0]->{name} } -sub class { $_[0]->{class} } -sub _is_metadata { $_[0]->{is} } -sub is_required { $_[0]->{required} } -sub default { $_[0]->{default} } -sub is_lazy { $_[0]->{lazy} } -sub predicate { $_[0]->{predicate} } -sub clearer { $_[0]->{clearer} } -sub handles { $_[0]->{handles} } -sub is_weak_ref { $_[0]->{weak_ref} } -sub init_arg { $_[0]->{init_arg} } -sub type_constraint { $_[0]->{type_constraint} } -sub trigger { $_[0]->{trigger} } -sub builder { $_[0]->{builder} } +sub name { $_[0]->{name} } +sub class { $_[0]->{class} } +sub _is_metadata { $_[0]->{is} } +sub is_required { $_[0]->{required} } +sub default { $_[0]->{default} } +sub is_lazy { $_[0]->{lazy} } +sub predicate { $_[0]->{predicate} } +sub clearer { $_[0]->{clearer} } +sub handles { $_[0]->{handles} } +sub is_weak_ref { $_[0]->{weak_ref} } +sub init_arg { $_[0]->{init_arg} } +sub type_constraint { $_[0]->{type_constraint} } +sub trigger { $_[0]->{trigger} } +sub builder { $_[0]->{builder} } +sub should_auto_deref { $_[0]->{auto_deref} } sub has_default { exists $_[0]->{default} } sub has_predicate { exists $_[0]->{predicate} } @@ -93,7 +94,20 @@ sub generate_accessor { $accessor .= ' if !exists($self->{$key});'; } - $accessor .= 'return $self->{$key} + if ($attribute->should_auto_deref) { + if ($attribute->type_constraint eq 'ArrayRef') { + $accessor .= 'if (wantarray) { + return @{ $self->{$key} || [] }; + }'; + } + else { + $accessor .= 'if (wantarray) { + return %{ $self->{$key} || {} }; + }'; + } + } + + $accessor .= 'return $self->{$key}; }'; return eval $accessor; @@ -141,12 +155,7 @@ sub generate_handles { sub create { my ($self, $class, $name, %args) = @_; - confess "You cannot have lazy attribute ($name) without specifying a default value for it" - 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'; + $self->validate_args($name, %args); $args{type_constraint} = delete $args{isa} if exists $args{isa}; @@ -184,6 +193,29 @@ sub create { return $attribute; } +sub validate_args { + my $self = shift; + my $name = shift; + my %args = @_; + + confess "You cannot have lazy attribute ($name) without specifying a default value for it" + 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'; + + confess "You cannot auto-dereference without specifying a type constraint on attribute $name" + 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'; + + return 1; +} + sub find_type_constraint { my $self = shift; my $type = $self->type_constraint; @@ -285,6 +317,8 @@ installed. Some error checking is done. =head2 has_builder -> Bool +=head2 should_auto_deref -> Bool + Informational methods. =head2 generate_accessor -> CODE