File Coverage

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


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