EntityModel
view release on metacpan or search on metacpan
lib/EntityModel/Transaction.pm view on Meta::CPAN
Takes no parameters.
Returns $self.
=cut
sub mark_goodbye {
my $self = shift;
$self->{on_goodbye}->($self) if exists $self->{on_goodbye};
$self
}
=head2 run
Takes the following (named) parameters:
=over 4
=item *
=back
Returns $self.
=cut
sub run {
my $self = shift;
my $code = shift;
my %args = @_;
$self->{"on_$_"} = delete $args{$_} for grep exists $args{$_}, qw(success failure goodbye);
# Attempt the transaction bit, normally we'd expect
# this to set up a few deferred events and return
# quickly
return $self unless try {
sap($self, $code)->();
1;
} catch {
# Something went wrong, bail
$self->mark_failure;
$self->mark_goodbye;
0;
};
$self;
}
sub commit {
my $self = shift;
return $self if $self->{rolled_back};
$self->{committed} = 1;
$self
}
sub DESTROY {
my $self = shift;
warn "did not finish" unless $self->{committed} || $self->{rolled_back};
}
sub sap {
my ($self, $sub) = @_;
Scalar::Util::weaken $self;
return sub {
$self->$sub(@_);
};
}
1;
__END__
=head1 AUTHOR
Tom Molesworth <cpan@entitymodel.com>
=head1 LICENSE
Copyright Tom Molesworth 2008-2011. Licensed under the same terms as Perl itself.
( run in 0.778 second using v1.01-cache-2.11-cpan-39bf76dae61 )