File Coverage

blib/lib/AlignDB/SQL/Library.pm
Criterion Covered Total %
statement 57 57 100.0
branch 13 18 72.2
condition n/a
subroutine 10 10 100.0
pod 1 8 12.5
total 81 93 87.1


line stmt bran cond sub pod time code
1             package AlignDB::SQL::Library;
2 4     4   82098 use Moose;
  4         1675374  
  4         30  
3 4     4   29149 use AlignDB::SQL;
  4         12  
  4         2632  
4              
5             has 'lib' => ( is => 'rw', isa => 'Str', required => 1 );
6             has 'contents' => ( is => 'rw', isa => 'HashRef', default => sub { {} } );
7              
8             sub BUILD {
9 3     3 0 3284 my $self = shift;
10              
11 3         8 my @lib_arr = ();
12 3 100       101 if ( -e $self->lib ) {
13 2 50       66 open my $lib_fh, '<', $self->lib
14             or die "Cannot open file: $!";
15 2         48 @lib_arr = <$lib_fh>;
16 2         19 close $lib_fh;
17             }
18              
19 3         5 my $curr_name = '';
20 3         10 foreach (@lib_arr) {
21 57 100       101 next if m{^\s*$};
22 54 50       69 next if m{^\s*#};
23 54 50       75 next if m{^\s*//};
24 54 100       74 if (m{^\[([^\]]+)\]}) {
25 3         8 $curr_name = $1;
26 3         5 next;
27             }
28 51 50       71 if ($curr_name) {
29 51         67 $self->{contents}->{$curr_name} .= $_;
30             }
31             }
32              
33 3         12 return;
34             }
35              
36             sub retr {
37 9     9 0 1049 my $self = shift;
38 9         14 my $entity_name = shift;
39 9         40 return $self->{contents}->{$entity_name};
40             }
41              
42             #@method
43             #@returns AlignDB::SQL
44             sub retrieve {
45 2     2 0 9 my $self = shift;
46 2         5 my $entity_name = shift;
47              
48 2         8 my $thaw_sql = AlignDB::SQL->thaw( $self->retr($entity_name) );
49 2         23181 return $thaw_sql;
50             }
51              
52             sub set {
53 4     4 0 11 my $self = shift;
54 4         7 my $entity_name = shift;
55 4         5 my $entity = shift;
56              
57 4 100       13 if ( ref $entity eq 'AlignDB::SQL' ) {
58 2         7 $entity->_sql( $entity->as_sql );
59 2         11 $self->{contents}->{$entity_name} = $entity->freeze;
60             }
61             else {
62 2         5 $self->{contents}->{$entity_name} = $entity;
63             }
64              
65 4         15423 return $self;
66             }
67              
68             sub drop {
69 2     2 0 101 my $self = shift;
70 2         4 my $entity_name = shift;
71 2         6 delete $self->{contents}->{$entity_name};
72 2         4 return $self;
73             }
74              
75             sub elements {
76 1     1 0 2 my $self = shift;
77 1         1 return sort keys %{ $self->{contents} };
  1         11  
78             }
79              
80             sub dump {
81 2     2 1 5 my $self = shift;
82 2         4 my $output = '';
83 2         4 foreach ( sort keys %{ $self->{contents} } ) {
  2         14  
84 3         19 $output .= sprintf "[%s]\n%s\n", $_, $self->{contents}->{$_};
85             }
86 2         19 return $output;
87             }
88              
89             sub write {
90 1     1 0 7836 my $self = shift;
91              
92 1 50       42 open my $lib_fh, '>', $self->lib
93             or die "Cannot open file: $!";
94 1         2 print {$lib_fh} $self->dump;
  1         7  
95 1         89 close $lib_fh;
96              
97 1         6 return $self;
98             }
99              
100             1;
101              
102             __END__