[jifty-devel] Subclassing models
Stanislav Sinyagin
ssinyagin at yahoo.com
Sat Jan 5 09:43:27 EST 2008
another thing that I had to figure out myself:
I need to create several Model classes with similar functionality.
For example, some classes will have a set of properties stored
in another model, and would implement set_property and get_property methods.
Creating these methods in every model class would be too dirty and prone to errors,
so I subclassed such models from another class where these methods are defined.
The problem is that I can't put such superclass in MyApp::Model namespace,
because Jifty expects every class in that namespace to be a valid Model class.
I created a new namespace MyApp::ModelAddons for such add-ons to the models.
Probably the core developers would have a better idea of a "canonical" place
for such add-ons?
the code follows:
======================
use strict;
use warnings;
package Toponet::ModelAddons::Properties;
sub _init_properties
{
my $self = shift;
my $propContainerClass = shift;
$self->{'_propertyContainerClass'} = $propContainerClass;
}
sub get_property
{
my $self = shift;
my $prop = shift;
my $propRecord = $self->{'_propertyContainerClass'}->new
( current_user => $self->current_user());
if( $propRecord->load_by_cols( objectID => $self->id(),
property => $prop ) )
{
return $propRecord->value();
}
return undef;
}
sub set_property
{
my $self = shift;
my $prop = shift;
my $value = shift;
my $propRecord = $self->{'_propertyContainerClass'}->new
( current_user => $self->current_user());
my( $ok, $msg) = $propRecord->load_by_cols( objectID => $self->id(),
property => $prop );
if( $ok )
{
$propRecord->set_value( $value );
}
else
{
$propRecord->create( objectID => $self->id(),
property => $prop,
value => $value );
}
}
1;
===========================
More information about the jifty-devel
mailing list