| 1 | package Stepford::Role::Step::FileGenerator::Atomic; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | our $VERSION = '0.006001'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Path::Class qw( file ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Scope::Guard qw( guard ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use Stepford::Types qw( File ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | with 'Stepford::Role::Step::FileGenerator'; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 3 | has pre_commit_file => ( | |||||||||||||||||||||||||||
| 19 | is => 'ro', | ||||||||||||||||||||||||||||
| 20 | isa => File, | ||||||||||||||||||||||||||||
| 21 | lazy => 1, | ||||||||||||||||||||||||||||
| 22 | builder => '_build_pre_commit_file', | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 9 | ▶ | sub BUILD { } | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 10 | ▶ | before BUILD => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 27 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 10 | my @production_names = sort map { $_->name } $self->productions; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 10 | ▶ | croak 'The ' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | . ( ref $self ) | ||||||||||||||||||||||||||||
| 33 | . ' class consumed the Stepford::Role::Step::FileGenerator::Atomic' | ||||||||||||||||||||||||||||
| 34 | . " role but contains more than one production: @production_names" | ||||||||||||||||||||||||||||
| 35 | if @production_names > 1; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 9 | return; | |||||||||||||||||||||||||||
| 38 | 3 | }; | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 8 | ▶ | sub _build_pre_commit_file { | ||||||||||||||||||||||||||
|
Subroutine
_build_pre_commit_file: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 8 | my $final_file = ( $self->productions )[0]; | |||||||||||||||||||||||||||
| 44 | 8 | my $reader = $final_file->get_read_method; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 8 | return file( $self->$reader . '.tmp' ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 10 | ▶ | around run => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 50 | 10 | my $orig = shift; | |||||||||||||||||||||||||||
| 51 | 10 | my $self = shift; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 10 | my $pre_commit = $self->pre_commit_file; | |||||||||||||||||||||||||||
| 54 | 10 | ▶ | my $guard = guard { $pre_commit->remove if -f $pre_commit }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 10 | $self->$orig(@_); | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 8 | my $read_method = ( $self->productions )[0]->get_read_method; | |||||||||||||||||||||||||||
| 59 | 8 | my $post_commit = $self->$read_method; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # The step's run method may decide to simply not do anything if the | ||||||||||||||||||||||||||||
| 62 | # post-commit file already exists, and that's ok. | ||||||||||||||||||||||||||||
| 63 | 8 | ▶ | return if -f $post_commit && !-f $pre_commit; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
-f $post_commit and not -f $pre_commit
MC/DC100%2/2
-f $post_commit and not -f $pre_commit
-f $post_commit
not -f $pre_commit
Truth table100%3/3
A and not(B)
A= -f $post_commitB= not -f $pre_commit
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 7 | ▶ | croak 'The ' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | . ( ref $self ) | ||||||||||||||||||||||||||||
| 67 | . ' class consumed the Stepford::Role::Step::FileGenerator::Atomic' | ||||||||||||||||||||||||||||
| 68 | . ' role but run produced no pre-commit production file at:' | ||||||||||||||||||||||||||||
| 69 | . " $pre_commit" | ||||||||||||||||||||||||||||
| 70 | unless -f $pre_commit; | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 6 | $self->logger->debug("Renaming $pre_commit to $post_commit"); | |||||||||||||||||||||||||||
| 73 | 6 | ▶ | rename( $pre_commit, $post_commit ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | or croak "Failed renaming $pre_commit -> $post_commit: $!"; | ||||||||||||||||||||||||||||
| 75 | 3 | }; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3 | 1; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # ABSTRACT: A role for steps that generate a file atomically | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | __END__ | ||||||||||||||||||||||||||||