[Dbix-class] Where to define constants.

Bill Moseley moseley at hank.org
Fri Apr 23 21:46:39 GMT 2010


My music DB has CDs and each CD has many tracks.

In my CD source class I have helper methods.

sub total_track_count {
    return shift->tracks->count;
}


And for the sake of the example, I have a "track_type".

sub foo_track_type_count {
    return shift->tracks( { track_type =3D> 1 } )->count;
}

sub bar_track_type_count {
   return shift->tracks( { track_type =3D> { -in =3D> [2,3] } } )->count;
}


I'm finding I need to know the track_type IDs in other code -- for example,
in validation code.  I don't want to hard-code the track_type ids all over
the place.  My question is how do you manage constants like this?

One option is to have a constants module, i.e. "use MyApp::Const
'track_types'" that my result source uses.  Then any other code that needs
those constants also uses MyApp::Const.

Another option would be to define the constants in the Track source.

sub bar_types { return [2,3] }


then back in the Cd class:

sub bar_track_type_count {
   my $self =3D shift;

   my $bar_types =3D $self->result_source->schema->resultset( 'Track'
)->result_class->bar_types;

   return $self->tracks( { track_type =3D> { -in =3D> $bar_types } } )->cou=
nt;
}


How do you manage constants like this?

BTW -- Can't I get the result class w/o building a resultset?  Something
like $schema->source( 'Track') ?





-- =

Bill Moseley
moseley at hank.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20100423/b47=
630ad/attachment.htm


More information about the DBIx-Class mailing list