blib/lib/MOBY/Config.pm untested

filter: Statementstmt 0.0% 0 / 99 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 174 CC 10 SCAR 24.9
CC
10
Cov
0%
AUTOLOAD
30.0
getDataAdaptor
20.0
new
12.0
CRAP 12.1
1 #$Id: Config.pm,v 1.5 2008/09/02 13:14:18 kawas Exp $
2  
3 =head1 NAME
4  
5 MOBY::Config.pm - An object containing information about how to get access to teh Moby databases, resources, etc. from the
6 mobycentral.config file
7  
8 =cut
9  
10  
11 =head2 USAGE
12  
13 $CONF = MOBY::Config->new();
14  
15 # for fields in the mobycentral.config file, use the section name
16 # as the first object, then the item name as the hash-hey within that
17 # object.
18 # i.e. $CONF->mobycentral returns a MOBY::dbConfig representing the
19 # [mobycentral] part of the mobycentral.config file
20  
21 $centraluser = $CONF->mobycentral->{username}
22 $centralpass = $CONF->mobycentral->{password}
23 $objectuser = $CONF->mobyobject->{username}
24  
25 $ADAPTOR = $CONF->getDataAdaptor(source => 'mobyobject');
26 # $ADAPTOR is probably a MOBY::adaptor::queryapi::mysql object
27  
28 my $dbh = $ADAPTOR->dbh();
29 my $sth = $object_dbh->prepare("select description from object where object_type = ?");
30 $sth->execute("GenericSequence");
31  
32 =cut
33  
34  
35 package MOBY::Config;
36  
37 BEGIN {}
38 use strict;
39 use Carp;
40 use MOBY::dbConfig;
41 use vars qw($AUTOLOAD);
42 use Text::Shellwords;
43 use vars '$VERSION', '@ISA', '@EXPORT', '$CONFIG';
44  
45 $VERSION = sprintf "%d.%02d", q$Revision: 1.5 $ =~ /: (\d+)\.(\d+)/;
46  
47 @ISA = qw(Exporter);
48 @EXPORT = ('$CONFIG');
49 {
50  
51 my %_attr_data = # DEFAULT ACCESSIBILITY
52 (
53 mobycentral => [ undef, 'read/write' ],
54 mobyobject => [ undef, 'read/write' ],
55 mobynamespace => [ undef, 'read/write' ],
56 mobyservice => [ undef, 'read/write' ],
57 mobyrelationship => [ undef, 'read/write' ],
58 valid_secondary_datatypes => [["String", "Integer", "DateTime", "Float", "Boolean"], 'read'],
59 primitive_datatypes => [["String", "Integer", "DateTime", "Float", "Boolean"], 'read'],
60  
61 );
62  
63 my $file = $ENV{MOBY_CENTRAL_CONFIG};
64 ( -e $file ) || die "MOBY Configuration file $file doesn't exist $!\n";
65 chomp $file;
66 if ( ( -e $file ) && ( !( -d $file ) ) ) {
67 open IN, $file
68 or die
69 "can't open MOBY Configuration file $file for unknown reasons: $!\n";
70 }
71 my @sections = split /(\[\s*\S+\s*\][^\[]*)/s, join "", <IN>;
72  
73 #print STDERR "split into @sections\n";
74 foreach my $section (@sections) {
75  
76 #print STDERR "calling MOBY::dbConfig\n";
77 my $dbConfig =
78 MOBY::dbConfig->new( section => $section )
79 ; # this is an object full of strings, no actual connections. It represents the information in the config file
80 next unless $dbConfig;
81 my $dbname = $dbConfig->section_title;
82 next unless $dbname;
83 $_attr_data{$dbname} = [$dbConfig, 'read']; # something like $_attr_data{mobycentral} = [$config, 'read']
84 }
85  
86 #Encapsulated class data
87 #___________________________________________________________
88 #ATTRIBUTES
89  
90 #_____________________________________________________________
91 # METHODS, to operate on encapsulated class data
92 # Is a specified object attribute accessible in a given mode
93 sub _accessible {
94 my ( $self, $attr, $mode ) = @_;
95 $_attr_data{$attr}[1] =~ /$mode/;
96 }
97  
98 # Classwide default value for a specified object attribute
99 sub _default_for {
100 my ( $self, $attr ) = @_;
101 $_attr_data{$attr}[0];
102 }
103  
104 # List of names of all specified object attributes
105 sub _standard_keys {
106 keys %_attr_data;
107 }
108 }
109  
110 # the expected sections (listed above) will have their dbConfig objects available
111 # as methods. The unexpected sections will have their dbConfig objects available
112 # by $dbConfig = $CONFIG->{section_title}
113 sub new {
114 my ( $caller, %args ) = @_;
115  
116 #print STDERR "creating MOBY::Config\n";
117 my $caller_is_obj = ref($caller);
118 my $class = $caller_is_obj || $caller;
119 my $self = bless {}, $class;
120 foreach my $attrname ( $self->_standard_keys ) {
121 if ( exists $args{$attrname} && defined $args{$attrname} ) {
122 $self->{$attrname} = $args{$attrname};
123 } elsif ($caller_is_obj) {
124 $self->{$attrname} = $caller->{$attrname};
125 } else {
126 $self->{$attrname} = $self->_default_for($attrname);
127 }
128 }
129 # return $self if $self->mobycentral->{'dbname'}; # if it has been set, then just return it
130 #print STDERR "setting the COnfig dbConfig for the title $dbname with object $dbConfig\n\n";
131 $CONFIG = $self;
132 return $self;
133 }
134  
135 sub getDataAdaptor {
136 my ( $self, %args ) = @_;
137 my $source = $args{datasource} || $args{source} || "mobycentral";
138 if ( $self->{"${source}Adaptor"} ) { return $self->{"${source}Adaptor"} }
139 ; # read from cache
140 my $username = $self->$source->{username};# $self->$source returns a MOBY::dbConfig object
141 my $password = $self->$source->{password};
142 my $port = $self->$source->{port};
143 my $dbname = $self->$source->{dbname};
144 my $url = $self->$source->{url};
145 my $adaptor = $self->$source->{adaptor};
146 eval "require $adaptor";
147 return undef if $@;
148 my $ADAPTOR = $adaptor->new( # by default, this is queryapi::mysql
149 username => $username,
150 password => $password,
151 port => $port,
152 dbname => $dbname,
153 url => $url,
154 );
155 if ($ADAPTOR) {
156 $self->{"${source}Adaptor"} = $ADAPTOR; # cache it
157 return $ADAPTOR;
158 } else {
159 return undef;
160 }
161 }
162 sub DESTROY { }
163  
164 sub AUTOLOAD {
165 no strict "refs";
166 my ( $self, $newval ) = @_;
167 $AUTOLOAD =~ /.*::(\w+)/;
168 my $attr = $1;
169 if ( $self->_accessible( $attr, 'write' ) ) {
170 *{$AUTOLOAD} = sub {
171 if ( defined $_[1] ) { $_[0]->{$attr} = $_[1] }
172 return $_[0]->{$attr};
173 }; ### end of created subroutine
174 ### this is called first time only
175 if ( defined $newval ) {
176 $self->{$attr} = $newval;
177 }
178 return $self->{$attr};
179 } elsif ( $self->_accessible( $attr, 'read' ) ) {
180 *{$AUTOLOAD} = sub {
181 return $_[0]->{$attr};
182 }; ### end of created subroutine
183 return $self->{$attr};
184 }
185  
186 # Must have been a mistake then...
187 croak "No such method: $AUTOLOAD";
188 }
189 1;