blib/lib/DBIx/Changeset/Collection/Disk.pm

filter: Statementstmt 100% 64 / 64 Branchbran 90.0% 9 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 96.8% 93 / 96 CC 7 SCAR 8.6
CC
7
Cov
97%
retrieve_like
5.0
add_changeset
3.0
BEGIN
1.0
CRAP 2.4
« blib/lib/DBIx/Changeset/Collection.pm ↑ Summary blib/lib/DBIx/Changeset/Record/Disk.pm »
1 package DBIx::Changeset::Collection::Disk;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use base qw/DBIx::Changeset::Collection/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use DBIx::Changeset::Record;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use IO::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use File::Slurp qw/read_file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use POSIX qw/strftime/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 $VERSION = '1.11';
15 }
16  
17 =head1 NAME
18  
19 DBIx::Changeset::Collection::Disk - Read changeset files from the disk
20  
21 =head1 SYNOPSIS
22  
23 Read changeset files from the disk
24  
25 this is a factory object and should be called the DBIx::Changeset::Collection factory
26  
27 use DBIx::Changeset::Collection;
28  
29 my $foo = DBIx::Changeset::Collection->new('disk', $opts);
30 ...
31 $foo->retrieve('moose.sql');
32  
33 =head1 METHODS
34  
35 =head2 retrieve_all
36 =cut
37 3 ▶sub retrieve_all {
Subroutine retrieve_all: called CC=1 SCAR=0.0
Pod documented
38 3 my ($self) = @_;
39 3 $self->retrieve_like(qr/\.sql$/xm);
40 2 return;
41 }
42  
43 5 ▶sub retrieve_like {
Subroutine retrieve_like: called CC=5 SCAR=16.1
Pod documented
44 5 my ($self,$regex) = @_;
45
46 5 my $d = IO::Dir->new($self->changeset_location);
47
48 5 ▶ if ( ( defined $d ) && ( -d $self->changeset_location ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d and -d $self->changeset_location) { } 4 1 5
Condition66%2/3
defined $d and -d $self->changeset_location
!ll&&!rl&&r
104
MC/DC50%1/2
defined $d and -d $self->changeset_location
defined $d -d $self->changeset_location
Truth table66%2/3
A and B
A= defined $dB= -d $self->changeset_location
ABresult
0X0
100
111
49 4 my @files = ();
50 4 ▶ while (defined($_ = $d->read)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = $d->read)) 28 4 32
51 28 ▶ next unless $_ =~ m!$regex!mx;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /$regex/mx 17 11 28
52 11 push @files, DBIx::Changeset::Record->new('disk', { changeset_location => $self->changeset_location, uri => $_ });
53 }
54 4 $self->files(\@files);
55 4 undef $d;
56 } else {
57 # Exception
58 1 DBIx::Changeset::Exception::ReadCollectionException->throw(error => 'Could not open changeset_location for reading.');
59 }
60
61 4 $self->sort_changesets();
62  
63 4 return;
64 }
65  
66 =head2 add_changeset
67  
68 =cut
69  
70 4 ▶sub add_changeset {
Subroutine add_changeset: called CC=3 SCAR=11.0
Pod documented
71 4 my ($self,$name) = @_;
72  
73 ### create the record
74 4 $name = POSIX::strftime("%Y%m%d_$name", localtime(time));
75  
76 4 my $record = DBIx::Changeset::Record->new('disk', { changeset_location => $self->changeset_location, uri => $name.".sql" });
77  
78 ### read in the record template
79 4 my $template;
80 4 ▶ if ( $self->create_template ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->create_template) { } 2 2 4
81 # open and read the template
82 2 $template = read_file($self->create_template);
83 } else {
84 # Exception
85 2 DBIx::Changeset::Exception::MissingAddTemplateException->throw(error => 'Missing create template path');
86 }
87
88 2 $record->generate_uid($template);
89 1 $record->validate();
90  
91 1 ▶ unless ( defined $self->files ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->files) 1 0 1
92 1 $self->files([]);
93 }
94  
95 1 push @{$self->files}, $record;
96  
97 1 $self->sort_changesets();
98  
99 1 return File::Spec->catfile($self->changeset_location, $record->uri);
100 }
101  
102 =head2 sort_changesets
103  
104 Sort the changesets in the collection from oldest to newest based ont eh date in the uri
105  
106 =cut
107  
108 5 ▶sub sort_changesets {
Subroutine sort_changesets: called CC=1 SCAR=0.0
Pod documented
109 5 my $self = shift;
110
111 my @sorted = sort {
112 9 my $auri = $a->uri;
113 9 my $buri = $b->uri;
114  
115 9 $auri cmp $buri;
116  
117 5 } @{$self->files};
118  
119 5 $self->files(\@sorted);
120  
121 5 return;
122 }
123  
124 =head1 COPYRIGHT & LICENSE
125  
126 Copyright 2004-2008 Grox Pty Ltd.
127  
128 This program is free software; you can redistribute it and/or modify it
129 under the same terms as Perl itself.
130  
131 The full text of the license can be found in the LICENSE file included with this module.
132  
133 =cut
134  
135 3 1; # End of DBIx::Changeset::Collection::Disk
« blib/lib/DBIx/Changeset/Collection.pm ↑ Summary blib/lib/DBIx/Changeset/Record/Disk.pm »