87b68c9952a85454bfb9d91c3ed48c1accb6a261
[catagits/Catalyst-Runtime.git] / lib / Catalyst / ClassData.pm
1 package Catalyst::ClassData;
2
3 use Moose::Role;
4 use Class::MOP;
5 use Class::MOP::Object;
6 use Scalar::Util 'blessed';
7
8 sub mk_classdata {
9   my ($class, $attribute) = @_;
10   confess("mk_classdata() is a class method, not an object method")
11     if blessed $class;
12
13   my $slot = '$'.$attribute;
14   my $accessor =  sub {
15     my $pkg = ref $_[0] || $_[0];
16     my $meta = $pkg->Class::MOP::Object::meta();
17     if (@_ > 1) {
18       $meta->namespace->{$attribute} = \$_[1];
19       no strict 'refs';
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;
27             last;
28           }
29         }      
30       }
31       return $_[1];
32     }
33
34     # tighter version of
35     # if ( $meta->has_package_symbol($slot) ) {
36     #   return ${ $meta->get_package_symbol($slot) };
37     # }
38     no strict 'refs';
39     my $v = *{"${pkg}::${attribute}"}{SCALAR};
40     if (defined ${$v}) {
41      return ${$v};
42     } else {
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;
47         if (defined ${$v}) {
48           return ${$v};
49         }
50       }
51     }
52     return;
53   };
54
55   confess("Failed to create accessor: $@ ")
56     unless ref $accessor eq 'CODE';
57
58   my $meta = $class->Class::MOP::Object::meta();
59   my $immutable_options;
60   if( $meta->is_immutable ){
61     $immutable_options = $meta->get_immutable_options;
62     $meta->make_mutable;
63   }
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 });
69   }
70   $class->$attribute($_[2]) if(@_ > 2);
71   return $accessor;
72 }
73
74 1;
75
76 __END__
77
78
79 =head1 NAME
80
81 Catalyst::ClassData - Class data acessors
82
83 =head1 METHODS
84
85 =head2 mk_classdata $name, $optional_value
86
87 A moose-safe clone of L<Class::Data::Inheritable> that borrows some ideas from
88 L<Class::Accessor::Grouped>;
89
90 =head1 AUTHOR
91
92 Guillermo Roditi
93
94 =head1 COPYRIGHT
95
96 This program is free software, you can redistribute it and/or modify it under
97 the same terms as Perl itself.
98
99 =cut