blib/lib/DBIx/Class/Fixtures/External/File.pm

filter: Statementstmt 100% 30 / 30 Branchbran 50.0% 3 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 93.4% 43 / 46 CC 4 SCAR 4.4
CC
4
Cov
92%
_load
2.0
_save
2.0
restore
2.0
CRAP 1.6
« blib/lib/DBIx/Class/Fixtures/DBI/mysql.pm ↑ Summary lib/DBIx/Class/Fixtures/DBI/mysql.pm »
1 package DBIx::Class::Fixtures::External::File;
2 3 $DBIx::Class::Fixtures::External::File::VERSION = '1.001034';
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  
6 3 ▶use File::Spec::Functions 'catfile', 'splitpath';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use File::Path 'mkpath';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub _load {
10 1 ▶ my ($class, $path) = @_;
Subroutine _load: called CC=2 SCAR=7.0
11 1 ▶ open(my $fh, '<', $path)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $path or die "can't open ${path}: $!" 0 1 1
12 || die "can't open $path: $!";
13 1 local $/ = undef;
14 1 my $content = <$fh>;
15 }
16  
17 sub _save {
18 1 ▶ my ($class, $path, $content) = @_;
Subroutine _save: called CC=2 SCAR=7.0
19 1 ▶ open (my $fh, '>', $path)
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $path or die "can't open ${path}: $!" 0 1 1
20 || die "can't open $path: $!";
21 1 print $fh $content;
22 1 close($fh);
23 }
24  
25 sub backup {
26 1 ▶ my ($class, $key, $args) = @_;
Subroutine backup: called CC=1 SCAR=0.0
Pod documented
27 1 my $path = catfile($args->{path}, $key);
28 1 return $class->_load($path);
29 }
30  
31 sub restore {
32 1 ▶ my ($class, $key, $content, $args) = @_;
Subroutine restore: called CC=2 SCAR=7.0
Pod documented
33 1 my $path = catfile($args->{path}, $key);
34 1 my ($vol, $directory, $file) = splitpath($path);
35 1 ▶ mkpath($directory) unless -d $directory;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $directory 0 1 1
36 1 $class->_save($path, $content);
37 }
38  
39 3 1;
40  
41 =head1 NAME
42  
43 DBIx::Class::Fixtures::External::File - save and restore external data
44  
45 =head1 SYNOPSIS
46  
47 my $fixtures = DBIx::Class::Fixtures
48 ->new({
49 config_dir => 't/var/configs',
50 config_attrs => { photo_dir => './t/var/files' });
51  
52 {
53 "sets": [{
54 "class": "Photo",
55 "quantity": "all",
56 "external": {
57 "file": {
58 "class": "File",
59 "args": {"path":"__ATTR(photo_dir)__"}
60 }
61 }
62 }]
63 }
64  
65 =head1 DESCRIPTION
66  
67 Sometimes your database fields are pointers to external data. The classic
68 example is you are using L<DBIx::Class::InflateColumn::FS> to manage blob
69 data. In these cases it may be desirable to backup and restore the external
70 data via fixtures.
71  
72 This module performs this function and can also serve as an example for your
73 possible custom needs.
74  
75 =head1 METHODS
76  
77 This module defines the following methods
78  
79 =head2 backup
80  
81 Accepts: Value of Database Field, $args
82  
83 Given the value of a database field (which is some sort of pointer to the location
84 of an actual file, and a hashref of args (passed in the args key of your config
85 set), slurp up the file and return to to be saved in the fixure.
86  
87 =head2 restore
88  
89 Accepts: Value of Database Field, Content, $args
90  
91 Given the value of a database field, some blob content and $args, restore the
92 file to the filesystem
93  
94 =head1 AUTHOR
95  
96 See L<DBIx::Class::Fixtures> for author information.
97  
98 =head1 CONTRIBUTORS
99  
100 See L<DBIx::Class::Fixtures> for contributor information.
101  
102 =head1 LICENSE
103  
104 See L<DBIx::Class::Fixtures> for license information.
105  
106 =cut
107  
« blib/lib/DBIx/Class/Fixtures/DBI/mysql.pm ↑ Summary lib/DBIx/Class/Fixtures/DBI/mysql.pm »