X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FClassData.pm;h=87b68c9952a85454bfb9d91c3ed48c1accb6a261;hb=76721d3c6f8660effa365b8c1574b1e87f4973cb;hp=e7379d1ddf13126c9ab2450c6c28ad0c2ea4dec9;hpb=46d0346ddafe8e167c679cddef9834946598e689;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/ClassData.pm b/lib/Catalyst/ClassData.pm index e7379d1..87b68c9 100644 --- a/lib/Catalyst/ClassData.pm +++ b/lib/Catalyst/ClassData.pm @@ -2,6 +2,7 @@ package Catalyst::ClassData; use Moose::Role; use Class::MOP; +use Class::MOP::Object; use Scalar::Util 'blessed'; sub mk_classdata { @@ -11,19 +12,40 @@ sub mk_classdata { my $slot = '$'.$attribute; my $accessor = sub { - my $meta = $_[0]->meta; - if(@_ > 1){ - $meta->add_package_symbol($slot, \ $_[1]); + my $pkg = ref $_[0] || $_[0]; + my $meta = $pkg->Class::MOP::Object::meta(); + if (@_ > 1) { + $meta->namespace->{$attribute} = \$_[1]; + no strict 'refs'; + if (! *{"${pkg}::${attribute}"}{CODE} ) { + foreach my $super ( $meta->linearized_isa ) { + # If there is a code symbol for this class data in a parent class, but not in our + # class then copy it into our package. This is evil. + my $parent_symbol = *{"${super}::${attribute}"}{CODE} ? \&{"${super}::${attribute}"} : undef; + if (defined $parent_symbol) { + *{"${pkg}::${attribute}"} = $parent_symbol; + last; + } + } + } return $_[1]; } - if( $meta->has_package_symbol($slot) ){ - return ${ $meta->get_package_symbol($slot) }; + # tighter version of + # if ( $meta->has_package_symbol($slot) ) { + # return ${ $meta->get_package_symbol($slot) }; + # } + no strict 'refs'; + my $v = *{"${pkg}::${attribute}"}{SCALAR}; + if (defined ${$v}) { + return ${$v}; } else { foreach my $super ( $meta->linearized_isa ) { - my $super_meta = Moose::Meta::Class->initialize($super); - if( $super_meta->has_package_symbol($slot) ){ - return ${ $super_meta->get_package_symbol($slot) }; + # tighter version of same after + # my $super_meta = Moose::Meta::Class->initialize($super); + my $v = ${"${super}::"}{$attribute} ? *{"${super}::${attribute}"}{SCALAR} : undef; + if (defined ${$v}) { + return ${$v}; } } } @@ -33,10 +55,18 @@ sub mk_classdata { confess("Failed to create accessor: $@ ") unless ref $accessor eq 'CODE'; - my $meta = $class->meta; + my $meta = $class->Class::MOP::Object::meta(); + my $immutable_options; + if( $meta->is_immutable ){ + $immutable_options = $meta->get_immutable_options; + $meta->make_mutable; + } my $alias = "_${attribute}_accessor"; $meta->add_method($alias, $accessor); $meta->add_method($attribute, $accessor); + if(defined $immutable_options){ + $meta->make_immutable(%{ $immutable_options }); + } $class->$attribute($_[2]) if(@_ > 2); return $accessor; }