Commit | Line | Data |
a7caa492 |
1 | package Catalyst::ClassData; |
2 | |
3 | use Moose::Role; |
76aab993 |
4 | use Class::MOP; |
74c89dea |
5 | use Class::MOP::Object; |
a7caa492 |
6 | use Scalar::Util 'blessed'; |
7 | |
8 | sub mk_classdata { |
efbfd430 |
9 | my ($class, $attribute) = @_; |
a7caa492 |
10 | confess("mk_classdata() is a class method, not an object method") |
efbfd430 |
11 | if blessed $class; |
12 | |
13 | my $slot = '$'.$attribute; |
14 | my $accessor = sub { |
eece41a2 |
15 | my $pkg = ref $_[0] || $_[0]; |
74c89dea |
16 | my $meta = $pkg->Class::MOP::Object::meta(); |
17 | if (@_ > 1){ |
ce50990e |
18 | $meta->namespace->{$attribute} = \$_[1]; |
efbfd430 |
19 | return $_[1]; |
20 | } |
76aab993 |
21 | |
eece41a2 |
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}; |
46d0346d |
30 | } else { |
31 | foreach my $super ( $meta->linearized_isa ) { |
8a440eba |
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; |
38 | } |
eece41a2 |
39 | # tighter version of same after |
40 | # my $super_meta = Moose::Meta::Class->initialize($super); |
875d8110 |
41 | my $v = ${"${super}::"}{$attribute} ? *{"${super}::${attribute}"}{SCALAR} : undef; |
eece41a2 |
42 | if (defined ${$v}) { |
43 | return ${$v}; |
46d0346d |
44 | } |
efbfd430 |
45 | } |
46 | } |
47 | return; |
a7caa492 |
48 | }; |
76aab993 |
49 | |
50 | confess("Failed to create accessor: $@ ") |
efbfd430 |
51 | unless ref $accessor eq 'CODE'; |
a7caa492 |
52 | |
74c89dea |
53 | my $meta = $class->Class::MOP::Object::meta(); |
843c9233 |
54 | my $immutable_options; |
55 | if( $meta->is_immutable ){ |
56 | $immutable_options = $meta->get_immutable_options; |
57 | $meta->make_mutable; |
58 | } |
a7caa492 |
59 | my $alias = "_${attribute}_accessor"; |
efbfd430 |
60 | $meta->add_method($alias, $accessor); |
61 | $meta->add_method($attribute, $accessor); |
843c9233 |
62 | if(defined $immutable_options){ |
11ae7378 |
63 | $meta->make_immutable(%{ $immutable_options }); |
843c9233 |
64 | } |
efbfd430 |
65 | $class->$attribute($_[2]) if(@_ > 2); |
a7caa492 |
66 | return $accessor; |
67 | } |
68 | |
69 | 1; |
70 | |
71 | __END__ |
46d0346d |
72 | |
73 | |
74 | =head1 NAME |
75 | |
76 | Catalyst::ClassData - Class data acessors |
77 | |
78 | =head1 METHODS |
79 | |
80 | =head2 mk_classdata $name, $optional_value |
81 | |
82 | A moose-safe clone of L<Class::Data::Inheritable> that borrows some ideas from |
83 | L<Class::Accessor::Grouped>; |
84 | |
85 | =head1 AUTHOR |
86 | |
87 | Guillermo Roditi |
88 | |
89 | =head1 COPYRIGHT |
90 | |
91 | This program is free software, you can redistribute it and/or modify it under |
92 | the same terms as Perl itself. |
93 | |
94 | =cut |