File Coverage

blib/lib/MooseX/Role/Tempdir.pm
Criterion Covered Total %
statement 6 6 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 8 8 100.0


line stmt bran cond sub pod time code
1             package MooseX::Role::Tempdir;
2             $MooseX::Role::Tempdir::VERSION = '0.101';
3 1     1   66326 use MooseX::Role::Parameterized;
  1         560303  
  1         5  
4 1     1   38955 use File::Temp qw//;
  1         18424  
  1         231  
5             #ABSTRACT: Moose role providing temporary directory attributes
6              
7              
8              
9             parameter 'dirs' => (
10             isa => 'ArrayRef[Str]|HashRef[HashRef]',
11             default => sub { [ 'tmpdir' ] },
12             );
13              
14             parameter 'tmpdir_opts' => (
15             isa => 'HashRef',
16             default => sub { {} },
17             );
18              
19             role {
20             my $self = shift;
21             my $dirs = $self->dirs;
22             my $global_opts = $self->tmpdir_opts;
23             if (ref $dirs eq 'ARRAY') {
24             for my $dir (@$dirs) {
25             has $dir => (
26             is => 'ro',
27             isa => 'File::Temp::Dir',
28             lazy => 1,
29             default => sub {
30             return File::Temp->newdir(%$global_opts);
31             },
32             );
33             }
34             }
35             else {
36             for my $dir (keys %$dirs) {
37             has $dir => (
38             is => 'ro',
39             isa => 'File::Temp::Dir',
40             lazy => 1,
41             default => sub {
42             return File::Temp->newdir(%$global_opts, %{$dirs->{$dir}});
43             },
44             )
45             }
46             }
47             };
48              
49             1; # End of MooseX::Role::Tempdir
50              
51             __END__
52              
53             =pod
54              
55             =encoding UTF-8
56              
57             =head1 NAME
58              
59             MooseX::Role::Tempdir - Moose role providing temporary directory attributes
60              
61             =head1 VERSION
62              
63             version 0.101
64              
65             =head1 SYNOPSIS
66              
67             By default, a single 'tmpdir' attribute is provided. It is recursively removed
68             when the object goes out of scope:
69              
70             package My::Awesome::Package;
71             use Moose;
72             with 'MooseX::Role::Tempdir';
73              
74             ...
75              
76             package main;
77             my $thing = My::Awesome::Package->new;
78             open(my $fh, '>', $thing->tmpdir."/somefile") or die "problem: $!";
79              
80             You can also use parameters for more directory attributes and/or options:
81              
82             with 'MooseX::Role::Tempdir' => {
83             dirs => [ qw/tmpdir workdir fundir/ ],
84             tmpdir_opts => { DIR => '/my/alternate/tmp' },
85             };
86              
87             ...
88              
89             open(my $fh, '>', $thing->fundir."/somefile") or die "problem: $!";
90              
91             Or be even more explicit:
92              
93             with 'MooseX::Role::Tempdir' => {
94             dirs => {
95             tmpdir => { TEMPLATE => 'fooXXXXX' },
96             permadir => { DIR => '/some/other/dir', CLEANUP => 0 },
97             },
98             tmpdir_opts => { DIR => '/default/dir' },
99             }
100              
101             ...
102              
103             open(my $fh, '>', $thing->nameddir."/somefile") or die "problem: $!";
104              
105             =head1 ATTRIBUTES
106              
107             For each C<dir> parameter (by default, only C<tmpdir>), a temporary directory
108             attribute is lazily created using L<File::Temp/newdir>. The default options to
109             C<newdir> will apply, unless overriden by further parameters. This means the
110             directory and its contents will be removed when the object using this role goes
111             out of scope.
112              
113             =head1 PARAMETERS
114              
115             Parameters may be given to this role as described in
116             L<MooseX::Role::Parameterized::Tutorial>.
117              
118             =head2 "dirs"
119              
120             A C<dirs> parameter may be an array or hash reference. An array reference will
121             create a temporary directory attribute for each value in the array. A hash
122             reference creates an attribute for each key, and its value must be a hash
123             reference of temporary directory options (see L<File::Temp/newdir>).
124              
125             =head2 "tmpdir_opts"
126              
127             This parameter sets temporary directory options for all attributes, unless
128             overridden for a specific directory as described above (with a C<dirs>
129             hashref).
130              
131             =head1 AUTHOR
132              
133             Brad Barden <b at 13os.net>
134              
135             =head1 COPYRIGHT AND LICENSE
136              
137             This software is Copyright (c) 2018 by Brad Barden.
138              
139             This is free software, licensed under:
140              
141             The ISC License
142              
143             =cut