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];
23 # if ( $meta->has_package_symbol($slot) ) {
24 # return ${ $meta->get_package_symbol($slot) };
27 my $v = *{"${pkg}::${attribute}"}{SCALAR};
31 foreach my $super ( $meta->linearized_isa ) {
32 # If there is a code symbol for this attr in a parent class,
33 # then copy it into our package. Is this the correct
34 # fix for C::D::I back-compat? (t0m)
35 my $parent_symbol = *{"${super}::${attribute}"}{CODE} ? \&{"${super}::${attribute}"} : undef;
36 if (defined $parent_symbol) {
37 *{"${pkg}::${attribute}"} = $parent_symbol;
39 # tighter version of same after
40 # my $super_meta = Moose::Meta::Class->initialize($super);
41 my $v = ${"${super}::"}{$attribute} ? *{"${super}::${attribute}"}{SCALAR} : undef;
50 confess("Failed to create accessor: $@ ")
51 unless ref $accessor eq 'CODE';
53 my $meta = $class->Class::MOP::Object::meta();
54 my $immutable_options;
55 if( $meta->is_immutable ){
56 $immutable_options = $meta->get_immutable_options;
59 my $alias = "_${attribute}_accessor";
60 $meta->add_method($alias, $accessor);
61 $meta->add_method($attribute, $accessor);
62 if(defined $immutable_options){
63 $meta->make_immutable(%{ $immutable_options });
65 $class->$attribute($_[2]) if(@_ > 2);
76 Catalyst::ClassData - Class data acessors
80 =head2 mk_classdata $name, $optional_value
82 A moose-safe clone of L<Class::Data::Inheritable> that borrows some ideas from
83 L<Class::Accessor::Grouped>;
91 This program is free software, you can redistribute it and/or modify it under
92 the same terms as Perl itself.