From: Tomas Doran Date: Wed, 18 Nov 2009 22:25:36 +0000 (+0000) Subject: Forgot to commit - ripped the config bits of ::Component out into a role so that... X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=commitdiff_plain;h=3ca85b85fc13f0981f4a9c07000d9fc62f1a43dd Forgot to commit - ripped the config bits of ::Component out into a role so that I could apply to the app class to get the basic stuff --- diff --git a/lib/Catalyst/Config.pm b/lib/Catalyst/Config.pm new file mode 100644 index 0000000..6c72cc5 --- /dev/null +++ b/lib/Catalyst/Config.pm @@ -0,0 +1,40 @@ +package Catalyst::Config; +use Moose::Role; +use Class::MOP (); +use Catalyst::Utils (); +use namespace::autoclean; + +sub config { + my $self = shift; + # Uncomment once sane to do so + #Carp::cluck("config method called on instance") if ref $self; + my $config = $self->_config || {}; + if (@_) { + my $newconfig = { %{@_ > 1 ? {@_} : $_[0]} }; + $self->_config( + $self->merge_config_hashes( $config, $newconfig ) + ); + } else { + # this is a bit of a kludge, required to make + # __PACKAGE__->config->{foo} = 'bar'; + # work in a subclass. + # TODO maybe this should be a ClassData option? + my $class = blessed($self) || $self; + my $meta = Class::MOP::get_metaclass_by_name($class); + unless ($meta->has_package_symbol('$_config')) { + # Call merge_hashes to ensure we deep copy the parent + # config onto the subclass + $self->_config( Catalyst::Utils::merge_hashes($config, {}) ); + } + } + return $self->_config; +} + +sub merge_config_hashes { + my ( $self, $lefthash, $righthash ) = @_; + + return Catalyst::Utils::merge_hashes( $lefthash, $righthash ); +} + +1; +