X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fp5-File-KDBX;a=blobdiff_plain;f=lib%2FFile%2FKDBX%2FTransaction.pm;h=0ed48b2141e8db8b904c1c6f94bf3d4a05f99076;hp=10e8b3f6b539fb1bf41425cf9d076d3ff6724f42;hb=05e0bcef1c2165c556b910314312866dc4a667b7;hpb=af22194015e0d2182beecda0d9f40e59784ce398 diff --git a/lib/File/KDBX/Transaction.pm b/lib/File/KDBX/Transaction.pm index 10e8b3f..0ed48b2 100644 --- a/lib/File/KDBX/Transaction.pm +++ b/lib/File/KDBX/Transaction.pm @@ -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 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 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; }