blib/lib/MOBY/dbConfig.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 151 CC 16 SCAR 32.2
CC
16
Cov
0%
_readSections
56.0
new
30.0
AUTOLOAD
30.0
CRAP 25.0
1 package MOBY::dbConfig;
2 use strict;
3 use Carp;
4 use vars qw($AUTOLOAD);
5  
6 use vars qw /$VERSION/;
7 $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /: (\d+)\.(\d+)/;
8  
9 use Text::Shellwords;
10 {
11  
12 #Encapsulated class data
13 #___________________________________________________________
14 #ATTRIBUTES
15 my %_attr_data = # DEFAULT ACCESSIBILITY
16 (
17 section_title => [ undef, 'read/write' ],
18 username => [ undef, 'read/write' ],
19 password => [ undef, 'read/write' ],
20 dbname => [ undef, 'read/write' ],
21 port => [ undef, 'read/write' ],
22 proxy => [ undef, 'read/write' ],
23 adaptor => [ "MOBY::Adaptor::moby::queryapi::mysql", 'read/write' ],
24 url => [ undef, 'read/write' ],
25 section => [ undef, 'read/write' ],
26 );
27  
28 #_____________________________________________________________
29 # METHODS, to operate on encapsulated class data
30 # Is a specified object attribute accessible in a given mode
31 sub _accessible {
32 my ( $self, $attr, $mode ) = @_;
33 $_attr_data{$attr}[1] =~ /$mode/;
34 }
35  
36 # Classwide default value for a specified object attribute
37 sub _default_for {
38 my ( $self, $attr ) = @_;
39 $_attr_data{$attr}[0];
40 }
41  
42 # List of names of all specified object attributes
43 sub _standard_keys {
44 keys %_attr_data;
45 }
46  
47 sub database_title {
48 my ( $self, $val ) = @_;
49 $self->section_title($val) if $val;
50 return $self->section_title;
51 }
52 }
53  
54 # this object will contain the full hash of what is in the config file, even if
55 # the key/value pairs are not expected. Only the expected key/value pairs will be available as
56 # methods, however (i.e. those in the _standard_keys hash above)
57 sub new {
58 my ( $caller, %args ) = @_;
59 my $caller_is_obj = ref($caller);
60 my $class = $caller_is_obj || $caller;
61 my $self = bless {}, $class;
62 foreach my $attrname ( $self->_standard_keys ) {
63 if ( exists $args{$attrname} && defined $args{$attrname} ) {
64 $self->{$attrname} = $args{$attrname};
65 } elsif ($caller_is_obj) {
66 $self->{$attrname} = $caller->{$attrname};
67 } else {
68 $self->{$attrname} = $self->_default_for($attrname);
69 }
70 }
71 my $key;
72  
73 #eval {$key = $self->_readSections($self->section);};
74 $key = $self->_readSections( $self->section );
75  
76 #if ($@){die "MOBY Configuration file is misconfigured: dbConfig line 71\n";}
77 #print STDERR "I received the key $key\n";
78 return undef unless $key;
79 return undef unless $key =~ /\S/;
80  
81 #print STDERR "returning the dbConfig object for database title $key\n";
82 $self->section_title($key);
83 return $self;
84 }
85  
86 sub _readSections {
87 my ( $self, $section ) = @_;
88 my $key;
89 my @lines = split "\n", $section;
90 while ( my $l = shift @lines ) {
91 chomp $l;
92 next unless $l;
93 next if $l =~ /\s*\#/; # ignore comments
94 next unless $l =~ /\S/; # ignore pure whitespace;
95  
96 #print STDERR "reading line $l\n";
97 if ( $l =~ /\[(\w+)\]/ ) {
98 $key = $1;
99 while ( my $l2 = shift @lines ) {
100 chomp $l2;
101 last unless ( $l2 =~ /\S/ );
102 my @terms = shellwords($l2);
103 last unless ( scalar @terms >= 2 );
104 $self->{ $terms[0] } = $terms[2];
105 }
106 }
107 }
108  
109 #print STDERR "returning key $key with terms ",(keys %{$self->{$key}})," \n";
110 return $key; # will be undef if this was not a valid section
111 }
112 sub DESTROY { }
113  
114 sub AUTOLOAD {
115 no strict "refs";
116 my ( $self, $newval ) = @_;
117 $AUTOLOAD =~ /.*::(\w+)/;
118 my $attr = $1;
119 if ( $self->_accessible( $attr, 'write' ) ) {
120 *{$AUTOLOAD} = sub {
121 if ( defined $_[1] ) { $_[0]->{$attr} = $_[1] }
122 return $_[0]->{$attr};
123 }; ### end of created subroutine
124 ### this is called first time only
125 if ( defined $newval ) {
126 $self->{$attr} = $newval;
127 }
128 return $self->{$attr};
129 } elsif ( $self->_accessible( $attr, 'read' ) ) {
130 *{$AUTOLOAD} = sub {
131 return $_[0]->{$attr};
132 }; ### end of created subroutine
133 return $self->{$attr};
134 }
135  
136 # Must have been a mistake then...
137 croak "No such method: $AUTOLOAD";
138 }
139 1;