]> Dogcows Code - chaz/p5-File-KDBX/blobdiff - lib/File/KDBX/Transaction.pm
Add recursive transactions
[chaz/p5-File-KDBX] / lib / File / KDBX / Transaction.pm
index 10e8b3f6b539fb1bf41425cf9d076d3ff6724f42..0ed48b2141e8db8b904c1c6f94bf3d4a05f99076 100644 (file)
@@ -9,38 +9,65 @@ use namespace::clean;
 
 our $VERSION = '999.999'; # VERSION
 
+=method new
+
+    $txn = File::KDBX::Transaction->new($object);
+
+Construct a new database transaction for editing an object atomically.
+
+=cut
+
 sub new {
-    my $class = shift;
-    my $object = shift;
-    my $orig   = shift // $object->clone;
-    return bless {object => $object, original => $orig}, $class;
+    my $class   = shift;
+    my $object  = shift;
+    $object->begin_work(@_);
+    return bless {object => $object}, $class;
 }
 
 sub DESTROY { !in_global_destruction and $_[0]->rollback }
 
-sub object   { $_[0]->{object} }
-sub original { $_[0]->{original} }
+=attr object
+
+Get the object being transacted on.
+
+=cut
+
+sub object { $_[0]->{object} }
+
+=method commit
+
+    $txn->commit;
+
+Commit the transaction, making updates to the L</object> permanent.
+
+=cut
 
 sub commit {
     my $self = shift;
+    return if $self->{done};
+
     my $obj = $self->object;
-    if (my $commit = $obj->can('_commit')) {
-        $commit->($obj, $self);
-    }
-    $self->{committed} = 1;
+    $obj->commit;
+    $self->{done} = 1;
     return $obj;
 }
 
+=method rollback
+
+    $txn->rollback;
+
+Roll back the transaction, throwing away any updates to the L</object> made since the transaction began. This
+happens automatically when the transaction is released, unless it has already been committed.
+
+=cut
+
 sub rollback {
     my $self = shift;
-    return if $self->{committed};
+    return if $self->{done};
 
     my $obj = $self->object;
-    my $orig = $self->original;
-
-    %$obj = ();
-    @$obj{keys %$orig} = values %$orig;
-
+    $obj->rollback;
+    $self->{done} = 1;
     return $obj;
 }
 
This page took 0.017662 seconds and 4 git commands to generate.