Config/UI.pm untested

filter: Statementstmt 0.0% 0 / 38 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 64 CC 3 SCAR 13.9
CC
3
Cov
0%
_split_opts
6.0
get_options
6.0
BEGIN
2.0
CRAP 4.0
1 package User::Config::UI;
2  
3 use strict;
4 use warnings;
5  
6 use Moose::Role;
7 require User::Config;
8  
9 our $VERSION = '0.01_00';
10 $VERSION = eval $VERSION; # see L<perlmodstyle>
11  
12 =pod
13  
14 =head1 NAME
15  
16 User::Config::UI - some helper-functions for User::Config UI-Generators
17  
18 =head1 DESCRIPTION
19  
20 Within the User::Config::UI namespace the UI-Generators for L<User::Config>
21 reside. Additionaly this modul will provide some common functions for these.
22  
23 =head1 SEE ALSO
24  
25 L<User::Config>
26  
27 =head2 Known UI-Generators
28  
29 L<User::Config::UI::HTMLFormHandler> - uses L<HTML::FormHandler> to process
30 and generate a HTML-Form
31  
32 =head1 EXPORT
33  
34 =head2 User::Config::UI::get_options
35  
36 This will a hashref of hashes containing all options in all known modules.
37  
38 The tree of the module-namespace is mapped on the references. Hashes with
39 option will contain an additional key called C<is_option>.
40  
41 package Module;
42 ...
43 has_option option1;
44 ...
45 package Module::SubModule;
46 ...
47 has_option option2;
48 ...
49 use Data::Dumper;
50 use User::Config::UI;
51 print Dumper User::Config::UI::get_options
52  
53 will generate the following
54  
55 VAR1 => {
56 Module => {
57 option1 => {
58 is_option => 1
59 },
60 SubModule => {
61 option2 => {
62 is_option => 1
63 }
64 }
65 }
66 }
67  
68 It is recommended for UI-Generators to use this function to generate their
69 list of options.
70  
71 =cut
72  
73 sub _split_opts {
74 my ($ret, $name) = @_;
75 my @name = split /::/,$name;
76 for(@name) {
77 $ret->{$_} = {} unless $ret->{$_};
78 $ret = $ret->{$_};
79 }
80 return $ret;
81 }
82  
83 sub get_options {
84 my $self = shift;
85 my $ret = {};
86  
87 my $uc = User::Config->instance;
88 my $opts = $uc->options;
89 for(keys %{$opts}) {
90 my $name = $_;
91 next unless $name =~ $self->modulefilter;
92 my $kopt = _split_opts($ret, $name);
93 my $nso = $opts->{$name};
94 map {
95 $kopt->{$_} = $opts->{$name}->{$_};
96 $kopt->{$_}->{is_option} => 1
97 } grep {
98 $nso->{$_} and not
99 ($nso->{$_}->{hidden} or
100 $nso->{$_}->{noset} or
101 $nso->{$_}->{references})
102 } keys %{$nso};
103 }
104 return $ret;
105 }
106  
107 =head2 ATTRIBUTES
108  
109 =head3 modulefilter
110  
111 If given in the initialization of an UI-object, this will specifiy a
112 regular expression, all modules to configure must match.
113  
114 By default all options in all modules will be shown.
115  
116 =cut
117  
118 has modulefilter => (
119 is => "ro",
120 isa => "RegexpRef",
121 default => sub { qr/./ },
122 );
123  
124 =head1 AUTHOR
125  
126 Benjamin Tietz E<lt>benjamin@micronet24.deE<gt>
127  
128 =head1 COPYRIGHT AND LICENSE
129  
130 Copyright (C) 2011 by Benjamin Tietz
131  
132 This library is free software; you can redistribute it and/or modify
133 it under the same terms as Perl itself, either Perl version 5.10.1 or,
134 at your option, any later version of Perl 5 you may have available.
135  
136 =cut
137  
138 1;
139