EvalWrap component
Yuval Kogman [Thu, 31 Jul 2008 23:23:23 +0000 (23:23 +0000)]
lib/DBIx/Class/Journal/EvalWrap.pm [new file with mode: 0644]
lib/DBIx/Class/Schema/Journal.pm
t/lib/DBICTest/Schema.pm

diff --git a/lib/DBIx/Class/Journal/EvalWrap.pm b/lib/DBIx/Class/Journal/EvalWrap.pm
new file mode 100644 (file)
index 0000000..2ed9cb4
--- /dev/null
@@ -0,0 +1,46 @@
+#!/usr/bin/perl
+
+package DBIx::Class::Journal::EvalWrap;
+use base qw(DBIx::Class::Journal);
+
+use strict;
+use warnings;
+
+foreach my $method qw(journal_log_update journal_log_insert journal_log_delete) {
+    local $@;
+    eval "sub $method " . ' {
+    my ( $self, @args ) = @_;
+    local $@;
+    eval { $self->next::method(@args) };
+    warn $@ if $@;
+    }; 1' || warn $@;
+}
+
+__PACKAGE__
+
+__END__
+
+=pod
+
+=head1 NAME
+
+DBIx::Class::Journal::EvalWrap - Wrap all journal ops with an eval { }
+
+=head1 SYNOPSIS
+
+    __PACKAGE__->journal_component("Journal::EvalWrap");
+
+=head1 DESCRIPTION
+
+This component is a wrapper for the row methods in L<DBIx:Class::Journal> that
+aides in retrofitting a schema for journaling, by wrapping all the journal CRUD
+operations with a C<local $@; eval { ... }}.
+
+This is desirable if you'd rather lose journal data than create runtime errors
+when retrofitting existing code.
+
+Use with caution.
+
+=cut
+
+
index 4afae67..b1db8c2 100644 (file)
@@ -11,6 +11,7 @@ __PACKAGE__->mk_classdata('journal_deploy_on_connect');
 __PACKAGE__->mk_classdata('journal_sources'); ## [ source names ]
 __PACKAGE__->mk_classdata('journal_user'); ## [ class, field for user id ]
 __PACKAGE__->mk_classdata('_journal_schema'); ## schema object for journal
+__PACKAGE__->mk_classdata('journal_component');
 __PACKAGE__->mk_classdata('journal_nested_changesets');
 
 use strict;
@@ -59,6 +60,7 @@ sub connection
 
     $self->_journal_schema($journal_schema);
 
+    my $comp = $self->journal_component || "Journal";
 
     ## Create auditlog+history per table
     my %j_sources = map { $_ => 1 } $self->journal_sources
@@ -69,7 +71,7 @@ sub connection
     {
         next unless($j_sources{$s_name});
         $self->create_journal_for($s_name);
-        $self->class($s_name)->load_components('Journal');
+        $self->class($s_name)->load_components($comp);
 #        print STDERR "$s_name :", $self->class($s_name), "\n";
     }
 
index ff49a97..ecf87c8 100644 (file)
@@ -5,6 +5,8 @@ use base qw/DBIx::Class::Schema/;
 
 __PACKAGE__->load_components(qw/+DBIx::Class::Schema::Journal/);
 
+__PACKAGE__->journal_component("Journal::EvalWrap");
+
 no warnings qw/qw/;
 DBICTest::Schema->load_classes(
 qw/