|
From: Chris W. <la...@us...> - 2001-10-22 15:21:46
|
Update of /cvsroot/openinteract/SPOPS
In directory usw-pr-cvs1:/tmp/cvs-serv5315
Modified Files:
SPOPS.pm
Log Message:
updated default_values to accept the key 'NOW' and to deal with a
class/method being defined in a hashref that will return the default
(me today speak english good)
Index: SPOPS.pm
===================================================================
RCS file: /cvsroot/openinteract/SPOPS/SPOPS.pm,v
retrieving revision 1.51
retrieving revision 1.52
diff -C2 -d -r1.51 -r1.52
*** SPOPS.pm 2001/10/22 05:42:37 1.51
--- SPOPS.pm 2001/10/22 15:21:44 1.52
***************
*** 4,13 ****
use strict;
! use Data::Dumper qw( Dumper );
require Exporter;
use SPOPS::Error;
! use SPOPS::Tie qw( IDX_CHANGE IDX_SAVE IDX_CHECK_FIELDS IDX_LAZY_LOADED );
! use SPOPS::Secure qw( SEC_LEVEL_WRITE );
! use Storable qw( store retrieve nstore );
$SPOPS::AUTOLOAD = '';
--- 4,14 ----
use strict;
! use Data::Dumper qw( Dumper );
require Exporter;
use SPOPS::Error;
! use SPOPS::Tie qw( IDX_CHANGE IDX_SAVE IDX_CHECK_FIELDS IDX_LAZY_LOADED );
! use SPOPS::Secure qw( SEC_LEVEL_WRITE );
! use SPOPS::Utility qw();
! use Storable qw( store retrieve nstore );
$SPOPS::AUTOLOAD = '';
***************
*** 116,124 ****
}
- 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 ) );
--- 117,125 ----
}
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 ) );
***************
*** 129,133 ****
my $defaults = $p->{default_values} || $CONFIG->{default_values};
if ( ref $defaults eq 'HASH' and ! $p->{skip_default_values} ) {
! map { $data{ $_ } = $defaults->{ $_ } } keys %{ $defaults };
}
--- 130,155 ----
my $defaults = $p->{default_values} || $CONFIG->{default_values};
if ( ref $defaults eq 'HASH' and ! $p->{skip_default_values} ) {
! foreach my $field ( keys %{ $defaults } ) {
! if ( ref $defaults->{ $field } eq 'HASH' ) {
! my $default_class = $defaults->{ $field }{class};
! my $default_method = $defaults->{ $field }{method};
! unless ( $default_class and $default_method ) {
! _w( 0, "Cannot set default for ($field) without a class ",
! "AND method being defined." );
! next;
! }
! $self->{ $field } = eval { $default_class->$default_method( $field ) };
! if ( $@ ) {
! _w( 0, "Cannot set default for ($field) in ($class) using",
! "($default_class) ($default_method): $@" );
! }
! }
! elsif ( $defaults->{ $field } eq 'NOW' ) {
! $self->{ $field } = SPOPS::Utility->now;
! }
! else {
! $self->{ $field } = $defaults->{ $field };
! }
! }
}
***************
*** 849,852 ****
--- 871,885 ----
object is initialized with C<new()>.
+ Normally the values of the hashref are the defaults to which you want
+ to set the fields. However, there are two special cases of values:
+
+ B<'NOW'> This string will insert the current timestamp in the format
+ C<yyyy-mm-dd hh:mm:ss>.
+
+ B<\%> A hashref with the keys 'class' and 'method' will get executed
+ as a class method and be passed the name of the field for which we
+ want a default. The method should return the default value for this
+ field.
+
One problem with setting default values in your object configuration
B<and> in your database is that the two may become unsynchronized,
***************
*** 1578,1579 ****
--- 1611,1613 ----
=cut
+ <
|