DO/FS/Config.pm untested

filter: Statementstmt 0.0% 0 / 33 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 75 SCAR 30.0
CC
4
Cov
0%
cleanup
6.0
new
6.0
odb
6.0
CRAP 20.0
1 =head1 NAME
2  
3 XAO::DO::FS::Config - embeddable configuration object for XAO::FS
4  
5 =head1 SYNOPSIS
6  
7 use XAO::Projects;
8 use XAO::Objects;
9  
10 my $config=XAO::Objects->new(objname => 'Config',
11 sitename => 'test');
12  
13 XAO::Projects::create_project(name => 'test',
14 object => $config,
15 set_current => 1);
16  
17 my $fsconfig=XAO::Objects->new(objname => 'FS::Config',
18 odb_args => {
19 dsn => 'OS:MySQL_DBI:test_os'
20 user => 'test',
21 password => 'TeSt',
22 });
23  
24 $config->embed(fs => $fsconfig);
25  
26 my $odb=$config->odb();
27  
28 =head1 DESCRIPTION
29  
30 The XAO::DO::FS::Config is normally used in larger projects
31 configurations that are persistent in memory. See L<XAO::DO::Config> for
32 more information on how embeddable configuration objects work.
33  
34 =cut
35  
36 ###############################################################################
37 package XAO::DO::FS::Config;
38 use strict;
39 use XAO::Utils;
40 use XAO::Errors qw(XAO::DO::FS::Config);
41  
42 ##
43 # Prototypes
44 #
45 sub cleanup ($;@);
46 sub disable_special_access ($);
47 sub embeddable_methods ($);
48 sub enable_special_access ($);
49 sub new ($%);
50 sub odb ($;$);
51  
52 ##
53 # Package version for checks and reference
54 #
55 use vars qw($VERSION);
56 $VERSION=(0+sprintf('%u.%03u',(q$Id: Config.pm,v 2.2 2008/02/21 02:22:15 am Exp $ =~ /\s(\d+)\.(\d+)\s/))) || die "Bad VERSION";
57  
58 ###############################################################################
59  
60 =head1 METHODS
61  
62 =over
63  
64 =cut
65  
66 ###############################################################################
67  
68 =item cleanup ()
69  
70 Calls reset() method on the odb to clean up the handler and prepare it
71 for the next session.
72  
73 =cut
74  
75 sub cleanup ($;@) {
76 my $self=shift;
77 $self->{'odb'}->reset(@_) if $self->{'odb'};
78 }
79  
80 ###############################################################################
81  
82 =item disable_special_access ()
83  
84 Disables use of odb() method to set a new value (this is the default
85 state).
86  
87 =cut
88  
89 sub disable_special_access ($) {
90 my $self=shift;
91 delete $self->{special_access};
92 }
93  
94 ###############################################################################
95  
96 =item embeddable_methods ()
97  
98 Used internally by global Config object, returns an array with
99 embeddable method names. Currently there is only one embeddable
100 method -- odb().
101  
102 =cut
103  
104 sub embeddable_methods ($) {
105 qw(odb);
106 }
107  
108 ###############################################################################
109  
110 =item enable_special_access ()
111  
112 Enables use of odb() method to set a new value. Normally you do
113 not need this method.
114  
115 Example:
116  
117 $config->enable_special_access();
118 $config->odb($odb);
119 $config->disable_special_access();
120  
121 =cut
122  
123 sub enable_special_access ($) {
124 my $self=shift;
125 $self->{special_access}=1;
126 }
127  
128 ###############################################################################
129  
130 =item new ($$)
131  
132 Creates a new empty configuration object. If odb_args is given then it
133 will connect to a database using these arguments.
134  
135 Example:
136  
137 my $fsconfig=XAO::Objects->new(objname => 'FS::Config',
138 odb_args => {
139 dsn => 'OS:MySQL_DBI:test_os'
140 user => 'test',
141 password => 'TeSt',
142 });
143  
144 =cut
145  
146 sub new ($%) {
147 my $proto=shift;
148 my $args=get_args(\@_);
149  
150 my $self=bless {},ref($proto) || $proto;
151  
152 if($args->{odb_args}) {
153 $self->{odb}=XAO::Objects->new(merge_refs( { objname => 'FS::Glue' },
154 $args->{odb_args}));
155 }
156  
157 $self;
158 }
159  
160 ###############################################################################
161  
162 =item odb (;$)
163  
164 Returns current database handler. If called with an argument and
165 speciall access is enabled then replaces database handler.
166  
167 =cut
168  
169 sub odb ($;$) {
170 my $self=shift;
171  
172 return $self->{odb} unless @_;
173  
174 $self->{special_access} ||
175 throw XAO::E::DO::FS::Config "odb - setting odb requires special access";
176  
177 $self->{odb}=shift;
178  
179 $self->{odb};
180 }
181  
182 ###############################################################################
183 1;
184 __END__