File Coverage

blib/lib/HackaMol/X/Roles/ExtensionRole.pm
Criterion Covered Total %
statement 34 34 100.0
branch 10 10 100.0
condition n/a
subroutine 10 10 100.0
pod 3 3 100.0
total 57 57 100.0


line stmt bran cond sub pod time code
1             package HackaMol::X::Roles::ExtensionRole;
2             $HackaMol::X::Roles::ExtensionRole::VERSION = '0.012';
3             # ABSTRACT: Role to assist writing HackaMol extensions to external programs
4 2     2   1315 use 5.008;
  2         5  
5 2     2   8 use Moose::Role;
  2         3  
  2         16  
6 2     2   8658 use Capture::Tiny ':all';
  2         23168  
  2         246  
7 2     2   12 use File::chdir;
  2         2  
  2         133  
8 2     2   7 use Carp;
  2         4  
  2         506  
9              
10             with qw(HackaMol::Roles::ExeRole HackaMol::Roles::PathRole);
11              
12             requires qw(_build_map_in _build_map_out build_command);
13              
14             has 'mol' => (
15             is => 'rw',
16             isa => 'HackaMol::Molecule',
17             predicate => 'has_mol',
18             clearer => 'clear_mol',
19             );
20              
21             has 'map_in' => (
22             is => 'ro',
23             isa => 'CodeRef',
24             predicate => 'has_map_in',
25             builder => '_build_map_in',
26             lazy => 1,
27             );
28             has 'map_out' => (
29             is => 'ro',
30             isa => 'CodeRef',
31             predicate => 'has_map_out',
32             builder => '_build_map_out',
33             lazy => 1,
34             );
35              
36             sub map_input {
37              
38             # pass everything and anything to map_in... i.e. keep @_ in tact
39 2     2 1 41836 my ($self) = @_;
40 2 100       54 local $CWD = $self->scratch if ( $self->has_scratch );
41 2         107 return ( &{ $self->map_in }(@_) );
  2         57  
42              
43             }
44              
45             sub map_output {
46              
47             # pass everything and anything to map_out... i.e. keep @_ in tact
48 2     2 1 81 my ($self) = @_;
49 2 100       182 local $CWD = $self->scratch if ( $self->has_scratch );
50 2         112 return ( &{ $self->map_out }(@_) );
  2         65  
51              
52             }
53              
54             sub capture_sys_command {
55              
56             # run it and return all that is captured
57 5     5 1 8954 my $self = shift;
58 5         8 my $command = shift;
59 5 100       17 unless ( defined($command) ) {
60 4 100       115 return 0 unless $self->has_command;
61 3         89 $command = $self->command;
62             }
63              
64 4 100       147 local $CWD = $self->scratch if ( $self->has_scratch );
65             my ( $stdout, $stderr, @exit ) = capture {
66 4     4   21553 system($command);
67 4         362 };
68 4         3098 return ( $stdout, $stderr, @exit );
69             }
70              
71 2     2   9 no Moose::Role;
  2         2  
  2         20  
72              
73             1;
74              
75             __END__
76              
77             =pod
78              
79             =head1 NAME
80              
81             HackaMol::X::Roles::ExtensionRole - Role to assist writing HackaMol extensions to external programs
82              
83             =head1 VERSION
84              
85             version 0.012
86              
87             =head1 SYNOPSIS
88              
89             package HackaMol::X::SomeExtension;
90             use Moose;
91              
92             with qw(HackaMol::X::Roles::ExtensionRole);
93              
94             sub _build_map_in{
95             my $sub_cr = sub { return (@_) };
96             return $sub_cr;
97             }
98              
99             sub _build_map_out{
100             my $sub_cr = sub { return (@_) };
101             return $sub_cr;
102             }
103              
104             sub BUILD {
105             my $self = shift;
106              
107             if ( $self->has_scratch ) {
108             $self->scratch->mkpath unless ( $self->scratch->exists );
109             }
110             }
111              
112             no Moose;
113             1;
114              
115             =head1 DESCRIPTION
116              
117             The HackaMol::X::Roles::ExtensionRole includes methods and attributes that are useful for building extensions
118             with code reuse. This role will improve as extensions are written and needs arise. This role is flexible
119             and can be encapsulated and rigidified in extensions. Advanced use of extensions should still be able to
120             access this flexibility to allow tinkering with internals! Consumes HackaMol::Roles::ExeRole and HackaMol::Roles::PathRole
121             ... ExeRole may be removed from core and wrapped in here.
122              
123             =head1 METHODS
124              
125             =head2 map_input
126              
127             the main function is to change to scratch directory, if set, and pass all arguments (including self) to
128             map_in CodeRef.
129              
130             $calc->map_input(@args);
131              
132             will invoke,
133              
134             &{$calc->map_in}(@_); #where @_ = ($self,@args)
135              
136             and return anything returned by the map_in function. Thus, any input writing should take place in map_in
137             inorder to actually write to the scratch directory.
138              
139             =head2 map_output
140              
141             completely analogous to map_input. Thus, the output must be opened and processed in the
142             map_out function.
143              
144             =head2 build_command
145              
146             builds the command from the attributes: exe, inputfn, exe_endops, if they exist, and returns
147             the command.
148              
149             =head2 capture_sys_command
150              
151             uses Capture::Tiny capture method to run a command using a system call. STDOUT, STDERR, are
152             captured and returned.
153              
154             my ($stdout, $stderr,@other) = capture { system($command) }
155              
156             the $command is taken from $calc->command unless the $command is passed,
157              
158             $calc->capture_sys_command($some_command);
159              
160             capture_sys_command returns ($stdout, $stderr,@other) or 0 if there is no command set.
161              
162             =head1 ATTRIBUTES
163              
164             =head2 scratch
165              
166             Coerced to be 'Path::Tiny' via AbsPath. If scratch is set, map_input and map_output will local CWD to the
167             scratch to carry out operations. See HackaMol::PathRole for more information about the scratch attribute
168             and other attributes available (such as in_fn and out_fn).
169              
170             =head2 mol
171              
172             isa HackaMol::Molecule that is ro
173              
174             =head2 map_in
175              
176             isa CodeRef that is ro. The default builder is required for consuming classes.
177              
178             intended for mapping input files from molecular information, but it is completely
179             flexible. Used in map_input method. Can also be directly ivoked,
180              
181             &{$calc->map_in}(@args);
182              
183             as any other subroutine would be. Extensions can build the map_in function so that it returns
184             the content of $input which can then be written within API methods.
185              
186             =head2 map_out
187              
188             isa CodeRef that is ro. The default builder is required for consuming classes.
189              
190             intended for mapping molecular information from output files, but it is completely
191             flexible and analogous to map_in.
192              
193             =head1 CONSUMES
194              
195             =over 4
196              
197             =item * L<HackaMol::Roles::ExeRole>
198              
199             =item * L<HackaMol::Roles::ExeRole|HackaMol::Roles::PathRole>
200              
201             =item * L<HackaMol::Roles::PathRole>
202              
203             =back
204              
205             =head1 AUTHOR
206              
207             Demian Riccardi <demianriccardi@gmail.com>
208              
209             =head1 COPYRIGHT AND LICENSE
210              
211             This software is copyright (c) 2016 by Demian Riccardi.
212              
213             This is free software; you can redistribute it and/or modify it under
214             the same terms as the Perl 5 programming language system itself.
215              
216             =cut