File Coverage

blib/lib/Prophet/Util.pm
Criterion Covered Total %
statement 18 54 33.3
branch 0 14 0.0
condition n/a
subroutine 6 12 50.0
pod 4 6 66.6
total 28 86 32.5


line stmt bran cond sub pod time code
1             package Prophet::Util;
2 40     40   4312 use strict;
  40         62  
  40         1464  
3 40     40   214 use File::Basename;
  40         57  
  40         3040  
4 40     40   205 use File::Spec;
  40         55  
  40         893  
5 40     40   185 use File::Path;
  40         65  
  40         2183  
6 40     40   1346 use Params::Validate;
  40         19281  
  40         13796  
7              
8             =head2 updir PATH
9              
10             Strips off the filename in the given path and returns the absolute
11             path of the remaining directory.
12              
13             =cut
14              
15             sub updir {
16 0     0 1   my $self = shift;
17 0           my $path = shift;
18 0           my ($file, $dir, undef) = fileparse(File::Spec->rel2abs($path));
19 0           return $dir;
20             }
21              
22             =head2 slurp FILENAME
23              
24             Reads in the entire file whose absolute path is given by FILENAME and
25             returns its contents, either in a scalar or in an array of lines,
26             depending on the context.
27              
28             =cut
29              
30             sub slurp {
31 0     0 1   my $self = shift;
32 0           my $abspath = shift;
33 0 0         open (my $fh, "<", "$abspath") || die "$abspath: $!";
34              
35 0           my @lines = <$fh>;
36 0           close $fh;
37              
38 0 0         return wantarray ? @lines : join('',@lines);
39             }
40              
41             =head2 instantiate_record class => 'record-class-name', uuid => 'record-uuid', app_handle => $self->app_handle
42              
43             Takes the name of a record class (must subclass L), a uuid,
44             and an application handle and returns a new instantiated record object
45             of the given class.
46              
47             =cut
48              
49             sub instantiate_record {
50 0     0 1   my $self = shift;
51 0           my %args = validate(@_, {
52             class => 1,
53             uuid => 1,
54             app_handle => 1
55              
56             });
57 0 0         die $args{class} ." is not a valid class " unless (UNIVERSAL::isa($args{class}, 'Prophet::Record'));
58 0           my $object = $args{class}->new( uuid => $args{uuid}, app_handle => $args{app_handle});
59 0           return $object;
60             }
61              
62             =head2 escape_utf8 REF
63              
64             Given a reference to a scalar, escapes special characters (currently just &, <,
65             >, (, ), ", and ') for use in HTML and XML.
66              
67             Not an object routine (call as Prophet::Util::escape_utf8( \$scalar) ).
68              
69             =cut
70              
71             sub escape_utf8 {
72 0     0 1   my $ref = shift;
73 40     40   232 no warnings 'uninitialized';
  40         64  
  40         15577  
74 0           $$ref =~ s/&/&/g;
75 0           $$ref =~ s/
76 0           $$ref =~ s/>/>/g;
77 0           $$ref =~ s/\(/(/g;
78 0           $$ref =~ s/\)/)/g;
79 0           $$ref =~ s/"/"/g;
80 0           $$ref =~ s/'/'/g;
81             }
82              
83              
84             sub write_file {
85 0     0 0   my $self = shift;
86 0           my %args = validate( @_, { file => 1, content => 1 } );
87              
88 0           my ( undef, $parent, $filename ) = File::Spec->splitpath($args{file});
89 0 0         unless ( -d $parent ) {
90 0           eval { mkpath( [$parent] ) };
  0            
91 0 0         if ( my $msg = $@ ) {
92 0           die "Failed to create directory " . $parent . " - $msg";
93             }
94             }
95              
96 0 0         open( my $fh, ">", $args{file} ) || die $!;
97 0           print $fh scalar( $args{'content'} )
98             ; # can't do "||" as we die if we print 0" || die "Could not write to " . $args{'path'} . " " . $!;
99 0 0         close $fh || die $!;
100             }
101              
102             sub hashed_dir_name {
103 0     0 0   my $hash = shift;
104              
105 0           return ( substr( $hash, 0, 1 ), substr( $hash, 1, 1 ), $hash );
106             }
107              
108             1;