NAME
Catalyst::Plugin::HashClass - use a class to back $c->*
SYNOPSIS
This module can make $c->config
or $c->stash
a real object instead of just a hash. This is good for creating a config class that can load itself from a file and be used both inside and outside of Catalyst.
Assuming your app is called MyApp, set up your config (etc.) object:
package MyApp::Config;
use base 'Catalyst::Component'; # if you want, not required
sub COMPONENT {
my ($class, $app, $original_config) = @_;
return bless $original_config => $class;
}
# you can also implement ACCEPT_CONTEXT if you need $c!
My gut tells me that if your config class needs $c
, you're doing something seriously wrong. But TMTOWTDI, so whatever.
Once you have a class that can load the config, just tell Catalyst to use this class as your config:
package MyApp;
use Catalyst qw/... HashClass .../;
MyApp->config->{Plugin::HashClass} = { config => 'MyApp::Config'};
# or
# MyApp->config->{Plugin::HashClass} = { config => Some::Config->new };
MyApp->setup;
# now $c->config returns an instance of C<MyApp::Config>, optionally
# calling ACCEPT_CONTEXT each time you ask for it
WHY
The main idea is so that you can have a MyApp::Config
class that works the same inside and outside of Catalyst. You can also use the class to massage the config data from something that looks good in the config file to something that the rest of Catalyst understands.
You aren't limited to just changing the config
object, though, you can also change the stash or even add methods:
MyApp->config->{Plugin::HashClass} = { stash => 'MyApp::Stash' };
Now $c->stash
will return a MyApp::Stash
, not an unblessed hash. This could be good; you can have accessors for your hash keys:
package MyApp::Stash;
use base 'Class::Accessor';
__PACKAGE__->mk_accessors(qw/foo bar baz/);
1;
Then in your app:
sub some_action :Local {
my ($self, $c) = @_;
$c->stash->foo('this is foo');
}
It's up to you to decide what to do. I have some uses for this; they'll be on the CPAN shortly :)
METHODS
setup
Called at setup time, obviously. It loads your classes at this point -- everything before HashClass
sees the method calls return their usual hashes, everything after this is called sees them as your configured class. If something bad happens, your app will die right here.
Reads the method => class mapping as an anonymous hash from YourApp->config->{Plugin::HashClass}
.
BUGS
Consider this an experiment. Your feedback is welcome.
AUTHOR
Jonathan Rockway jrockway@cpan.org
COPYRIGHT
Same as Perl itself, yada yada yada.