File Coverage

blib/lib/SQL/Library.pm
Criterion Covered Total %
statement 43 52 82.6
branch 9 18 50.0
condition n/a
subroutine 8 9 88.8
pod 7 7 100.0
total 67 86 77.9


line stmt bran cond sub pod time code
1             package SQL::Library;
2              
3 1     1   32101 use strict;
  1         2  
  1         38  
4 1     1   6 use warnings;
  1         2  
  1         970  
5              
6             our $VERSION = '0.0.5';
7              
8             sub new
9             {
10 1     1 1 14 my $proto = shift;
11 1         2 my $options = shift;
12 1         3 my $self = {
13             'options' => $options,
14             'contents' => undef
15             };
16              
17 1         2 my $curr_name = '';
18              
19 1         2 my @lib_arr = ();
20 1 50       5 if ( ref $self->{'options'}->{'lib'} eq 'ARRAY' )
21             {
22             # Could be a filehandle or a string.
23 0 0       0 if ( @{ $self->{'options'}->{'lib'} } == 1 )
  0         0  
24             {
25 0         0 @lib_arr = split /(?<=\n)/, $self->{'options'}->{'lib'}->[0];
26             }
27             else
28             {
29 0         0 @lib_arr = @{ $self->{'options'}->{'lib'} };
  0         0  
30             }
31             }
32             else
33             {
34 1 50       431 open LIB, $self->{'options'}->{'lib'}
35             or die "Cannot open $self->{'options'}->{'lib'}: $!";
36 1         36 @lib_arr = ;
37 1         11 close LIB;
38             }
39              
40 1         2 foreach ( @lib_arr )
41             {
42 10 100       34 next if m{^\s*$};
43 8 50       18 next if m{^\s*#};
44 8 50       16 next if m{^\s*//};
45 8 100       21 if ( m{^\[([^\]]+)\]} )
46             {
47 2         9 $curr_name = $1;
48 2         3 next;
49             }
50 6 50       14 if ( $curr_name )
51             {
52 6         18 $self->{'contents'}->{$curr_name} .= $_;
53             }
54             }
55              
56 1         3 bless $self, $proto;
57 1         6 return $self;
58             }
59              
60             sub retr
61             {
62 4     4 1 1157 my ( $self, $entity_name ) = @_;
63 4         26 return $self->{'contents'}->{$entity_name};
64             }
65              
66             sub set
67             {
68 2     2 1 5 my ( $self, $entity_name, $entity ) = @_;
69 2         6 $self->{'contents'}->{$entity_name} = $entity;
70 2         5 return $self;
71             }
72              
73             sub drop
74             {
75 1     1 1 3 my ( $self, $entity_name ) = @_;
76 1         5 delete $self->{'contents'}->{$entity_name};
77 1         3 return $self;
78             }
79              
80             sub elements
81             {
82 1     1 1 2 my $self = shift;
83 1         2 return sort keys %{$self->{'contents'}};
  1         13  
84             }
85              
86             sub dump
87             {
88 1     1 1 2 my $self = shift;
89 1         2 my $output = '';
90 1         3 foreach ( sort keys %{$self->{'contents'}} )
  1         5  
91             {
92 2         12 $output .= sprintf "[%s]\n%s\n", $_, $self->{'contents'}->{$_};
93             }
94 1         6 return $output;
95             }
96              
97             sub write
98             {
99 0     0 1   my $self = shift;
100 0 0         open OUT, ">$self->{'options'}->{'lib'}"
101             or die "Cannot open $self->{'options'}->{'lib'}: $!";
102 0           print OUT $self->dump;
103 0           close OUT;
104             }
105              
106             1 ;
107             __END__