File Coverage

blib/lib/DBIx/Changeset/Record/Disk.pm
Criterion Covered Total %
statement 44 46 95.6
branch 5 6 83.3
condition n/a
subroutine 11 11 100.0
pod 3 3 100.0
total 63 66 95.4


line stmt bran cond sub pod time code
1             package DBIx::Changeset::Record::Disk;
2              
3 7     7   20401 use warnings;
  7         17  
  7         298  
4 7     7   104 use strict;
  7         16  
  7         333  
5              
6 7     7   40 use vars qw{$VERSION};
  7         15  
  7         384  
7             BEGIN {
8 7     7   176 $VERSION = '1.11';
9             }
10              
11 7     7   39 use base qw/DBIx::Changeset::Record/;
  7         15  
  7         753  
12 7     7   4417 use IO::File;
  7         45004  
  7         1746  
13 7     7   58 use File::Spec;
  7         15  
  7         179  
14 7     7   5329 use Encode;
  7         64080  
  7         3061  
15             require Digest::MD5;
16              
17             =head1 NAME
18              
19             DBIx::Changeset::Record::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::Record factory
26              
27             use DBIx::Changeset::Record;
28              
29             my $foo = DBIx::Changeset::Record->new('Disk', $opts);
30             ...
31             $foo->read('/moose/moose.sql');
32              
33             =head1 METHODS
34              
35             =head2 read
36             This will read a file from the disk in the given location returning the data
37             this is normally called from validate (which will set the id in the File object)
38             =cut
39             sub read {
40 8     8 1 1334 my ($self) = @_;
41 8         45 my $fh = new IO::File;
42 8         316 my $file = File::Spec->catfile($self->changeset_location, $self->uri);
43 8         159 my $data;
44 8 100       43 if ($fh->open("< $file")) {
45 7         462 while( <$fh> ) {
46 45         180 $data .= $_;
47             }
48 7         43 $fh->close;
49             } else {
50 1         49 DBIx::Changeset::Exception::ReadRecordException->throw(error => 'Could not open file for reading.');
51             }
52 7         155 return $data;
53             }
54              
55             =head2 write
56             This is the write interface implement in your own class
57             =cut
58             sub write {
59 5     5 1 120 my ($self, $data) = @_;
60 5         37 my $fh = IO::File->new();
61 5         202 my $file = File::Spec->catfile($self->changeset_location, $self->uri);
62 5 100       204 if ( -e $file ) {
63 2         40 DBIx::Changeset::Exception::DuplicateRecordNameException->throw(error => 'Could not open file for writing as it already exists.', filename => $file);
64 0         0 return;
65             }
66 3 50       16 if ($fh->open("> $file")) {
67 3         365 printf $fh $data;
68 1         8 $fh->close;
69             } else {
70 0         0 DBIx::Changeset::Exception::WriteRecordException->throw(error => 'Could not open file for writing.');
71             }
72 1         39 return;
73             }
74              
75             =head2 md5
76              
77             Return the md5 of the files contents
78              
79             =cut
80             sub md5 {
81 1     1 1 494 my $self = shift;
82              
83 1         3 my $md5 = Digest::MD5::md5_hex( Encode::encode_utf8( $self->read ) );
84              
85 1         19 return $md5;
86             }
87              
88              
89             =head1 COPYRIGHT & LICENSE
90              
91             Copyright 2004-2008 Grox Pty Ltd.
92              
93             This program is free software; you can redistribute it and/or modify it
94             under the same terms as Perl itself.
95              
96             The full text of the license can be found in the LICENSE file included with this module.
97              
98             =cut
99              
100             1; # End of DBIx::Changeset::Record::Disk