more correctly restore path with external file
[dbsrgits/DBIx-Class-Fixtures.git] / lib / DBIx / Class / Fixtures / External / File.pm
CommitLineData
6d01d9c1 1package DBIx::Class::Fixtures::External::File;
2
3use strict;
4use warnings;
5
a3ebae95 6use File::Spec::Functions 'catfile', 'splitpath';
7use File::Path 'mkpath';
6d01d9c1 8
9sub _load {
10 my ($class, $path) = @_;
11 open(my $fh, '<', $path)
12 || die "can't open $path: $!";
13 local $/ = undef;
14 my $content = <$fh>;
15}
16
17sub _save {
18 my ($class, $path, $content) = @_;
19 open (my $fh, '>', $path)
20 || die "can't open $path: $!";
21 print $fh $content;
22 close($fh);
23}
24
c040a9b0 25sub backup {
6d01d9c1 26 my ($class, $key, $args) = @_;
27 my $path = catfile($args->{path}, $key);
c040a9b0 28 return $class->_load($path);
6d01d9c1 29}
30
c040a9b0 31sub restore {
6d01d9c1 32 my ($class, $key, $content, $args) = @_;
a3ebae95 33 my ($vol, $directory, $file) = splitpath catfile($args->{path}, $key);
34 mkpath($directory) unless -d $directory;
35 my $path = catfile($vol, $directory, $file);
6d01d9c1 36 $class->_save($path, $content);
37}
38
391;
c040a9b0 40
41=head1 NAME
42
43DBIx::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
67Sometimes your database fields are pointers to external data. The classic
68example is you are using L<DBIx::Class::InflateColumn::FS> to manage blob
69data. In these cases it may be desirable to backup and restore the external
70data via fixtures.
71
72This module performs this function and can also serve as an example for your
73possible custom needs.
74
75=head1 METHODS
76
77This module defines the following methods
78
79=head2 backup
80
81Accepts: Value of Database Field, $args
82
83Given the value of a database field (which is some sort of pointer to the location
84of an actual file, and a hashref of args (passed in the args key of your config
85set), slurp up the file and return to to be saved in the fixure.
86
87=head2 restore
88
89Accepts: Value of Database Field, Content, $args
90
91Given the value of a database field, some blob content and $args, restore the
92file 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