docs and config substitutions
[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
6use File::Spec::Functions 'catfile';
7
8sub _load {
9 my ($class, $path) = @_;
10 open(my $fh, '<', $path)
11 || die "can't open $path: $!";
12 local $/ = undef;
13 my $content = <$fh>;
14}
15
16sub _save {
17 my ($class, $path, $content) = @_;
18 open (my $fh, '>', $path)
19 || die "can't open $path: $!";
20 print $fh $content;
21 close($fh);
22}
23
c040a9b0 24sub backup {
6d01d9c1 25 my ($class, $key, $args) = @_;
26 my $path = catfile($args->{path}, $key);
c040a9b0 27 return $class->_load($path);
6d01d9c1 28}
29
c040a9b0 30sub restore {
6d01d9c1 31 my ($class, $key, $content, $args) = @_;
32 my $path = catfile($args->{path}, $key);
33 $class->_save($path, $content);
34}
35
361;
c040a9b0 37
38=head1 NAME
39
40DBIx::Class::Fixtures::External::File - save and restore external data
41
42=head1 SYNOPSIS
43
44 my $fixtures = DBIx::Class::Fixtures
45 ->new({
46 config_dir => 't/var/configs',
47 config_attrs => { photo_dir => './t/var/files' });
48
49 {
50 "sets": [{
51 "class": "Photo",
52 "quantity": "all",
53 "external": {
54 "file": {
55 "class": "File",
56 "args": {"path":"__ATTR(photo_dir)__"}
57 }
58 }
59 }]
60 }
61
62=head1 DESCRIPTION
63
64Sometimes your database fields are pointers to external data. The classic
65example is you are using L<DBIx::Class::InflateColumn::FS> to manage blob
66data. In these cases it may be desirable to backup and restore the external
67data via fixtures.
68
69This module performs this function and can also serve as an example for your
70possible custom needs.
71
72=head1 METHODS
73
74This module defines the following methods
75
76=head2 backup
77
78Accepts: Value of Database Field, $args
79
80Given the value of a database field (which is some sort of pointer to the location
81of an actual file, and a hashref of args (passed in the args key of your config
82set), slurp up the file and return to to be saved in the fixure.
83
84=head2 restore
85
86Accepts: Value of Database Field, Content, $args
87
88Given the value of a database field, some blob content and $args, restore the
89file to the filesystem
90
91=head1 AUTHOR
92
93 See L<DBIx::Class::Fixtures> for author information.
94
95=head1 CONTRIBUTORS
96
97 See L<DBIx::Class::Fixtures> for contributor information.
98
99=head1 LICENSE
100
101 See L<DBIx::Class::Fixtures> for license information.
102
103=cut
104