]> Dogcows Code - chaz/p5-File-KDBX/blob - lib/File/KDBX/Transaction.pm
10e8b3f6b539fb1bf41425cf9d076d3ff6724f42
[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 namespace::clean;
9
10 our $VERSION = '999.999'; # VERSION
11
12 sub new {
13 my $class = shift;
14 my $object = shift;
15 my $orig = shift // $object->clone;
16 return bless {object => $object, original => $orig}, $class;
17 }
18
19 sub DESTROY { !in_global_destruction and $_[0]->rollback }
20
21 sub object { $_[0]->{object} }
22 sub original { $_[0]->{original} }
23
24 sub commit {
25 my $self = shift;
26 my $obj = $self->object;
27 if (my $commit = $obj->can('_commit')) {
28 $commit->($obj, $self);
29 }
30 $self->{committed} = 1;
31 return $obj;
32 }
33
34 sub rollback {
35 my $self = shift;
36 return if $self->{committed};
37
38 my $obj = $self->object;
39 my $orig = $self->original;
40
41 %$obj = ();
42 @$obj{keys %$orig} = values %$orig;
43
44 return $obj;
45 }
46
47 1;
This page took 0.031614 seconds and 3 git commands to generate.