no Moose;
+__PACKAGE__->meta->make_immutable;
+
1;
use Moose;
use Catalyst::Exception;
-
-with 'Catalyst::ClassData';
use Scalar::Util 'blessed';
+with 'Catalyst::ClassData';
no Moose;
-__PACKAGE__->mk_classdata($_) for qw/_attr_cache _action_cache/;
-__PACKAGE__->_attr_cache( {} );
-__PACKAGE__->_action_cache( [] );
+__PACKAGE__->mk_classdata(_attr_cache => {} );
+__PACKAGE__->mk_classdata( _action_cache => [] );
# note - see attributes(3pm)
sub MODIFY_CODE_ATTRIBUTES {
my $slot = '$'.$attribute;
my $accessor = sub {
+ my $meta = $_[0]->meta;
if(@_ > 1){
- $_[0]->meta->add_package_symbol($slot, \ $_[1]);
+ $meta->add_package_symbol($slot, \ $_[1]);
return $_[1];
}
- foreach my $super ( (blessed $_[0] || $_[0]), $_[0]->meta->linearized_isa ) {
- my $meta = Moose::Meta::Class->initialize($super);
- if( $meta->has_package_symbol($slot) ){
- return ${ $meta->get_package_symbol($slot) };
+ if( $meta->has_package_symbol($slot) ){
+ return ${ $meta->get_package_symbol($slot) };
+ } 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) };
+ }
}
}
return;
1;
__END__
+
+
+=head1 NAME
+
+Catalyst::ClassData - Class data acessors
+
+=head1 METHODS
+
+=head2 mk_classdata $name, $optional_value
+
+A moose-safe clone of L<Class::Data::Inheritable> that borrows some ideas from
+L<Class::Accessor::Grouped>;
+
+=head1 AUTHOR
+
+Guillermo Roditi
+
+=head1 COPYRIGHT
+
+This program is free software, you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
=cut
-__PACKAGE__->mk_classdata($_) for qw/_config _plugins/;
+__PACKAGE__->mk_classdata('_plugins');
around new => sub {
my ( $orig, $self) = @_;
}
sub config {
- my $self = shift;
- my $config = $self->_config ||{};
- if (@_) {
- my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} };
- $self->_config(
- $self->merge_config_hashes( $config, $newconfig )
- );
+ my $self = shift;
+ my $class = blessed $self || $self;
+
+ my $config;
+ my $meta = $class->meta;
+ if( $meta->has_package_symbol('$config') ){
+ $config = ${ $meta->get_package_symbol('$config') };
+ } else {
+ foreach my $super ( $meta->linearized_isa ) {
+ my $super_meta = Moose::Meta::Class->initialize($super);
+ if( $super_meta->has_package_symbol('$config') ){
+ $config = ${ $super_meta->get_package_symbol('$config') };
+ unless( @_ ){ #don't copy and write it twice
+ $config = $class->merge_config_hashes( $config, {} );
+ $meta->add_package_symbol('$config', \ $config);
+ }
+ last;
+ }
}
- return $config;
+ }
+
+ unless( defined $config ){
+ $config = {};
+ $meta->add_package_symbol('$config', \ $config) unless @_;
+ }
+
+ if (@_) {
+ my $from_args = { %{@_ > 1 ? {@_} : $_[0]} };
+ my $new_config = $class->merge_config_hashes( $config, $from_args);
+ $meta->add_package_symbol('$config', \ $new_config);
+ }
+
+ return $config;
}
sub merge_config_hashes {
. " did not override Catalyst::Component::process" );
}
+
+__PACKAGE__->meta->make_immutable;
1;
__END__
no Moose;
+__PACKAGE__->meta->make_immutable;
+
1;
__END__
=cut
+__PACKAGE__->meta->make_immutable;
+
1;
=cut
no Moose;
-#__PACKAGE__->meta->make_immutable();
+__PACKAGE__->meta->make_immutable();
1;
use strict;
use warnings;
use Scalar::Util qw/refaddr blessed/;
-use Test::More tests => 32;
+use Test::More tests => 37;
{
package ClassDataTest;
my $scalarref2 = \$scalar2;
my $coderef2 = sub { "beep" };
+ my $scalar3 = '300';
+ my $arrayref3 = [];
+ my $hashref3 = {};
+ my $scalarref3 = \$scalar3;
+ my $coderef3 = sub { "beep" };
+
my @accessors = qw/_arrayref _hashref _scalarref _coderef _scalar/;
ClassDataTest->mk_classdata($_) for @accessors;
is(refaddr(ClassDataTest->_scalarref), refaddr($scalarref));
is(refaddr(ClassDataTest->_coderef), refaddr($coderef));
is(ClassDataTest->_scalar, $scalar);
+
+ClassDataTest->_arrayref($arrayref3);
+ClassDataTest->_hashref($hashref3);
+ClassDataTest->_scalarref($scalarref3);
+ClassDataTest->_coderef($coderef3);
+ClassDataTest->_scalar($scalar3);
+
+is(refaddr(ClassDataTest->_arrayref), refaddr($arrayref3));
+is(refaddr(ClassDataTest->_hashref), refaddr($hashref3));
+is(refaddr(ClassDataTest->_scalarref), refaddr($scalarref3));
+is(refaddr(ClassDataTest->_coderef), refaddr($coderef3));
+is(ClassDataTest->_scalar, $scalar3);