1 package Catalyst::ClassData;
5 use Class::MOP::Object;
6 use Scalar::Util 'blessed';
9 my ($class, $attribute) = @_;
10 confess("mk_classdata() is a class method, not an object method")
13 my $slot = '$'.$attribute;
15 my $pkg = ref $_[0] || $_[0];
16 my $meta = $pkg->Class::MOP::Object::meta();
18 $meta->namespace->{$attribute} = \$_[1];
20 if (! *{"${pkg}::${attribute}"}{CODE} ) {
21 foreach my $super ( $meta->linearized_isa ) {
22 # If there is a code symbol for this class data in a parent class, but not in our
23 # class then copy it into our package. This is evil.
24 my $parent_symbol = *{"${super}::${attribute}"}{CODE} ? \&{"${super}::${attribute}"} : undef;
25 if (defined $parent_symbol) {
26 *{"${pkg}::${attribute}"} = $parent_symbol;
35 # if ( $meta->has_package_symbol($slot) ) {
36 # return ${ $meta->get_package_symbol($slot) };
39 my $v = *{"${pkg}::${attribute}"}{SCALAR};
43 foreach my $super ( $meta->linearized_isa ) {
44 # tighter version of same after
45 # my $super_meta = Moose::Meta::Class->initialize($super);
46 my $v = ${"${super}::"}{$attribute} ? *{"${super}::${attribute}"}{SCALAR} : undef;
55 confess("Failed to create accessor: $@ ")
56 unless ref $accessor eq 'CODE';
58 my $meta = $class->Class::MOP::Object::meta();
59 my $immutable_options;
60 if( $meta->is_immutable ){
61 $immutable_options = $meta->get_immutable_options;
64 my $alias = "_${attribute}_accessor";
65 $meta->add_method($alias, $accessor);
66 $meta->add_method($attribute, $accessor);
67 if(defined $immutable_options){
68 $meta->make_immutable(%{ $immutable_options });
70 $class->$attribute($_[2]) if(@_ > 2);
81 Catalyst::ClassData - Class data acessors
85 =head2 mk_classdata $name, $optional_value
87 A moose-safe clone of L<Class::Data::Inheritable> that borrows some ideas from
88 L<Class::Accessor::Grouped>;
96 This program is free software, you can redistribute it and/or modify it under
97 the same terms as Perl itself.