In the documentation of DBIx::Class::Schema::Loader::Base there is a reference to adding "schema components"[1] to the generated classes. But I can't find any documentation on how you might write such a component - or any clearly-labelled examples of a schema component.

I think I have an example of something that I'd like to write as a schema component. Please either tell me that I'm wrong, or gently nudge me in the right direction.

Here's what I want to do. Recently I've found myself adding a useful "get_schema()" method to many of my schema classes. It looks something like this:

sub get_schema {
  my @errs;

    push @errs, $_ unless defined $ENV{$_};
  if (@errs) {
    croak "You need to set the following environment variables: @errs\n";

  return __PACKAGE__->connect(
    { mysql_enable_utf8 => 1 }

The "XXX" is a placeholder for some prefix that is meaningful to whatever schema I'm working with and it will change with each project (as will, potentially, the DBD name and the options hash).

I've been copying the code into my schema classes, but we all know what a Bad Idea that is. And a schema component seemed a likely-looking approach for getting round that. I thought I could write DBIx::Class::Component::GetSchema which adds the method to any schema class that loads it.

But, as I said above, I can't find any explanations or examples of schema components that I can steal from.

Does anyone have any suggestions?




Searchable Archive:

Reply via email to