include test for failure mode
[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
7 sub mk_classdata {
8   my ($class, $attribute) = @_;
9   confess("mk_classdata() is a class method, not an object method")
10     if blessed $class;
11
12   my $slot = '$'.$attribute;
13   my $accessor =  sub {
14     my $pkg = ref $_[0] || $_[0];
15     # Hack - delberately create a metaclass instance
16     my $meta = $pkg->Class::MOP::Object::meta();
17     if (@_ > 1) {
18       $meta->namespace->{$attribute} = \$_[1];
19       return $_[1];
20     }
21
22     # tighter version of
23     # if ( $meta->has_package_symbol($slot) ) {
24     #   return ${ $meta->get_package_symbol($slot) };
25     # }
26     no strict 'refs';
27     my $v = *{"${pkg}::${attribute}"}{SCALAR};
28     if (defined ${$v}) {
29      return ${$v};
30     } else {
31       foreach my $super ( $meta->linearized_isa ) {
32         # tighter version of same after
33         # my $super_meta = Moose::Meta::Class->initialize($super);
34         my $v = ${"${super}::"}{$attribute} ? *{"${super}::${attribute}"}{SCALAR} : undef;
35         if (defined ${$v}) {
36           return ${$v};
37         }
38       }
39     }
40     return;
41   };
42
43   confess("Failed to create accessor: $@ ")
44     unless ref $accessor eq 'CODE';
45
46   my $meta = $class->Class::MOP::Object::meta();
47   confess "${class}'s metaclass is not a Class::MOP::Class"
48     unless $meta->isa('Class::MOP::Class');
49   my $immutable_options;
50   if( $meta->is_immutable ){
51     $immutable_options = $meta->get_immutable_options;
52     $meta->make_mutable;
53   }
54   my $alias = "_${attribute}_accessor";
55   $meta->add_method($alias, $accessor);
56   $meta->add_method($attribute, $accessor);
57   if(defined $immutable_options){
58     $meta->make_immutable(%{ $immutable_options });
59   }
60   $class->$attribute($_[2]) if(@_ > 2);
61   return $accessor;
62 }
63
64 1;
65
66 __END__
67
68
69 =head1 NAME
70
71 Catalyst::ClassData - Class data accessors
72
73 =head1 METHODS
74
75 =head2 mk_classdata $name, $optional_value
76
77 A moose-safe clone of L<Class::Data::Inheritable> that borrows some ideas from
78 L<Class::Accessor::Grouped>;
79
80 =head1 AUTHOR
81
82 Guillermo Roditi
83
84 =head1 COPYRIGHT
85
86 This program is free software, you can redistribute it and/or modify it under
87 the same terms as Perl itself.
88
89 =cut