Config.pm

filter: Statementstmt 44.6% 42 / 94 Branchbran 4.7% 2 / 42 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 61.1% 11 / 18 Podpod 100% 2 / 2 totaltotal 31.6% 57 / 180 CC 27 SCAR 41.1
CC
27
Cov
29%
__ANON__
182.0
_context_test
30.0
__ANON__
12.0
CRAP 60.9
↑ Summary Config/DB.pm »
1 package User::Config;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use User::Config::DB::Mem;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.02_01';
13 1 $VERSION = eval $VERSION; # see L<perlmodstyle>
14  
15 #Moose::Exporter->setup_import_methods is called at the end of this document,
16 # to make sure all accessors are valid
17  
18 =pod
19  
20 =head1 NAME
21  
22 User::Config - Handling the per-user configuration in multi-user applications
23  
24 =head1 SYNOPSIS
25  
26 use User::Config;
27  
28 has_option foo => (
29 isa => 'String',
30 documentation => 'a sample option',
31 default => 'foobar',
32 dataclass => 'myclass',
33 presets => {
34 "Use this" => "value",
35 "or this" => "another value",
36 custom => undef,
37 },
38 );
39  
40 has_option bar => (
41 isa => 'Integer',
42 default => sub { return 28; },
43 hidden => 1,
44 );
45  
46 ...
47 $setting = $self->foo;
48
49 =head1 DESCRIPTION
50  
51 This primaly documents this modules interface. Please see
52 L<User::Config::Manual> for an in-depth discussion of the concepts.
53  
54 =cut
55  
56 1 my $instance;
57  
58 0 ▶sub _context_test {
Subroutine _context_test: not called CC=5 SCAR=34.0
59 0 my ($val) = @_;
60 0 ▶ return 0 unless $val and ref $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val and ref $val 0 0 0
Condition0%0/3
$val and ref $val
!ll&&!rl&&r
000
MC/DC0%0/2
$val and ref $val
$val ref $val
Truth table0%0/3
A and B
A= $valB= ref $val
ABresult
0X0
100
111
61 0 ▶ return 1 if exists $val->{user};
Branch: 1 branch
BranchTrueFalseTotal
if exists $val->{"user"} 0 0 0
62 0 ▶ return 1 if $val->can("user");
Branch: 1 branch
BranchTrueFalseTotal
if $val->can("user") 0 0 0
63 0 return 0;
64 }
65  
66 =head2 EXPORTS
67  
68 =head3 has_option name;
69  
70 C<has_option> is the way to declare new user-configurable options. To specify
71 the behaviour of this modul, the extended syntax with parameters can be used.
72 The following parameters are currently supported:
73  
74 =over 4
75  
76 =item C<<anon_default => ($default|sub { ... })>>
77  
78 Will be used as C<default>, but only if no user logged in. If this isn't given, C<default> is used.
79  
80 =item C<<coerce => (1|0)>>
81  
82 Use coercion. See L<Moose/has>
83  
84 =item C<<dataclass => $db_class>>
85  
86 this entry might be used by the database-backend to decide how or where
87 to save the entry
88  
89 =item C<<default => ($default|sub { ... })>>
90  
91 defines the default value for a given entry. If defined this either might be
92 a scalar, which will be used as value for this option, as long as the user
93 didn't set it to something else. Or it might be a CODE-ref. In this case
94 the code will be executed the first time this option is read for a certain
95 user. On this first time it automaticly will be set in the backend.
96  
97 =item C<<documentation => "Documentative string">>
98  
99 Document the option. This will be accessable as $self->item->documentation and
100 may be used by UI-Generators.
101  
102 =item C<<hidden => (1|0)>>
103  
104 If this set to a true value, the corresponding item will be ignored by the
105 UI-Generator.
106  
107 =item C<<isa => $type_name>>
108  
109 set up runtime constrain checking for this option using L<Moose/has>.
110  
111 =item C<<lazy => (1|0)>>
112  
113 See L<Moose/has>
114  
115 =item C<<noset => (1|0)>>
116  
117 If set to something true, a value set to this value will silently be
118 ignored. This implies C<hidden>.
119 This might be useful in conjunction with C<default>/C<anon_default>, if
120 a database-connection night not be available.
121  
122 =item C<<range => [ $min, $max ]>>
123  
124 if this is set, the UI-Generator will assure, that the user didn't set
125 a value outside.
126  
127 =item C<<references => 'Path::To::Modul::option_name'>>
128  
129 this tells User::Config to access the C<option_name> of the Modul
130 C<Path::To::Modul>, when this option is accessed. Thus both options always
131 will have the same value. The UI-Generator will silently ignore this option
132 and do not a display a corresponding entry to the user.
133  
134 =item C<<trigger => sub { ... }>>
135  
136 triggers the following code every time the option is set. See L<Moose/has> for
137 details.
138  
139 =item C<<ui_type => 'String'>>
140  
141 this will be used by the UI-Generator to provide a suitable element.
142  
143 =item C<<validate => CODEREF>>
144  
145 a code that will be executed, if the user set's a new value.
146  
147 =item C<<weak_ref => (1|0)>>
148  
149 See L<Moose/has>
150  
151 =back
152  
153 =cut
154  
155 1 ▶sub has_option {
Subroutine has_option: called CC=3 SCAR=22.4
Pod documented
156 1 my ($meta, $name, %options) = @_;
157  
158 1 my $call = Moose::Util::_caller_info();
159 1 my $be = User::Config::instance();
160 1 ▶ Moose->throw_error(
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 == 1 0 1 1
161 'Usage: has_option \'name\' => ( key => value, ... )')
162 if @_ % 2 == 1;
163 1 $options{is_option} = 1;
164 1 $be->options($call->{package}, $name, \%options);
165  
166 0 ▶ if($options{references}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"references"}) 0 0 0
167 0 my @name = split(/::/,$options{references});
168 0 my $module = join('::', @name[0..($#name-1)]);
169 0 my $option = $name[-1];
170 $meta->add_attribute($name => (
171 0 ▶ accessor => { $name => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 0 my $self = shift;
173 0 return eval "$module->$option(\@_)";
174 }},
175 definition_context => $call,
176 0 map { $_ => $options{$_} }
177 0 grep { exists $options{$_} }
178 qw/isa coerce weak_ref lazy trigger documentation/,
179 ));
180 0 return;
181 }
182  
183 $meta->add_attribute($name => (
184 accessor => {
185 0 ▶ $name => sub {
Subroutine __ANON__: not called CC=13 SCAR=52.0
186 0 my $self = shift;
187 0 my $option = User::Config->instance;
188 0 my ($context, $user);
189 0 my $getter = defined wantarray;
190 0 ▶ $context = shift
Branch: 2 branches
BranchTrueFalseTotal
if $getter ? $_[0] : defined $_[1] and _context_test($_[0]) 0 0 0
$getter ? : 0 0 0
Condition0%0/3
$getter ? $_[0] : defined $_[1] and _context_test($_[0])
!ll&&!rl&&r
000
MC/DC0%0/2
$getter ? $_[0] : defined $_[1] and _context_test($_[0])
$getter ? $_[0] : defined $_[1] _context_test($_[0])
Truth table0%0/3
A and B
A= $getter ? $_[0] : defined $_[1]B= _context_test($_[0])
ABresult
0X0
100
111
191 if(($getter?($_[0]):(defined $_[1])) and
192 _context_test($_[0]));
193 0 ▶ $context = $self->context unless $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $context 0 0 0
194 0 ▶ $context = $option->context unless $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $context 0 0 0
195 0 ▶ if($context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context) 0 0 0
196 $user = $context->{user}
197 0 ▶ if exists $context->{user};
Branch: 1 branch
BranchTrueFalseTotal
if exists $context->{"user"} 0 0 0
198 0 ▶ $user = $context->user
Branch: 1 branch
BranchTrueFalseTotal
if not defined $user and ref $context ne "HASH" and $context->can("user") 0 0 0
Condition0%0/6
not defined $user and ref $context ne "HASH" and $context->can("user")
!ll&&!rl&&r
000
not defined $user and ref $context ne "HASH"
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $user and ref $context ne "HASH" and $context->can("user")
not defined $user ref $context ne "HASH" $context->can("user")
Truth table0%0/4
not(A) and B and C
A= not defined $userB= ref $context ne "HASH"C= $context->can("user")
ABCresult
0XX0
10X0
1100
1111
199 if not defined $user and
200 ref $context ne "HASH" and
201 $context->can("user");
202 }
203 0 my $ns = blessed($self);
204 0 ▶ $ns = $self unless $ns;
Branch: 1 branch
BranchTrueFalseTotal
unless $ns 0 0 0
205 0 ▶ if($getter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($getter) { } 0 0 0
206 0 return $option->db->get($ns,
207 $user, $name, $context);
208 } else {
209 0 $option->db->set($ns,
210 $user, $name, $context, shift);
211 }
212 }
213 },
214 definition_context => $call,
215 0 map { $_ => $options{$_} }
216 0 grep { exists $options{$_} }
217 qw/isa coerce weak_ref lazy trigger documentation/,
218 ));
219 }
220  
221 =head2 METHODS
222  
223 =head3 $self->context({ User => 'foo' });
224  
225 This can be used to set the global or module-wide context.
226 See L<User::Config::Manual> for more details.
227  
228 =cut
229  
230 has context => (
231 is => 'rw',
232 0 ▶ trigger => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
233 0 my ($self, $new, $old) = @_;
234 0 ▶ return unless defined $new;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new 0 0 0
235 0 ▶ return if _context_test($new);
Branch: 1 branch
BranchTrueFalseTotal
if _context_test($new) 0 0 0
236 # no user found; reset to old value
237 0 $self->context($old);
238 },
239 1 );
240  
241 =head3 my $uc = User::Config->instance
242  
243 C<instance> will always return a handle to the global over-all object handling
244 with the database-backend and storing references to all configuration
245 items
246  
247 =cut
248  
249 has instance => (
250 reader => {
251 1 ▶ instance => sub {
Subroutine instance: called CC=2 SCAR=7.2
252 1 ▶ $instance = User::Config->new unless $instance;
Branch: 1 branch
BranchTrueFalseTotal
unless $instance 1 0 1
253 1 return $instance;
254 }
255 }
256 1 );
257  
258 =head3 $uc->db("Interface", { parameters => "" })
259  
260 To set the database used to make any user-configuration persistent, the
261 database should be set using this method. The first argument specifies the
262 interface to be used, the second contains arguments for this interface to
263 initialize.
264  
265 For a list of valid Interfaces see L<User::Config::DB>
266  
267 =cut
268  
269 has db => (
270 1 ▶ default => sub { User::Config::DB::Mem->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
271 accessor => {
272 0 ▶ db => sub {
Subroutine db: not called CC=3 SCAR=24.8
273 0 my ($self, $val, $arg ) = @_;
274  
275 # Sometimes the writer is called instead of the reader?
276 0 ▶ return $self->{db} unless $val;
Branch: 1 branch
BranchTrueFalseTotal
unless $val 0 0 0
277 0 my $mod = "User::Config::DB::$val";
278 0 my $ret = eval "require $mod; $mod->new(\$arg)";
279 0 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
280 0 $self->{db} = $ret;
281 0 return $ret;
282 }
283 },
284 1 );
285  
286  
287 =head3 $uc->ui("Interface", { parmeters => "" })
288  
289 This will return a new instance for generating a userinterface. There might be
290 multiple interfaces supported. To choose one of them, provide the name of the
291 Interface as first parameter. Valid names are any modules within
292 L<User::Config::UI>. The parameters to supply will depend on the interface in
293 use, so please refer to it's documentation.
294  
295 =cut
296  
297 0 ▶sub ui {
Subroutine ui: not called CC=2 SCAR=17.9
Pod documented
298 0 my ($self, $name, $params) = @_;
299  
300 0 my $mod = "User::Config::UI::$name";
301 0 my $ret = eval "require $mod; $mod->new(\$params)";
302 0 ▶ croak $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
303 0 return $ret;
304 }
305  
306 =head3 $uc->options
307  
308 This will return a hash-ref containing all currently registered options. The
309 content will look like the following example. The reference of a given
310 option will be the same as used in C<has_option>.
311  
312 $ perl ... -e "print Dumper $uc->options"
313 $VAR1 = {
314 My::Namespace => {
315 option1 => {
316 },
317 option2 => {
318 hidden => 1,
319 },
320 };
321  
322 =cut
323  
324 has options => (
325 is => "rw",
326 accessor => {
327 0 ▶ options => sub {
Subroutine options: not called CC=3 SCAR=24.8
328 0 my $self = shift;
329 0 ▶ $self->{options}->{$_[0]}->{$_[1]} = $_[2]
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ and (caller)[0] eq "User::Config" 0 0 0
Condition0%0/3
scalar @_ and (caller)[0] eq "User::Config"
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ and (caller)[0] eq "User::Config"
scalar @_ (caller)[0] eq "User::Config"
Truth table0%0/3
A and B
A= scalar @_B= (caller)[0] eq "User::Config"
ABresult
0X0
100
111
330 if scalar @_ and (caller)[0] eq "User::Config";
331 0 return $self->{options};
332  
333 },
334 },
335 1 );
336  
337 =head1 SEE ALSO
338  
339 L<User::Config::Manual> - a introduction into the concept behind User::Config
340  
341 L<User::Config::DB> - for a Overview over the database-backends
342  
343 L<User::Config::UI> - for more information on generating User-Interfaces
344  
345 =head1 AUTHOR
346  
347 Benjamin Tietz, E<lt>benjamin@micronet24.deE<gt>
348  
349 =head1 COPYRIGHT AND LICENSE
350  
351 Copyright (C) 2011 by Benjamin Tietz
352  
353 This library is free software; you can redistribute it and/or modify
354 it under the same terms as Perl itself, either Perl version 5.10.1 or,
355 at your option, any later version of Perl 5 you may have available.
356  
357  
358 =cut
359  
360 1 Moose::Exporter->setup_import_methods(
361 with_meta => [ qw/has_option/ ],
362 as_is => [ qw/context/ ],
363 also => 'Moose',
364 );
365  
366 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
367 1 __PACKAGE__->meta->make_immutable;
368  
369 1 1;
↑ Summary Config/DB.pm »