Question

I'm trying to get multiple Models for each resource type to share one main resource which does login for a Catalyst::Model::REST extended class.

MyApp::Model::Game (Moose enforced required attributes of hostname, username, password) MyApp::Model::Game::Account MyApp::Model::Game::Character

The ::Account and ::Character both extend MyApp::Model::Game, Game has it's own package config defined in the MyApp/lib/MyApp.pm:

__PACKAGE__->config(
    name => 'PowerGame',
   'Model::Game' => {
        hostname => "Somehostname.com",
        username => "username",
        password => "Hax0rs",
    }
}

I'm unable to find documentation on allowing the Model::Game::Account and Model::Game::Character to use the same application config as Model::Game, since I have required attributes, the application fails to start claiming I haven't assigned required attributes.

I'm currently just creating another entry for 'Model::Game::Account' and 'Model::Game::Character' respectively as a workaround.

If I'm going about this the wrong way to share configs between packages,

Was it helpful?

Solution

Take a look at the docs for Catalyst::Component, which says:

The component's config hash is merged with any config entry on the application for this component and passed to new() ...

Which is quite a useful feature! This basically means that you can populate any attribute in the class from the config option.

In which case, you can create a single base class for all your models that derives from Catalyst::Model::REST and set the credentials in the attributes in that class. However, that will not allow you to put credentials into external configuration file.

package MyApp::ModelBase::REST;
use Moose;
extends 'Catalyst::Model::REST';
has '+username' => (default => 'username');

package MyApp::Model::Game;
use Moose;
extends 'MyApp::ModelBase::REST';

The only way I can think of achieving this with external configuration file is unfortunately to duplicate the data, unless you use Perl file as your config, in which case you can define variables and re-use them inside the config.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top