Commit | Line | Data |
6d01d9c1 |
1 | package DBIx::Class::Fixtures::External::File; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use File::Spec::Functions 'catfile'; |
7 | |
8 | sub _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 | |
16 | sub _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 |
24 | sub backup { |
6d01d9c1 |
25 | my ($class, $key, $args) = @_; |
26 | my $path = catfile($args->{path}, $key); |
c040a9b0 |
27 | return $class->_load($path); |
6d01d9c1 |
28 | } |
29 | |
c040a9b0 |
30 | sub restore { |
6d01d9c1 |
31 | my ($class, $key, $content, $args) = @_; |
32 | my $path = catfile($args->{path}, $key); |
33 | $class->_save($path, $content); |
34 | } |
35 | |
36 | 1; |
c040a9b0 |
37 | |
38 | =head1 NAME |
39 | |
40 | DBIx::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 | |
64 | Sometimes your database fields are pointers to external data. The classic |
65 | example is you are using L<DBIx::Class::InflateColumn::FS> to manage blob |
66 | data. In these cases it may be desirable to backup and restore the external |
67 | data via fixtures. |
68 | |
69 | This module performs this function and can also serve as an example for your |
70 | possible custom needs. |
71 | |
72 | =head1 METHODS |
73 | |
74 | This module defines the following methods |
75 | |
76 | =head2 backup |
77 | |
78 | Accepts: Value of Database Field, $args |
79 | |
80 | Given the value of a database field (which is some sort of pointer to the location |
81 | of an actual file, and a hashref of args (passed in the args key of your config |
82 | set), slurp up the file and return to to be saved in the fixure. |
83 | |
84 | =head2 restore |
85 | |
86 | Accepts: Value of Database Field, Content, $args |
87 | |
88 | Given the value of a database field, some blob content and $args, restore the |
89 | file 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 | |