package Catalyst::ClassData;
use Moose::Role;
+use Moose::Meta::Class ();
use Class::MOP;
-use Scalar::Util 'blessed';
+use Moose::Util ();
sub mk_classdata {
- my ($class, $attribute) = @_;
+ my ($class, $attribute, $warn_on_instance) = @_;
confess("mk_classdata() is a class method, not an object method")
if blessed $class;
my $slot = '$'.$attribute;
my $accessor = sub {
- my $meta = $_[0]->meta;
- if(@_ > 1){
+ my $pkg = ref $_[0] || $_[0];
+ my $meta = Moose::Util::find_meta($pkg)
+ || Moose::Meta::Class->initialize( $pkg );
+ if (@_ > 1) {
$meta->namespace->{$attribute} = \$_[1];
return $_[1];
}
- if( $meta->has_package_symbol($slot) ){
- return ${ $meta->get_package_symbol($slot) };
+ # tighter version of
+ # if ( $meta->has_package_symbol($slot) ) {
+ # return ${ $meta->get_package_symbol($slot) };
+ # }
+ no strict 'refs';
+ my $v = *{"${pkg}::${attribute}"}{SCALAR};
+ if (defined ${$v}) {
+ return ${$v};
} else {
foreach my $super ( $meta->linearized_isa ) {
- my $super_meta = Moose::Meta::Class->initialize($super);
- if( $super_meta->has_package_symbol($slot) ){
- return ${ $super_meta->get_package_symbol($slot) };
+ # tighter version of same after
+ # my $super_meta = Moose::Meta::Class->initialize($super);
+ my $v = ${"${super}::"}{$attribute} ? *{"${super}::${attribute}"}{SCALAR} : undef;
+ if (defined ${$v}) {
+ return ${$v};
}
}
}
confess("Failed to create accessor: $@ ")
unless ref $accessor eq 'CODE';
- my $meta = $class->meta;
+ my $meta = $class->Class::MOP::Object::meta();
+ confess "${class}'s metaclass is not a Class::MOP::Class"
+ unless $meta->isa('Class::MOP::Class');
+
+ my $was_immutable = $meta->is_immutable;
+ my %immutable_options = $meta->immutable_options;
+
+ $meta->make_mutable if $was_immutable;
+
my $alias = "_${attribute}_accessor";
$meta->add_method($alias, $accessor);
$meta->add_method($attribute, $accessor);
+
+ $meta->make_immutable(%immutable_options) if $was_immutable;
+
$class->$attribute($_[2]) if(@_ > 2);
return $accessor;
}
=head1 NAME
-Catalyst::ClassData - Class data acessors
+Catalyst::ClassData - Class data accessors
=head1 METHODS
=head1 COPYRIGHT
-This program is free software, you can redistribute it and/or modify it under
+This library is free software. You can redistribute it and/or modify it under
the same terms as Perl itself.
=cut