config wins, groditi loses. FUCK YOU FOR SUPPORTING THAT STUPID BEHAVIOR
[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 {
46d0346d 14 my $meta = $_[0]->meta;
efbfd430 15 if(@_ > 1){
46d0346d 16 $meta->add_package_symbol($slot, \ $_[1]);
efbfd430 17 return $_[1];
18 }
76aab993 19
46d0346d 20 if( $meta->has_package_symbol($slot) ){
21 return ${ $meta->get_package_symbol($slot) };
22 } else {
23 foreach my $super ( $meta->linearized_isa ) {
24 my $super_meta = Moose::Meta::Class->initialize($super);
25 if( $super_meta->has_package_symbol($slot) ){
26 return ${ $super_meta->get_package_symbol($slot) };
27 }
efbfd430 28 }
29 }
30 return;
a7caa492 31 };
76aab993 32
33 confess("Failed to create accessor: $@ ")
efbfd430 34 unless ref $accessor eq 'CODE';
a7caa492 35
efbfd430 36 my $meta = $class->meta;
a7caa492 37 my $alias = "_${attribute}_accessor";
efbfd430 38 $meta->add_method($alias, $accessor);
39 $meta->add_method($attribute, $accessor);
40 $class->$attribute($_[2]) if(@_ > 2);
a7caa492 41 return $accessor;
42}
43
441;
45
46__END__
46d0346d 47
48
49=head1 NAME
50
51Catalyst::ClassData - Class data acessors
52
53=head1 METHODS
54
55=head2 mk_classdata $name, $optional_value
56
57A moose-safe clone of L<Class::Data::Inheritable> that borrows some ideas from
58L<Class::Accessor::Grouped>;
59
60=head1 AUTHOR
61
62Guillermo Roditi
63
64=head1 COPYRIGHT
65
66This program is free software, you can redistribute it and/or modify it under
67the same terms as Perl itself.
68
69=cut