X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst%2FClassData.pm;h=f79149b063d4abcd5678460da8c0fc068c3443be;hp=23fd1107b999bcc97a05b8ff3ed2ef93a93e7288;hb=b0ff1be8caade25b5485bf174f0224b2066a3b8f;hpb=efbfd430a9e62335eca426a2be3bc098d65f8d1a diff --git a/lib/Catalyst/ClassData.pm b/lib/Catalyst/ClassData.pm index 23fd110..f79149b 100644 --- a/lib/Catalyst/ClassData.pm +++ b/lib/Catalyst/ClassData.pm @@ -1,35 +1,64 @@ package Catalyst::ClassData; use Moose::Role; -use Scalar::Util 'blessed'; +use Moose::Meta::Class (); +use Class::MOP; +use Moose::Util (); sub mk_classdata { - my ($class, $attribute) = @_; + my ($class, $attribute, $warn_on_instance) = @_; confess("mk_classdata() is a class method, not an object method") if blessed $class; my $slot = '$'.$attribute; my $accessor = sub { - if(@_ > 1){ - $_[0]->meta->add_package_symbol($slot, \ $_[1]); + my $pkg = ref $_[0] || $_[0]; + my $meta = Moose::Util::find_meta($pkg) + || Moose::Meta::Class->initialize( $pkg ); + if (@_ > 1) { + $meta->namespace->{$attribute} = \$_[1]; return $_[1]; } - foreach my $super ( $_[0], $_[0]->meta->linearized_isa ) { - my $meta = $super->meta; - 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 ) { + # 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}; + } } } return; }; - my $accessor = eval $code; - confess("Failed to create accessor: $@ \n $code \n") + + confess("Failed to create accessor: $@ ") unless ref $accessor eq 'CODE'; - my $meta = $class->meta; + my $meta = $class->Class::MOP::Object::meta(); + confess "${class}'s metaclass is not a Class::MOP::Class" + unless $meta->isa('Class::MOP::Class'); + + my $was_immutable = $meta->is_immutable; + my %immutable_options = $meta->immutable_options; + + $meta->make_mutable if $was_immutable; + my $alias = "_${attribute}_accessor"; $meta->add_method($alias, $accessor); $meta->add_method($attribute, $accessor); + + $meta->make_immutable(%immutable_options) if $was_immutable; + $class->$attribute($_[2]) if(@_ > 2); return $accessor; } @@ -38,17 +67,29 @@ sub mk_classdata { __END__ -# my $code = ' sub { -# if(@_ > 1){ -# $_[0]->meta->add_package_symbol(\''.$slot.'\', \ $_[1]); -# return $_[1]; -# } -# foreach my $super ( $_[0], $_[0]->meta->linearized_isa ) { -# my $meta = $super->meta; -# if( $meta->has_package_symbol(\''.$slot.'\') ){ -# return $meta->get_package_symbol(\''.$slot.'\'); -# } -# } -# return; -# }'; -# my $accessor = eval $code; + +=head1 NAME + +Catalyst::ClassData - Class data accessors + +=head1 METHODS + +=head2 mk_classdata $name, $optional_value + +A moose-safe clone of L that borrows some ideas from +L; + +=head1 AUTHOR + +=begin stopwords + +Guillermo Roditi + +=end stopwords + +=head1 COPYRIGHT + +This library is free software. You can redistribute it and/or modify it under +the same terms as Perl itself. + +=cut