]> Dogcows Code - chaz/p5-File-KDBX/blob - lib/File/KDBX/Transaction.pm
Remove objects from deleted objects when added
[chaz/p5-File-KDBX] / lib / File / KDBX / Transaction.pm
1 package File::KDBX::Transaction;
2 # ABSTRACT: Make multiple database edits atomically
3
4 use warnings;
5 use strict;
6
7 use Devel::GlobalDestruction;
8 use File::KDBX::Util qw(:class);
9 use namespace::clean;
10
11 our $VERSION = '999.999'; # VERSION
12
13 =method new
14
15 $txn = File::KDBX::Transaction->new($object);
16
17 Construct a new database transaction for editing an object atomically.
18
19 =cut
20
21 sub new {
22 my $class = shift;
23 my $object = shift;
24 $object->begin_work(@_);
25 return bless {object => $object}, $class;
26 }
27
28 sub DESTROY { !in_global_destruction and $_[0]->rollback }
29
30 =attr object
31
32 Get the object being transacted on.
33
34 =cut
35
36 has 'object', is => 'ro';
37
38 =method commit
39
40 $txn->commit;
41
42 Commit the transaction, making updates to the L</object> permanent.
43
44 =cut
45
46 sub commit {
47 my $self = shift;
48 return if $self->{done};
49
50 my $obj = $self->object;
51 $obj->commit;
52 $self->{done} = 1;
53 return $obj;
54 }
55
56 =method rollback
57
58 $txn->rollback;
59
60 Roll back the transaction, throwing away any updates to the L</object> made since the transaction began. This
61 happens automatically when the transaction is released, unless it has already been committed.
62
63 =cut
64
65 sub rollback {
66 my $self = shift;
67 return if $self->{done};
68
69 my $obj = $self->object;
70 $obj->rollback;
71 $self->{done} = 1;
72 return $obj;
73 }
74
75 1;
This page took 0.040409 seconds and 4 git commands to generate.