| 1 | package DBIx::Changeset::Record::Disk; | ||||||||||
| 2 | |||||||||||
| 3 | 7 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 7 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 7 | ▶ | use vars qw{$VERSION}; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 7 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 7 | $VERSION = '1.11'; | |||||||||
| 9 | } | ||||||||||
| 10 | |||||||||||
| 11 | 7 | ▶ | use base qw/DBIx::Changeset::Record/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 7 | ▶ | use IO::File; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 7 | ▶ | use File::Spec; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 7 | ▶ | use Encode; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 7 | 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 | 8 | ▶ | sub read { | ||||||||
|
Subroutine
read: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 40 | 8 | my ($self) = @_; | |||||||||
| 41 | 8 | my $fh = new IO::File; | |||||||||
| 42 | 8 | my $file = File::Spec->catfile($self->changeset_location, $self->uri); | |||||||||
| 43 | 8 | my $data; | |||||||||
| 44 | 8 | ▶ | if ($fh->open("< $file")) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 7 | ▶ | while( <$fh> ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | 45 | $data .= $_; | |||||||||
| 47 | } | ||||||||||
| 48 | 7 | $fh->close; | |||||||||
| 49 | } else { | ||||||||||
| 50 | 1 | DBIx::Changeset::Exception::ReadRecordException->throw(error => 'Could not open file for reading.'); | |||||||||
| 51 | } | ||||||||||
| 52 | 7 | return $data; | |||||||||
| 53 | } | ||||||||||
| 54 | |||||||||||
| 55 | =head2 write | ||||||||||
| 56 | This is the write interface implement in your own class | ||||||||||
| 57 | =cut | ||||||||||
| 58 | 5 | ▶ | sub write { | ||||||||
|
Subroutine
write: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 59 | 5 | my ($self, $data) = @_; | |||||||||
| 60 | 5 | my $fh = IO::File->new(); | |||||||||
| 61 | 5 | my $file = File::Spec->catfile($self->changeset_location, $self->uri); | |||||||||
| 62 | 5 | ▶ | if ( -e $file ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 1 | DBIx::Changeset::Exception::DuplicateRecordNameException->throw(error => 'Could not open file for writing as it already exists.', filename => $file); | |||||||||
| 64 | 0 | return; | |||||||||
| 65 | } | ||||||||||
| 66 | 4 | ▶ | if ($fh->open("> $file")) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 4 | printf $fh $data; | |||||||||
| 68 | 1 | $fh->close; | |||||||||
| 69 | } else { | ||||||||||
| 70 | 0 | DBIx::Changeset::Exception::WriteRecordException->throw(error => 'Could not open file for writing.'); | |||||||||
| 71 | } | ||||||||||
| 72 | 1 | return; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | =head2 md5 | ||||||||||
| 76 | |||||||||||
| 77 | Return the md5 of the files contents | ||||||||||
| 78 | |||||||||||
| 79 | =cut | ||||||||||
| 80 | 1 | ▶ | sub md5 { | ||||||||
|
Subroutine
md5: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 81 | 1 | my $self = shift; | |||||||||
| 82 | |||||||||||
| 83 | 1 | my $md5 = Digest::MD5::md5_hex( Encode::encode_utf8( $self->read ) ); | |||||||||
| 84 | |||||||||||
| 85 | 1 | 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 | 7 | 1; # End of DBIx::Changeset::Record::Disk | |||||||||