Commit | Line | Data |
a7caa492 |
1 | package Catalyst::ClassData; |
2 | |
3 | use Moose::Role; |
76aab993 |
4 | use Class::MOP; |
a7caa492 |
5 | use Scalar::Util 'blessed'; |
6 | |
7 | sub 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){ |
ce50990e |
16 | $meta->namespace->{$attribute} = \$_[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 | |
44 | 1; |
45 | |
46 | __END__ |
46d0346d |
47 | |
48 | |
49 | =head1 NAME |
50 | |
51 | Catalyst::ClassData - Class data acessors |
52 | |
53 | =head1 METHODS |
54 | |
55 | =head2 mk_classdata $name, $optional_value |
56 | |
57 | A moose-safe clone of L<Class::Data::Inheritable> that borrows some ideas from |
58 | L<Class::Accessor::Grouped>; |
59 | |
60 | =head1 AUTHOR |
61 | |
62 | Guillermo Roditi |
63 | |
64 | =head1 COPYRIGHT |
65 | |
66 | This program is free software, you can redistribute it and/or modify it under |
67 | the same terms as Perl itself. |
68 | |
69 | =cut |