still failing some tests. waiting for suggestions on whether to fix old CDIretardedness
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ClassData.pm
CommitLineData
a7caa492 1package Catalyst::ClassData;
2
3use Moose::Role;
76aab993 4use Class::MOP;
a7caa492 5use Scalar::Util 'blessed';
6
7sub mk_classdata {
efbfd430 8 my ($class, $attribute) = @_;
a7caa492 9 confess("mk_classdata() is a class method, not an object method")
efbfd430 10 if blessed $class;
11
12 my $slot = '$'.$attribute;
13 my $accessor = sub {
14 if(@_ > 1){
76aab993 15 $_[0]->meta->add_package_symbol($slot, \ $_[1]);
efbfd430 16 return $_[1];
17 }
76aab993 18
19 foreach my $super ( (blessed $_[0] || $_[0]), $_[0]->meta->linearized_isa ) {
20 my $meta = Moose::Meta::Class->initialize($super);
efbfd430 21 if( $meta->has_package_symbol($slot) ){
76aab993 22 return ${ $meta->get_package_symbol($slot) };
efbfd430 23 }
24 }
25 return;
a7caa492 26 };
76aab993 27
28 confess("Failed to create accessor: $@ ")
efbfd430 29 unless ref $accessor eq 'CODE';
a7caa492 30
efbfd430 31 my $meta = $class->meta;
a7caa492 32 my $alias = "_${attribute}_accessor";
efbfd430 33 $meta->add_method($alias, $accessor);
34 $meta->add_method($attribute, $accessor);
35 $class->$attribute($_[2]) if(@_ > 2);
a7caa492 36 return $accessor;
37}
38
391;
40
41__END__