| 1 | package SPOPS::Tool::CreateOnly; | ||
| 2 | |||
| 3 | # $Id: CreateOnly.pm,v 3.3 2004/06/02 00:48:24 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Log::Log4perl qw( get_logger ); | ||
| 7 | use SPOPS; | ||
| 8 | use SPOPS::ClassFactory qw( OK ); | ||
| 9 | |||
| 10 | my $log = get_logger(); | ||
| 11 | |||
| 12 | $SPOPS::Tool::CreateOnly::VERSION = sprintf("%d.%02d", q$Revision: 3.3 $ =~ /(\d+)\.(\d+)/); | ||
| 13 | |||
| 14 | sub behavior_factory { | ||
| 15 | my ( $class ) = @_; | ||
| 16 | $log->is_info && | ||
| 17 | $log->info( "Installing create-only persistence methods for [$class]" ); | ||
| 18 | return { read_code => \&generate_persistence_methods }; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub generate_persistence_methods { | ||
| 22 | my ( $class ) = @_; | ||
| 23 | $log->is_info && | ||
| 24 | $log->info( "Generating create-only save() [$class]" ); | ||
| 25 | my $first_isa = $class->CONFIG->{isa}->[0]; | ||
| 26 | no strict 'refs'; | ||
| 27 | *{ "${class}::save" } = | ||
| 28 | sub { | ||
| 29 | my $self = shift; | ||
| 30 | if ( $self->is_saved() ) { | ||
| 31 | SPOPS::Exception->throw( | ||
| 32 | "Objects in [", ref $self, "] can only be created, ", | ||
| 33 | "not updated. No changes made." ); | ||
| 34 | } | ||
| 35 | my $full_method = $first_isa. '::save'; | ||
| 36 | return $self->$full_method( @_ ); | ||
| 37 | }; | ||
| 38 | return OK; | ||
| 39 | } | ||
| 40 | |||
| 41 | 1; | ||
| 42 | |||
| 43 | __END__ |