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