File Coverage

blib/lib/BioX/Workflow/Command/run/Rules/Directives/Types/Path.pm
Criterion Covered Total %
statement 15 41 36.5
branch 0 14 0.0
condition n/a
subroutine 5 9 55.5
pod 0 3 0.0
total 20 67 29.8


line stmt bran cond sub pod time code
1             package BioX::Workflow::Command::run::Rules::Directives::Types::Path;
2              
3 1     1   715 use Moose::Role;
  1         3  
  1         7  
4 1     1   4820 use MooseX::Types::Path::Tiny qw/Path Paths AbsPath AbsFile/;
  1         3  
  1         17  
5 1     1   3180 use Path::Tiny;
  1         2  
  1         61  
6 1     1   6 use Cwd;
  1         2  
  1         53  
7 1     1   7 use Data::Walk;
  1         2  
  1         477  
8              
9             =head2 File Options
10              
11             =head3 indir outdir
12              
13             The initial indir is where samples are found
14              
15             All output is written relative to the outdir
16              
17             =cut
18              
19             has 'indir' => (
20             is => 'rw',
21             isa => Path,
22             coerce => 1,
23             required => 0,
24             default => sub { cwd(); },
25             predicate => 'has_indir',
26             clearer => 'clear_indir',
27             documentation => q(Directory to look for samples),
28             );
29              
30             has 'outdir' => (
31             is => 'rw',
32             isa => Path,
33             coerce => 1,
34             required => 0,
35             default => sub { cwd(); },
36             predicate => 'has_outdir',
37             clearer => 'clear_outdir',
38             documentation => q(Output directories for rules and processes),
39             );
40              
41             has 'cwd' => (
42             is => 'rw',
43             isa => Path,
44             coerce => 1,
45             required => 0,
46             default => sub { cwd(); },
47             predicate => 'has_cwd',
48             clearer => 'clear_cwd',
49             documentation => q(Placeholder for the cwd.),
50             );
51              
52             =head3 INPUT OUTPUT
53              
54             Special variables that can have input/output
55              
56             =cut
57              
58             has 'OUTPUT' => (
59             is => 'rw',
60             required => 0,
61             predicate => 'has_OUTPUT',
62             documentation => q(At the end of each process the OUTPUT becomes
63             the INPUT.)
64             );
65              
66             has 'INPUT' => (
67             is => 'rw',
68             required => 0,
69             predicate => 'has_INPUT',
70             documentation => q(See OUTPUT)
71             );
72              
73             =head3 coerce_abs_dir
74              
75             Coerce dirs to absolute paths (True)
76             Keep paths as relative directories (False)
77              
78             =cut
79              
80             has 'coerce_abs_dir' => (
81             is => 'rw',
82             isa => 'Bool',
83             default => 1,
84             documentation => q{Coerce '*_dir' to absolute directories},
85             predicate => 'has_coerce_abs_dir',
86             clearer => 'clear_coerce_abs_dir',
87             );
88              
89             =head3 create_outdir
90              
91             =cut
92              
93             has 'create_outdir' => (
94             is => 'rw',
95             isa => 'Bool',
96             predicate => 'has_create_outdir',
97             clearer => 'clear_create_outdir',
98             documentation =>
99             q(Create the outdir. You may want to turn this off if doing a rule that doesn't write anything, such as checking if files exist),
100             default => 1,
101             );
102              
103             after 'BUILD' => sub {
104             my $self = shift;
105              
106             $self->set_register_process_directives(
107             'path',
108             {
109             builder => 'process_directive_path',
110             lookup =>
111             [ '^indir$', '^outdir$', '^INPUT$', '^OUTPUT$', '.*_dir$' ]
112             }
113             );
114             };
115              
116             =head2 Type Specific Process Directives
117              
118             Sometimes you want a type specific way of processing directives. This will generally require two different methods
119              
120             process_directive*
121             walk_directives*
122              
123             process_directive gives some official parameters
124             walk_directives walks the data structure
125              
126             Take a look at the methods in 'BioX::Workflow::Command::run::Rules::Directives::Walk'
127              
128             Your functions will probably be very similar
129              
130             =cut
131              
132             sub process_directive_path {
133 0     0 0   my $self = shift;
134 0           my $k = shift;
135 0           my $v = shift;
136              
137 0 0         if ( ref($v) ) {
138             walk {
139 0     0     wanted => sub { $self->walk_directives_path( @_ ) }
140             },
141 0           $self->$k;
142             }
143             else {
144 0           my $text = '';
145 0 0         $text = $self->interpol_directive($v) if $v;
146 0 0         if ( $text ne '' ) {
147 0           $text = $self->return_path($text);
148             }
149 0           $self->$k($text);
150             }
151             }
152              
153             =head3 walk_directives_paths
154              
155             Invoke with
156             walk { wanted => sub { $self->directives(@_) } }, $self->other_thing;
157              
158             Acts funny with $self->some_other_thing is not a reference
159              
160             =cut
161              
162             sub walk_directives_path {
163 0     0 0   my $self = shift;
164 0           my $ref = shift;
165              
166 0 0         return if ref($ref);
167 0 0         return unless $ref;
168              
169 0           my $text = '';
170 0 0         $text = $self->interpol_directive($ref) if $ref;
171 0           $text = $self->return_path($text);
172              
173 0           $self->update_directive($text);
174             }
175              
176             sub return_path {
177 0     0 0   my $self = shift;
178 0           my $text = shift;
179              
180 0 0         if ( $self->coerce_abs_dir ) {
181 0           $text = path($text)->absolute;
182 0           $text = path($text);
183             }
184             else {
185 0           $text = path($text);
186             }
187 0           return $text;
188             }
189              
190             1;