File Coverage

blib/lib/DBIx/Changeset/Collection/Disk.pm
Criterion Covered Total %
statement 63 63 100.0
branch 7 8 87.5
condition 2 3 66.6
subroutine 13 13 100.0
pod 4 4 100.0
total 89 91 97.8


line stmt bran cond sub pod time code
1             package DBIx::Changeset::Collection::Disk;
2              
3 3     3   6132 use warnings;
  3         5  
  3         95  
4 3     3   16 use strict;
  3         5  
  3         92  
5              
6 3     3   14 use base qw/DBIx::Changeset::Collection/;
  3         6  
  3         240  
7 3     3   1152 use DBIx::Changeset::Record;
  3         23  
  3         34  
8 3     3   932 use IO::Dir;
  3         30373  
  3         176  
9 3     3   2656 use File::Slurp qw/read_file/;
  3         22354  
  3         206  
10 3     3   24 use POSIX qw/strftime/;
  3         7  
  3         23  
11              
12 3     3   160 use vars qw{$VERSION};
  3         6  
  3         111  
13             BEGIN {
14 3     3   1424 $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             sub retrieve_all {
38 3     3 1 9955 my ($self) = @_;
39 3         24 $self->retrieve_like(qr/\.sql$/xm);
40 2         15 return;
41             }
42              
43             sub retrieve_like {
44 5     5 1 1431 my ($self,$regex) = @_;
45            
46 5         45 my $d = IO::Dir->new($self->changeset_location);
47            
48 5 100 66     594 if ( ( defined $d ) && ( -d $self->changeset_location ) ) {
49 4         117 my @files = ();
50 4         25 while (defined($_ = $d->read)) {
51 32 100       554 next unless $_ =~ m!$regex!mx;
52 13         53 push @files, DBIx::Changeset::Record->new('disk', { changeset_location => $self->changeset_location, uri => $_ });
53             }
54 4         72 $self->files(\@files);
55 4         129 undef $d;
56             } else {
57             # Exception
58 1         18 DBIx::Changeset::Exception::ReadCollectionException->throw(error => 'Could not open changeset_location for reading.');
59             }
60            
61 4         26 $self->sort_changesets();
62              
63 4         15 return;
64             }
65              
66             =head2 add_changeset
67              
68             =cut
69              
70             sub add_changeset {
71 4     4 1 2531 my ($self,$name) = @_;
72              
73             ### create the record
74 4         643 $name = POSIX::strftime("%Y%m%d_$name", localtime(time));
75              
76 4         37 my $record = DBIx::Changeset::Record->new('disk', { changeset_location => $self->changeset_location, uri => $name.".sql" });
77              
78             ### read in the record template
79 4         13 my $template;
80 4 100       29 if ( $self->create_template ) {
81             # open and read the template
82 2         27 $template = read_file($self->create_template);
83             } else {
84             # Exception
85 2         49 DBIx::Changeset::Exception::MissingAddTemplateException->throw(error => 'Missing create template path');
86             }
87            
88 2         229 $record->generate_uid($template);
89 1         9 $record->validate();
90              
91 1 50       8 unless ( defined $self->files ) {
92 1         11 $self->files([]);
93             }
94              
95 1         13 push @{$self->files}, $record;
  1         3  
96              
97 1         8 $self->sort_changesets();
98              
99 1         3 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             sub sort_changesets {
109 5     5 1 155 my $self = shift;
110            
111 15         91 my @sorted = sort {
112 5         27 my $auri = $a->uri;
113 15         159 my $buri = $b->uri;
114              
115 15         168 $auri cmp $buri;
116              
117 5         10 } @{$self->files};
118              
119 5         37 $self->files(\@sorted);
120              
121 5         54 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             1; # End of DBIx::Changeset::Collection::Disk