|
From: Chris W. <la...@us...> - 2001-10-22 05:42:39
|
Update of /cvsroot/openinteract/SPOPS
In directory usw-pr-cvs1:/tmp/cvs-serv1495
Modified Files:
SPOPS.pm
Log Message:
added the ability to set default values when you initialize an object
Index: SPOPS.pm
===================================================================
RCS file: /cvsroot/openinteract/SPOPS/SPOPS.pm,v
retrieving revision 1.50
retrieving revision 1.51
diff -C2 -d -r1.50 -r1.51
*** SPOPS.pm 2001/10/12 19:59:09 1.50
--- SPOPS.pm 2001/10/22 05:42:37 1.51
***************
*** 116,126 ****
}
DEBUG() && _w( 1, "Creating new object of class ($class) with tie class ",
"($tie_class); lazy loading ($params->{is_lazy_load});",
"field mapping ($params->{is_field_map})" );
- my ( %data );
my $int = tie %data, $tie_class, $class, $params;
DEBUG() && _w( 4, "Internal tie structure of new object: ", Dumper( $int ) );
my $self = bless( \%data, $class );
$self->initialize( $p );
return $self;
--- 116,135 ----
}
+ my ( %data );
+
DEBUG() && _w( 1, "Creating new object of class ($class) with tie class ",
"($tie_class); lazy loading ($params->{is_lazy_load});",
"field mapping ($params->{is_field_map})" );
my $int = tie %data, $tie_class, $class, $params;
DEBUG() && _w( 4, "Internal tie structure of new object: ", Dumper( $int ) );
my $self = bless( \%data, $class );
+
+ # Set defaults if set, unless NOT specified
+
+ my $defaults = $p->{default_values} || $CONFIG->{default_values};
+ if ( ref $defaults eq 'HASH' and ! $p->{skip_default_values} ) {
+ map { $data{ $_ } = $defaults->{ $_ } } keys %{ $defaults };
+ }
+
$self->initialize( $p );
return $self;
***************
*** 792,795 ****
--- 801,807 ----
the MyUser class.
+ You can also pass in default values to use for the object in the
+ 'default_values' key.
+
We use a number of parameters from your object configuration. These
are:
***************
*** 829,832 ****
--- 841,860 ----
All you need to do is create a field map, defining the interface
property names as the keys and the database field names as the values.
+
+ =item *
+
+ B<default_values> (\%) (optional)
+
+ Hashref of field names and default values for the fields when the
+ object is initialized with C<new()>.
+
+ One problem with setting default values in your object configuration
+ B<and> in your database is that the two may become unsynchronized,
+ resulting in many pulled hairs in debugging.
+
+ To get around the synchronization issue, you can set this dynamically
+ using various methods with
+ L<SPOPS::ClassFactory|SPOPS::ClassFactory>. (A sample,
+ C<My::DBI::FindDefaults>, is shipped with SPOPS.)
=back
|